brokenrules in constructor

brokenrules in constructor

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


yh_ink posted on Tuesday, August 29, 2006

Hi,

i need to place a broken rule in a constructor which would set all rules which i set with that class to true when my object is initialised.

How do i set in a constructor????

i would like to know how to do this...would anyone show me an example for better understanding...

i want my class to control brokenrules and not the UI....

thanks

yh_ink replied on Wednesday, August 30, 2006

anyone please let me know about this

 

ajj3085 replied on Wednesday, August 30, 2006

I'm not sure I follow what you want to do..

If you're using Csla 2.x you usually override AddBusinessRules and that is where your rules are defined.  Is there a reason you want to add rules in the constructor?

yh_ink replied on Wednesday, August 30, 2006

What i want to do is when the object is instantiated first i want to set all the broken rules to true by default.Do we need to set the broken rules in the business property region only..

ajj3085 replied on Wednesday, August 30, 2006

Ahh ok.

Just override AddBusinessRules, this will setup the rules for your object.

Make sure your property setters follow this format:

public string MyString {
     get {
       CanReadProperty();
       return myString;
     }
     set {
        CanWriteProperty();

        value = value ?? "";
        if ( value != myString ) {
             myString = value;
             PropertyHasChanged(); // This will trigger rules
        }
    }
}

Then you have two options; in your DataPortal_Create, set the MyString value through the property and NOT the private field.  Do this for each property.

Option 2 is to set all the values via the FIELDS, and just before the end of the DataPortal_Create, call ValidationRules.CheckRules();

HTH
Andy

JoeFallon1 replied on Wednesday, August 30, 2006

I like Option 2 myself.

It is what Rocky outlines in the book too. (IIRC.)

Note: this will run all of your rules - they may not all be broken though.

Perhaps some of your Default values that you initialize the BO with are acceptable to your rules.

So the rule will run but not break. That is fine and normal. I don't see why you would want them all to actually be broken to start with - that would add a lot of complexity for no benefit.

Joe

 

Copyright (c) Marimer LLC