ITPub博客

首页 > > > 写了10年的代码,我最怕写这段代码

写了10年的代码,我最怕写这段代码

作者:u_453e1a12781dea 时间:2016-05-06 18:03:33 0 删除 编辑
引言:

能够坚持写10年代码的人并不多,有一部份都转行做管理了,有一部份就压根儿的转行了,我的工作还是一直在坚持写代码,功能也写,架构也写。

自己写过的,就把老项目翻出来拷贝拷贝

自己没写过的,就去在网上搜来拷贝拷贝

如果是实在没有拷贝了,就自己写写。

搞来搞去,我最烦的就是写每个软件的用户管理这个功能的代码。最近做了一款刷单了,就有用户管理,随即发一发牢骚。

一:用户管理业务分析

一个软件的用户管理界面,看似简单,其实一点都不简单。
简单是因为其所用的技术简单,说到最本质的技术,就是对一个或者几个表进行CRUD操作。不简单的原因,就是因功能不简单。比如一个用户管理

1,用户的划分

a,OA,ERP系统,要考虑到公司部门的划分,比哪这个用户属于集团->子公司->某部门->某员工,一个用户属于多个部门的情况。
b,就像我这种软件刷软件,虽然不为用户分公司,分部门,但是也要为用户分类别,比如这个用户是买家,还是卖家。

2,用户的自身数据管理

a,注册时要判断用户是否存在。
b,某些软件,需要用户的性别,年龄,居住地址,有些软件又不需要这些用户属性,有些用户属注册了不能修改,有些属性又只能在指定范围修改,反正限制很多。

3,用户的密码维护,登录密码的修改,找回,提款密码的修改等问题。就牵涉到发邮件,发短信的问题。

4,用户的登录判断,比如冻结用户,记录用户的日志,IP等信息。
还有很多……

二:用户管理界面的功能分析

看似一个简单的用户注册,登录,基本资料维护,到少需要5个页面以上。

页面一:登录页面
页面二:注册页面
页面三:用户基本资料维护页面
页面四:找回密码页面
页面五:密码修改页面
并且还牵涉到发邮件,发短信的验证
……

三:工作量评估

看似一个毕业生半天都能搞定的功能,喊我来做,居然要半个月,甚至一个月,并且这个工作量还很饱和。所以我非常不愿意做这个功能。

四:干货分享

在做用户管理的时候,肯定都会对用户的输入信息进行验证
1,验证字符串长度:

public static bool StrLength(this String str, int startLength, int endLength)
{
    bool result = false;
    if (str != null)
    {
        int len = str.Length;
        if (len >= startLength && len <= endLength)
        {
            result = true;
        }
        else
        {
            result = false;
        }
    }
    else
    {
        result = false;
    }
    return result;
}

,验证小写,大写,数字,空格,横杠,小数点

/// <summary>
/// 小写,大写,数字,空格,横杠,小数点
/// </summary>
public static bool AlphaNumericSpaceDashDot(this String str)
{
    bool result = false;
    if (!string.IsNullOrEmpty(str) && System.Text.RegularExpressions.Regex.IsMatch(str, @"^[a-zA-Z0-9 \-\.]*$"))
    {
        result = true;
    }
    else
    {
        result = false;
    }
    return result;
}

3,小写,大写,数字,下划线,字符长度(用于验证用户名,密码)

/// <summary>
/// 小写,大写,数字,下划线,字符长度(用于验证用户名,密码)
/// </summary>
public static bool AlphaNumericUnderscoreLength(this String str,int startLength, int endLength)
{
    bool result = false;  //^[A-Za-z0-9]{7}$ 
    if (!string.IsNullOrEmpty(str) && System.Text.RegularExpressions.Regex.IsMatch(str, @"^[a-zA-Z0-9_]{" + startLength + "," + endLength + "}$"))
    {
        result = true;
    }
    else
    {
        result = false;
    }
    return result;
}

4,小写,大写,数字,横线,字符长度(用于验证GUID)

/// <summary>
/// 小写,大写,数字,横线,字符长度(用于验证GUID)
/// </summary>
public static bool AlphaNumericDashLength(this String str, int startLength, int endLength)
{
    bool result = false;  
    if (!string.IsNullOrEmpty(str) && System.Text.RegularExpressions.Regex.IsMatch(str, @"^[a-zA-Z0-9\-]{" + startLength + "," + endLength + "}$"))
    {
        result = true;
    }
    else
    {
        result = false;
    }
    return result;
}

5,正负小数

/// <summary>
/// 正负小数
/// </summary>
public static bool Numeric(this String str)
{
    bool result = false;
    if (!string.IsNullOrEmpty(str) && System.Text.RegularExpressions.Regex.IsMatch(str, @"^\-?[0-9]*\.?[0-9]*$"))
    {
        result = true;
    }
    else
    {
        result = false;
    }
    return result;
}

6,正负整数

/// <summary>
/// 正负整数
/// </summary>
public static bool Integer(this String str)
{
    bool result = false;
    if (!string.IsNullOrEmpty(str) && System.Text.RegularExpressions.Regex.IsMatch(str, @"^\-?[0-9]*$"))
    {
        result = true;
    }
    else
    {
        result = false;
    }
    return result;
}

7,电子邮箱

/// <summary>
/// 电子邮箱
/// </summary>
public static bool Email(this String str)
{
    bool result = false;
    if (!string.IsNullOrEmpty(str) && System.Text.RegularExpressions.Regex.IsMatch(str, @"^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$"))
    {
        result = true;
    }
    else
    {
        result = false;
    }
    return result;
}

8,网页地址

/// <summary>
/// 网页地址
/// </summary>
public static bool Url(this String str)
{
    bool result = false;

    //正则表达式一  ^^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_=]*)?$
    //正则表达式二  http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?   //取自于 RegularExpressionValidator 控件
    if (!string.IsNullOrEmpty(str) && System.Text.RegularExpressions.Regex.IsMatch(str, @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"))
    {
        result = true;
    }
    else
    {
        result = false;
    }
    return result;
}

五:结尾

当然还有什么QQ验证,电话号码验证,身份证验证,这个我的正则写的不好,希望大家补充,推荐。

转自:博客园
填问卷!赢大奖!DTCC 2016门票等你拿!
2016第七届中国数据库技术大会(DTCC)将于2016年5月12日-14日在北京国际会议中心盛大召开!

本届大会云集了100余位国内外顶尖专家,共设2大主题会场,25个技术专场,吸引了超过5000人次的行业人士参加。

这样一场旷世盛宴,你,想不想参加?

现在,机会来了!

即日起,填写DTCC 2016会前调查问卷,就有机会赢取价值2600元的大会门票1张!仅限10张!

快来试试吧!
点击阅读原文,即刻填写问卷!
↓↓↓ 

原文作者:;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2019-07-17

  • 博文量
    1982
  • 访问量
    17885