您的位置:知识库 » .NET技术

从丑陋到优雅,让代码越变越美续集之服务器端数据校验

作者: KenBlove  来源: 博客园  发布时间: 2009-04-02 11:31  阅读: 1907 次  推荐: 0   原文链接   [收藏]  

int类型检测子类:

 

Code10
public class vInt
v
{
    
int? intT = null;

    
public vInt(int? T)
     {
        intT 
= T;
        Status 
= true;
        Message 
= string.Empty;
    }

    
public vInt IsNotNullOrEmpty(string msg)
    {
        
if (intT == null)
         {
            Status 
=
 false;
            Message 
= msg;
        }

        
return this;
    }

    
public vInt Min (int minstring msg)
    {
        
if (!Status)
            
return this;

        
if  (intT < min)
         {
            Status 
= false;
            Message 
= msg;
        }

        
return this;
    }

    
public vInt Max (int maxstring msg)
    {
        
if (!Status)
            
return this;

        
if  (intT > max)
         {
            Status 
= false;
            Message 
= msg;
        }

        
return this;
    }
}

  当然..还有更多更多的类在战斗..不过出场机会就是每个类都有.世界是不公平的,总是一部 分人先富起来,然后更大部分的人穷下去......Sorry,牢骚了.各位大侠看明白就可以了.废话的看过就算了吧,忘了吧.

  最后,万 事俱备只欠东风了..粮草兵马都有了,就差一个大将军了,大将军可是调动各个部队的灵魂人物,没有他仗就打不胜利了..下边有请我们 的的大将,三军调度员,总设计师,联络官,发言人....(旁白:靠,小样你有完没完啊!!)

Code11
public class vHelper
{
    
int? intT = null;
    
string stringT = null;

    
public static vInt StartVerify(int? T)
     {
        
return new vInt(T);
    }

    
public static vString StartVerify(string T)
    {
        
return new vString (T);
    }
}

  vHelper类就是工厂类,它重载了 StartVerify()函数,并根据参数的类型调度不同的检测类来完成检测过程.

  怎么样?很简单吧,只要扩展得好,检测不再是难事 了吧? 把它们封装到一个dll中吧,这样,你就可以随时随地地校验了.

  (当然,没有最好只有更好,小弟在坐井观天呢,您有更好 的方法吗?欢迎您指教!另外,对于链式编程,VS2005的智能提示好像有Bug.总不显示提示,不知道VS2008有没有修复。)

 

  最后附上完整代码: 点击下载

 

0
0
标签:ASP.NET 验证

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻