111
cjs
2025-06-06 03e67373c4c3bef21936ec1f9037f2ebcd434583
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/////////////////////////////////////////////////////////////////////////////
//    File Description    : 验证实体集合,封装了对实体的验证
//    Copyright           : joyin
// -------------------------------------------------------------------------
//    Date Created        : Mar 26, 2010
//    Author                : jiangxinjun
//
/////////////////////////////////////////////////////////////////////////////
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace HH.AMS.Entitys.Common
{
    /// <summary>
    /// 验证实体集合
    /// </summary>
    public class SimpleInvalidEntityInfo
    {
        /// <summary>
        /// 实体在集合的序号
        /// </summary>
        public int IndexInList { get; set; }
 
        /// <summary>
        /// 错误信息
        /// </summary>
        public List<string> ErrorMessages { get; set; }
    }
 
    /// <summary>
    /// 验证实体集合
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class ValidatorListEntity<TEntity>
    {
        #region InvalidEntityInfo 实体错误信息
        /// <summary>
        /// 实体错误信息
        /// </summary>
        public class InvalidEntityInfo : SimpleInvalidEntityInfo
        {
            /// <summary>
            /// 实体
            /// </summary>
            public TEntity Entity { get; set; }
        }
        #endregion
 
        #region 属性
        private IList<TEntity> list;
        private Dictionary<Predicate<TEntity>, string> predicates;
        private List<InvalidEntityInfo> invalidInfos;
 
        /// <summary>
        /// 错误信息
        /// </summary>
        public List<InvalidEntityInfo> InvalidInfos
        {
            get
            {
                if (invalidInfos == null)
                {
                    invalidInfos = new List<ValidatorListEntity<TEntity>.InvalidEntityInfo>();
                }
                return invalidInfos;
            }
        }
 
        /// <summary>
        /// 建议错误信息
        /// </summary>
        /// <remarks>InvalidInfos转换的建议信息</remarks>
        public List<SimpleInvalidEntityInfo> SimpleInvalidInfos
        {
            get
            {
                if (InvalidInfos != null)
                {
                    return InvalidInfos.ConvertAll<SimpleInvalidEntityInfo>(_ele => { return _ele; });
                }
                return null;
            }
        }
        #endregion
 
        #region 公共方法
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="list">要验证的初始化列表</param>
        public ValidatorListEntity(IList<TEntity> list)
        {
            this.list = list;
            predicates = new Dictionary<Predicate<TEntity>, string>();
        }
 
        /// <summary>
        /// 设置验证
        /// </summary>
        /// <param name="predicate">错误成立条件</param>
        /// <param name="errorMsg">如果错误成立,显示的错误信息</param>
        /// <returns></returns>
        public ValidatorListEntity<TEntity> SetValidate(Predicate<TEntity> predicate, string errorMsg)
        {
            predicates.Add(predicate, errorMsg);
            return this;
        }
 
        /// <summary>
        /// 验证集合
        /// </summary>
        /// <returns>是否成功,如果不成功,访问InvalidInfos获取错误数据</returns>
        public bool Valid()
        {
            new List<TEntity>(this.list).ForEach(_entity =>
            {
                ValidatorEntity = new ValidatorEntity<TEntity>(_entity);
                foreach (var predicate in predicates)
                {
                    ValidatorEntity.Validate(predicate.Key, predicate.Value);
                }
                if (ValidatorEntity.ErrorMessages != null && ValidatorEntity.ErrorMessages.Count > 0)
                {
                    //InvalidInfos.Add(new ValidatorListEntity<TEntity>.InvalidEntityInfo()
                    //{
                    //    IndexInList = this.list.IndexOf(_entity),
                    //    Entity = _entity,
                    //    ErrorMessages = validator.ErrorMessages
                    //});
                    SetErrorMessages(_entity, ValidatorEntity);
                }
            });
 
            return (InvalidInfos.Count == 0);
        }
 
        public ValidatorEntity<TEntity> ValidatorEntity { get; set; }
 
        public void SetErrorMessages(TEntity _entity, ValidatorEntity<TEntity> validator)
        {
            InvalidInfos.Add(new ValidatorListEntity<TEntity>.InvalidEntityInfo()
            {
                IndexInList = this.list.IndexOf(_entity),
                Entity = _entity,
                ErrorMessages = validator.ErrorMessages
            });
        }
        #endregion
    }
}