Ensuring property is set...

Ensuring property is set...

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


ballistic posted on Sunday, December 23, 2007

Is there a way to ensure a property is set? 

I have set up validation which is called when a property is set.  Of course, the front-end code could alway ommit to set that property.  I know I can override my object's IsValid method and call ValidationRules.CheckRules(), however, if the property I'm checking is set, the ValidationRules.CheckRules() would call the same validation method, causing validation to occur twice on the same property.

Does CSLA keep track of which validation methods have been called so it does not check twice?

JoeFallon1 replied on Sunday, December 23, 2007

I sometimes override IsValid and call ValidationRules.CheckRules - but not for all properties - only for properties which are not set through the UI - or multiple properties. e.g. If I have 5 checkboxes on a form and have a rule that AtLeastOneIsChecked then I do not have a rule for each property, I have one rule which only runs after all 5 are set. (I have a web form so this is a postback and all values are posted at the same time so I only need to evaluate them once they are all set.)

CSLA does not keep track to see if a property is called twice. It allows you to call it as many times as you want/need to. That is how dependent properties work too. If you set a property and have a rule stating that another property should also be checked then all the rules for that property get checked right after this first one. But then if you Set the 2nd property the riules get re-run. That is "just the way it is". I like this style much better than the 1.x style though. It is a lot cleaner and simpler to understand.

Joe

 

Marjon1 replied on Monday, December 24, 2007

I use the DataPortal.Create method to ensure that default values are initialized when the object is created and then call ValidationRules.CheckRules() as the last line in this method. Then it doesn't matter who creates  your object, it will have the correct values & if you set the values directly to the variables you don't have to worry about validation rules firing twice.

Regards,

Marjon

Copyright (c) Marimer LLC