-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicValidationPackage.cs
171 lines (149 loc) · 5.3 KB
/
BasicValidationPackage.cs
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using FFP.CoreUtilities;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Validations
{
public abstract class BasicValidationPackage : IValidationPackage
{
public BasicValidationPackage()
{
AddValidationClasses();
}
public Dictionary<Type, List<IValidation>> ValidationRuleTypeDic { get; set; }
public List<string> GoodGroups { get; set; } = new List<string>();
public List<string> BadGroups { get; set; } = new List<string>();
public void Setup()
{
AddValidationClasses();
}
private IEnumerable<Type> lvTypes;
public IEnumerable<Type> TypesValidated
{
get
{
if (lvTypes == null)
lvTypes = MakeTypes();
return lvTypes;
}
set
{
lvTypes = value;
}
}
protected abstract IEnumerable<Type> MakeTypes();
protected virtual void AddValidationClasses()
{
if (ValidationRuleTypeDic == null)
{
ValidationRuleTypeDic = new Dictionary<Type, List<IValidation>>();
foreach (System.Type tp in TypesValidated)
{
if (tp.GetInterfaces().Contains(typeof(IValidation)) && tp.IsAbstract.IsFalse() && IsProperGroup(tp))
{
IValidation classValidations = (IValidation)System.Activator.CreateInstance(tp);
foreach (Type clsTp in classValidations.TypeFor)
{
if (ValidationRuleTypeDic.ContainsKey(clsTp).IsFalse())
ValidationRuleTypeDic.Add(clsTp, new List<IValidation>());
ValidationRuleTypeDic[clsTp].Add(classValidations);
}
}
}
}
}
protected virtual bool IsProperGroup(Type theType)
{
if (HasGroups())
{
if (IsGoodGroup(theType))
return true;
if (IsBadGroup(theType))
return false;
}
else
return true;
return false;
}
protected bool HasGroups()
{
if (BadGroups.IsEmpty() && GoodGroups.IsEmpty())
return false;
else
return true;
}
protected bool IsBadGroup(Type theType)
{
if (BadGroups.IsEmpty())
return false;
else if (Attribute.IsDefined(theType, typeof(ValidationGroupAttribute)))
{
ValidationGroupAttribute ValidationGroup = (ValidationGroupAttribute)Attribute.GetCustomAttribute(theType, typeof(ValidationGroupAttribute));
if (ValidationGroup.Groups.Contains(from itm in BadGroups
select itm.Trim().ToUpper()))
return true;
}
return false;
}
protected bool IsGoodGroup(Type theType)
{
if (GoodGroups.IsEmpty())
return false;
else if (Attribute.IsDefined(theType, typeof(ValidationGroupAttribute)))
{
ValidationGroupAttribute ValidationGroup = (ValidationGroupAttribute)Attribute.GetCustomAttribute(theType, typeof(ValidationGroupAttribute));
if (ValidationGroup.Groups.Contains(from itm in GoodGroups
select itm.Trim().ToUpper()))
return true;
}
return false;
}
public abstract string PackageName { get; }
public override string ToString()
{
return PackageName;
}
public override int GetHashCode()
{
return this.PackageName.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj is IValidationPackage)
{
if (((IValidationPackage)obj).PackageName == this.PackageName)
return true;
else
return false;
}
else
return false;
}
public IEnumerable<IRule> PackageRules(object obj)
{
return TypesRules(obj.GetType());
}
public IEnumerable<IRule> TypesRules(Type objType)
{
List<IRule> lst = new List<IRule>();
if (ValidationRuleTypeDic.ContainsKey(objType))
{
foreach (IValidation itm in ValidationRuleTypeDic[objType])
lst.AddRange(itm.ValidationRules());
return lst;
}
return lst;
}
public IEnumerable<IBrokenRule> CheckRules(object obj)
{
if (ValidationRuleTypeDic.ContainsKey(obj.GetType()))
{
BrokenValidationRules broke = new BrokenValidationRules();
foreach (IValidation clasValidations in ValidationRuleTypeDic[obj.GetType()])
broke.AddRange(clasValidations.Validate(obj));
return broke;
}
return null;
}
}
}