I have created a rule for checking if a DateTime is valid. I want to add this as a BusinesRule for every single DateTime Property to checks if the user input entered is valid and else give a warning.
Now I would have to add in every class the businessrules for each DateTime Property manually
BusinessRules.AddRule(new DateValidRule(BornDateProperty) { Severity = RuleSeverity.Warning });
BusinessRules.AddRule(new DateValidRule(LegitimationDateProperty) { Severity = RuleSeverity.Warning });
....
This will be needed to be done manually for every single class for every single Property of type DateTime.
Is there a better more efficient way to do this?
Yes, you can use the FieldManager
property to access the list of registered properties and loop through them.
foreach (var property in FieldManager.GetRegisteredProperties().Where(r=>r.Type == typeof(DateTime)))
{
BusinessRules.AddRule(new DateValidRule(property) { Severity = RuleSeverity.Warning });
}