Add Instance Rule issue

Add Instance Rule issue

Old forum URL: forums.lhotka.net/forums/t/6178.aspx


rob.polak posted on Friday, January 16, 2009

So me and a co-worker are working on a project where we use very dynamic validation.  The problem is that the context of the validation is only determined after the object is loaded.

Since we do not know the context of the validation when we create the object we cannot use the overloaded AddInstanceBusinessRules method (since the object is not loaded at the time of validation).

So the way we approached this problem was to load the validation after we load the object data.  However, the problem with this is that the Rules never seem to apply for the object.  After stepping through the source it seems attributed to the following code:

    private ValidationRulesManager RulesToCheck
    {
      get
      {
        if (_rulesToCheck == null )


It appears that once the rules are loaded they are never loaded again.  So my question is there a way to reload the rules after I add one (not in one of the two provided methods)?  Or do I need to modify the source to always get the current rules to check?

stacy.odell replied on Friday, January 16, 2009

He's talking about version 3.0.4, by the way.

We are loading unique and dynamic validation methods from a database, per object instance -- and we obviously need to know certain things about the instance (like the ID for example) when we go to look up the validators for that item in the database and apply them as instance rules.  One instance of an object can have completely and wildly different validation rules applied to it than another.

AddBusinessInstanceRules fires before we know what the instance's ID property is, so we cannot look up the rules in the database at that point.  The rest is as Rob states above

-- Stacy (the coworker)

ajj3085 replied on Friday, January 16, 2009

What if you tackle this by going another route... all rules always apply (that is, are registered with AddValidationRule), but the rule itself is smart enough to know it should not really do anything or that it should do something.

For this to work, the instance (or possibly class) may need to load all the data affecting the rules.   For example, the instance would load it's values, and then you have another class that specifies which values cause the rule to apply.  The rule then uses your instance and this helper class to figure out if it should run the rule or return true.

Does that make sense?

Fintanv replied on Friday, January 16, 2009

When the id changes can you clear and re-load the instance rules?
The following thread provides a mechanism to clear the objects loaded instance rules without modifying the CSLA code directly.  When the new rules are loaded simply run a CheckRules().

http://forums.lhotka.net/forums/thread/25819.aspx

Copyright (c) Marimer LLC