6

I've been using .Net languages for 4 years. I develop 3 tier and 5 tier applications using WCF, ASP.NET for web applications, and C# for windows applications. Every time I start a project, business rules and validations are a concern.

Where should I place custom validation rules (button-click events, page load, or in setters/getters within my classes)?

If a project is large and there is just a field that instead of 5 characters should be 7 characters - Why should I rebuild the whole project (or business classes project)?

I think if I had a file that had my custom rules, then when a change is needed I could simply place a new rule in it. I have read some articles on the internet that offer an XML based file for this purpose, but this seems problematic because:

  • No Intellisense and errors in the XML file are very hard to find
  • We have to write custom XML parsers
  • Since this method requires numerous casts, it's very slow

My Question:

Is there a design pattern or anything else using .NET methods (Reflection, Expression Trees, Lambda Expressions, Dynamics, Runtime Creation of DLLs, etc.) to have dynamic validation using custom rules?


Edit 1)

What about Attributes? Can we use them with Reflection to Custom validations? Can we validate a property according another property(form example P1 should be P2+1) with this approach?

wonea
  • 4,783
  • 17
  • 86
  • 139
Arian
  • 12,793
  • 66
  • 176
  • 300
  • [Attribute](http://msdn.microsoft.com/en-us/library/system.attribute.aspx) provides some sort of meta data for a class, etc. You can define a custom attribute on `property1` such that it can be linked with `property2` of the same instance. But the approach is complex and leads to the hard coding to dependency rule. – Devendra D. Chavan Aug 17 '11 at 08:16

2 Answers2

5

The best way to denote the business rules is in an xml. To take full advantage of this notation, you should start with defining the structure of the rule engine's data model i.e. answer these questions.

  1. What are the rules?
  2. Can the rules be categorized?
  3. Do the rules contain common properties (attributes) like allowed values, format, etc.?

Once this is done, create a dummy rules xml and then derive an xml schema based on this xml. The xsd.exe tool can aid you in creating the schema. It is easier to create the schema if you can use tools like Altova XmlSpy.

As for answers to your specific questions,

  • We can't using Intellisense and if we have error in XML file it is very hard to find it.

Once you have the schema in place, Visual Studio provides ample support in creating the xml (including intellisense and validation).

  • We should write a custom xml parsers

Not required, the XmlSerializer Class provides logic for serialization/deserialization i.e. to convert the rules xml into the rules data model and vice versa.

  • Because this method needs numerous casting ,it's very slow

Well, this is a partly valid point when compared to hard coded rules (rules that are embedded into your assembly as classes), but the flexibility of this approach far outweighs any performance demerits. You do not need to rebuild the solution in case there a change in the rules. In most cases, the performance impact is minimal.

Unless you have a strict performance criteria, the xml approach is the preferred way to implement the rules engine. Remember that the more loosely coupled your architecture is, the higher is the flexibility at runtime but there is negative impact on performance.

Sample rule

<RulesEngine>
  <Rules>
    <Rule Id="Rule1">
      <Function>
        <Equals>
          <Property name="Property1" classId="MyClassId"/>
            <Sum>
              <Property name="Property2" classId="MyClassId"/>
              <Constant type="UInt16" value="1"/>
            </Sum>
          </Equals>
        </Function>
      </Rule>
    </Rules>
    <Classes>
    <Class name="MyNamespace.MyClass" Id="MyClassId">
      <Property name="Property1" type="UInt16"/>
      <Property name="Property2" type="UInt16"/>
    </Class>
  </Classes>
</RulesEngine>

The rules engine needs to interpret this rule and deduce the meaning accordingly.

Devendra D. Chavan
  • 8,871
  • 4
  • 31
  • 35
  • thanks,and if you want to compare XML and dynamic generation dlls and classes,what is your Idea? – Arian Aug 16 '11 at 18:28
  • consider if I want a custom Rule that according that propert1 shpuld be property2 + 1.what is the prefered way to implements this such rules with XML? – Arian Aug 16 '11 at 18:32
  • The xml rules approach does create the rules data model dynamically from the xml. But I cannot comment on the dynamic generation approach as I have not used it, but it might use [CodeDOM](http://msdn.microsoft.com/en-us/library/y2k85ax6.aspx) – Devendra D. Chavan Aug 17 '11 at 03:25
  • I have updated the answer to reflect a custom rule for `property1 == (property2 + 1)` – Devendra D. Chavan Aug 17 '11 at 03:26
  • Thanks.It is very like to Expression Tree.Can we use that for this purpose?\ – Arian Aug 17 '11 at 05:01
  • 1
    Yes, the approach is similar to [Expression trees](http://msdn.microsoft.com/en-us/library/bb397951.aspx) only the notation is different. You can use them if you are familiar with them. – Devendra D. Chavan Aug 17 '11 at 08:18
  • 1
    I do not have first hand experience with expression trees, but you can refer MSDN ([Expression trees](http://msdn.microsoft.com/en-us/library/bb397951.aspx)) or [Understanding Expression Trees](http://geekswithblogs.net/Martinez/articles/understanding-expression-trees.aspx). Better still look for questions on expression trees on [stackoverflow](http://stackoverflow.com/search?q=expression+tree). – Devendra D. Chavan Aug 17 '11 at 09:39
  • I think if I use database instead of xml tht's better because in a big web site that use sessions variable replacing an xml file cause all session being abondon – Arian Aug 17 '11 at 11:04
  • Is it not possible to retrieve the rule when it is required rather than loading it at once (SAX parser) (the answer uses the DOM approach i.e. the xml is loaded in memory)? In case, you have a rule hierarchy, it is much easier to represent it in an xml, than in a database. – Devendra D. Chavan Aug 17 '11 at 11:28
5

Take a look at FluentValidation. It uses expressions and you can create conditional validations (e.g. validate these properties if that one meets some criteria). FV is perhaps not as dynamic out of the box, but you gain Intellisense, expressiveness, and type-safety. It's generic nature means it runs reasonably fast. You can inject some of the runtime dynamics by passing in validation delegates or custom validators that can do just about whatever you can think of.

It does mean you'd have to recompile, but you could put the validators in a separate assembly. And it does make sense for the validator not to be on/in the class, because you often find that validation is performed in context. For example, a car might be valid if it has all its wheels. But, if you're trying to drive it and it has no gas battery, then it's "invalid" for driving. That said I'd locate the rules "close" to what they are validating as they are part of your domain.

If you need a rule for a property that depends on one or more properties (including itself) and a custom message if the rule's criteria isn't met, you can do this to. Consider this:

RuleFor(x => x.P1)
    .Must(x => x.P1 > x.P2)
    .Message("P1 must be one more than P2. P1 was {0}; P2 was {1}", x=>x.P1, x=>x.P2);

gives a simple comparison, but you could make something much more complex.

Kit
  • 20,354
  • 4
  • 60
  • 103
  • Thanks `FluentValidation` is very interesting validation framwork.Please see Edit 1 – Arian Aug 17 '11 at 06:59
  • 1
    Sure. FV uses imperative code (e.g. `RuleFor(...).Must(...)`) in a fluent style to essentially create declarative statements about what is valid; it can setup the dependencies just as your idea about using attributes would. I favor the former approach though because the dependency is more succinctly specified (1 line of code vs. the attributes spread throughout the type being validated). This also allows you to conditionally create the dependency. – Kit Aug 17 '11 at 14:08
  • thanks.can FV create dependent Rule between two properties?Such P1=P2+1.does it explain in it's documnet? – Arian Aug 17 '11 at 14:10
  • 3
    Sure, here's how you'd do what you want, and a custom message if the rule's criteria isn't met: `RuleFor(x => x.P1).Must(x => x.P1 == x.P2 + 1).Message("P1 must be one more than P2. P1 was {0}; P2 was {1}", x=>x.P1, x=>x.P2);` – Kit Aug 17 '11 at 14:16