我为一个小项目创建了一个简单的验证过程。验证规则被创建为类属性的属性。
我有一个需要传递类类型的静态类。该方法将首先检查给定类的规则集是否已经在字典中,否则,它将使用反射来遍历每个属性。
使用这种方法会不会有什么问题?我担心在多线程环境中访问时可能会导致一些问题。
public static class Validator
{
private static Dictionary<Type, ValidationRulesCollection> _validationRules = new Dictionary<Type, ValidationRulesCollection>();
public static ValidationRulesCollection GetValidationRules(object obj)
{
return GetValidationRules(obj.GetType());
}
public static ValidationRulesCollection GetValidationRules(Type t)
{
ValidationRulesCollection rules = null;
/* Check if the centralized rules repository already contains the rules for this class type */
if (_validationRules.ContainsKey(t) == true)
{
rules = _validationRules[t];
}
else
{
/* Using reflection, get the list of properties for the class type */
PropertyInfo[] properties = t.GetProperties();
if (properties != null)
{
/* Iterate through each property info and check if it contains a validation rule */
ValidationAttribute[] attribs = null;
foreach (PropertyInfo property in properties)
{
/* For each property, check if it contains a validation rule */
attribs = (ValidationAttribute[])property.GetCustomAttributes(typeof(ValidationAttribute), true);
if (attribs != null)
{
foreach (ValidationAttribute attrib in attribs)
{
if (rules == null)
rules = new ValidationRulesCollection();
/* Add the validation rule to the collection */
rules.Add(new ValidationRule(property, attrib));
}
}
}
}
/* Add the rules collection to the centralized rules repository */
if (rules.Count > 0)
_validationRules.Add(t, rules);
else
throw new ArgumentNullException("The type " + t.ToString() + " does not have any ValidationAttributes");
}
return rules;
}
public static ValidationRulesCollection Validate(object obj)
{
/* Get the Validation Rules */
ValidationRulesCollection rules = GetValidationRules(obj);
/* Validate the rules */
foreach (ValidationRule rule in rules)
{
rule.Validate(obj);
}
return rules;
}
}