zoukankan      html  css  js  c++  java
  • 实体验证---测试代码

    文章出处:http://www.cnblogs.com/tristanguo/archive/2009/05/15/1457197.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;
            }
        }

    }

  • 相关阅读:
    Codeforces 1190C Tokitsukaze and Duel game
    2019牛客多校第一场E ABBA 贪心 + DP
    Codeforces 1195E OpenStreetMap 单调队列套单调队列
    由 Vue 中三个常见问题引发的深度思考
    jszip打包上传下载
    Ubuntu切换登录用户和root用户
    vue2.0右键菜单
    main.js中import引入css与引入js的区别
    node和npm版本引起的安装依赖和运行项目失败问题
    reduce()之js与python
  • 原文地址:https://www.cnblogs.com/movemoon/p/4164821.html
Copyright © 2011-2022 走看看