CSLA: Is it possible to BusinessRule.Add() for every Property that is of a specific Type, without having to add it manually for every property?

112 views Asked by At

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?

1

There are 1 answers

0
Rockford Lhotka On BEST ANSWER

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 });
      }