zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/////////////////////////////////////////////////////////////////////////////
//    File Description    : 验证实体类
//    Copyright           : joyin
// -------------------------------------------------------------------------
//    Date Created        : Mar 26, 2010
//    Author                : jiangxinjun
//
/////////////////////////////////////////////////////////////////////////////
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace HH.WMS.Entitys.Common
{
    /// <summary>
    /// 验证实体类
    /// </summary>
    /// <typeparam name="TEntity">需要验证的实体类类型</typeparam>
    public class ValidatorEntity<TEntity>
    {
        private TEntity entity;
 
        private List<string> errorMsgs;
 
        /// <summary>
        /// 错误信息
        /// </summary>
        public List<string> ErrorMessages
        {
            get
            {
                if (errorMsgs == null)
                {
                    errorMsgs = new List<string>();
                }
                return errorMsgs;
            }
        }
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="entity"></param>
        public ValidatorEntity(TEntity entity)
        {
            this.entity = entity;
        }
 
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="predicate">错误成立条件</param>
        /// <param name="errorMsg">如果错误成立,显示的错误信息</param>
        /// <returns></returns>
        public ValidatorEntity<TEntity> Validate(Predicate<TEntity> predicate, string errorMsg)
        {
            if (predicate != null && predicate(entity))
            {
                this.ErrorMessages.Add(errorMsg);
            }
            return this;
        }
    }
}