实体验证---测试代码
2014-12-15 14:34
190 查看
文章出处:/article/5461039.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
user u = new user() { userAge = 9, userName = "rree" };
if (u.Checked().ToList().Count>0)
{
u.Checked().ToList().ForEach(d => Console.WriteLine(d));
}
Console.Read();
}
}
public class user : EntityBase
{
public string userName { set; get; }
public int userAge { set; get; }
public IEnumerable<String> Checked()
{
return new Validator<user>(this)
.Validata(u => u.userAge > 10, "age must larger than 10")
.Validata(u => u.userName.Length > 3, "user name must larger than 3")
.ErrorList;
}
}
public abstract class EntityBase
{ }
public class Validator<T> where T : EntityBase
{
private T entity;
List<string> errorList = new List<string>();
public Validator(T tEntity)
{
entity = tEntity;
}
public List<string> ErrorList
{
get { return errorList; }
}
public Validator<T> Validata(Predicate<T> predicate, string errMsg)
{
if (!predicate(entity))
{
this.errorList.Add(errMsg);
}
return this;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
user u = new user() { userAge = 9, userName = "rree" };
if (u.Checked().ToList().Count>0)
{
u.Checked().ToList().ForEach(d => Console.WriteLine(d));
}
Console.Read();
}
}
public class user : EntityBase
{
public string userName { set; get; }
public int userAge { set; get; }
public IEnumerable<String> Checked()
{
return new Validator<user>(this)
.Validata(u => u.userAge > 10, "age must larger than 10")
.Validata(u => u.userName.Length > 3, "user name must larger than 3")
.ErrorList;
}
}
public abstract class EntityBase
{ }
public class Validator<T> where T : EntityBase
{
private T entity;
List<string> errorList = new List<string>();
public Validator(T tEntity)
{
entity = tEntity;
}
public List<string> ErrorList
{
get { return errorList; }
}
public Validator<T> Validata(Predicate<T> predicate, string errMsg)
{
if (!predicate(entity))
{
this.errorList.Add(errMsg);
}
return this;
}
}
}
相关文章推荐
- socket验证半连接的代码和测试结果
- 安全性“依赖客户端验证”的测试方法和代码实现
- 代码测试:简单用户注册信息验证
- 自己的服务器通过微信公众号Token验证测试的代码(Python版)
- 关于json字符串与实体之间的严格验证代码
- JavaScript验证手机号码代码简单代码实例(未测试)
- 工作中积累的一些验证基础知识的测试代码(以后不定期更新)
- mongodb3.0分片及java代码连接操作测试(开启用户验证)
- python gevent 项目验证测试代码
- 13个浏览器测试和代码验证的途径
- 自己的服务器通过微信公众号Token验证测试的代码(Python版)
- GIS+=地理信息+大数据——Windows部署Pandas环境及代码测试验证
- curl请求https禁止验证证书(钉钉机器人php测试代码)
- 使用 MSXML 5.0 或 .NET Framework 托管代码在 InfoPath 2003 中验证数字签名
- 单元测试-使用nmock测试你的.NET代码(1)
- 利用反射实现ASP.NET控件和数据实体之间的双向绑定,并且在客户端自动验证输入的内容是否合法
- C#验证邮件的正则表达式的代码
- 身份证18位验证及15位升18位的代码,挺简单的,初学者可以看一看....
- 用ASP动态生成JS表单验证代码
- 提供《华容道与数据结构》代码供测试