public class ValidationDummyEntity
{
public Int32 IntProperty { get; set; }
public string StringProperty { get; set; }
public DateTimeOffset DateTimeProperty { get; set; }
public decimal DecimalProperty { get; set; }
}
public class DummyEntityValidator<T> : AbstractValidator<ValidationDummyEntity>
{
public Dictionary<string, List<string>> GetValidationRuleNames()
{
Dictionary<string, List<string>> validationRulesNames = new Dictionary<string, List<string>>();
if (typeof(T) == typeof(System.Int32))
{
// Add Validatin Rules for Int property type
IRuleBuilderInitial<ValidationDummyEntity, Int32> ruleBuilderInt = RuleFor(q => q.IntProperty);
IEnumerable businessObjectCollection = ruleBuilderInt as IEnumerable;
MethodInfo addMethod = businessObjectCollection.GetType().GetMethods()
.Where(m => m.Name == "Add" && m.GetParameters().Count() == 1).FirstOrDefault();
foreach (object obj in businessObjectCollection as IEnumerable)
{
addMethod.Invoke(ruleBuilderInt, new object[] { obj });
}
validationRulesNames.Add("Int32", ruleBuilderInt);
return validationRulesNames;
}
}
}`
Using Fluient Library for validation :-
public IRuleBuilderInitial<T, TProperty> RuleFor<TProperty>(Expression<Func<T, TProperty>> expression);
I need to add these action in validationRulesNames Dictionary
ruleBuilderInt.NotEmpty();
ruleBuilderInt.NotNull();
...... ans so on
here ruleBuilderInt after putting dot(.) containing more than 10 methods
How can I do that using Reflection, There is certain restriction for foreach loop here ?
Aucun commentaire:
Enregistrer un commentaire