CSLA .NET

From Rockford Lhotka's Expert C# 2005 and VB 2005 Business Objects books

Welcome to CSLA .NET Sign in | Join | Help
in Search

Dynamically loaded validation rules?

Last post 07-07-2008, 8:00 PM by stacy.odell. 7 replies.
Sort Posts: Previous Next
  •  05-12-2008, 12:16 PM 23518

    Dynamically loaded validation rules?

    I have a need to define validation rules in a database and have them loaded and applied at run time to my objects.

    For example, in the database I would specify the property name, the rule, and (optionally) the value -- for example, property "MyString", rule "StringMaxLength", value 10.

    Along those lines, I would also like to define dependencies between rules in the database as well (example:  if the value of this one boolean property is True, then make sure the length of this other string property is a maximum of 30)

    Has anyone done anything like this (CSLA 3.0.4)?  any examples you'd like to share?

  •  05-13-2008, 1:51 PM 23546 in reply to 23518

    Re: Dynamically loaded validation rules?

    The rule implementations need to be written in code. But assuming you have that, then associating the rules with properties can be done from metadata.

    What you need to know in the database are:

    1. Business class name
    2. Property name
    3. Name of assembly containing rule class
    4. Name of rule class
    5. Name of rule method
    6. Name/value list of parameters for rule

    Items 1 and 2 are needed to identify which rules go with which properties of course.

    Items 3-5 are necessary so you can use reflection to get at the specific rule method metadata and then create a delegate reference to that method.

    Item 6 provides the parameters for the rule method. In CSLA .NET 3.0 I introduced the idea of DecoratedRuleArgs, which you can read about in the Using CSLA .NET 3.0 ebook. The purpose is specifically to make your scenario easier. The rules in CommonRules now all use DecoratedRuleArgs, so you can pass in parameters as name/value pairs instead of having to use the strongly-typed RuleArgs subclass for each rule.


    Rocky
  •  07-02-2008, 11:31 AM 24491 in reply to 23546

    Re: Dynamically loaded validation rules?

    Excellent!  Just what I was looking for.  This will help me to dynamically define validation rules in a database and apply them at run time.

    Now for the second part of my question:  how can I dynamically define dependencies between properties in a database and apply them at run time?

  •  07-03-2008, 2:15 PM 24533 in reply to 24491

    Re: Dynamically loaded validation rules?

    Lhotka is it possible to let user wrte his own businessrule using some kind of scripting?
  •  07-07-2008, 7:04 AM 24573 in reply to 24491

    Re: Dynamically loaded validation rules?

    What kind of dependencies are you talking about?  Foreign key constraints, or something else?
  •  07-07-2008, 8:38 AM 24574 in reply to 24573

    Re: Dynamically loaded validation rules?

    ajj3085:
    What kind of dependencies are you talking about?  Foreign key constraints, or something else?

     

    Dependent properties, a la "if the value of property A changes, automatically run the validation code associated with property B"

  •  07-07-2008, 9:22 AM 24575 in reply to 24574

    Re: Dynamically loaded validation rules?

    See ValidationRules.AddDependentProperty
  •  07-07-2008, 8:00 PM 24587 in reply to 24575

    Re: Dynamically loaded validation rules?

    ajj3085:
    See ValidationRules.AddDependentProperty

     

    Doh!  (slaps forehead) I knew that.  I guess until now I just never put two and two together and realized all I need are the property names and the bi-directionalness in the database.

     

    Thanks for pointing out the obvious.  :)

View as RSS news feed in XML

Please contact Magenic for your .NET consulting and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Powered by Community Server, by Telligent Systems