Managered Properties - change value in setter or business rule?

Managered Properties - change value in setter or business rule?

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


ajj3085 posted on Friday, December 05, 2008

Hi,

I have a property which is a string.  I need to call Trim if the value passes isn't null.  Is this best done in the setter, prior to calling SetValue, or should a business rule do this?

Maybe in the future Csla can do something similar to Coerce value.. done before the value's actually set in the BO but after the security checks are done.

Thoughts?  What's the best way to deal with this issue now?

Andy

Fintanv replied on Friday, December 05, 2008

FWIW I do something similar in the setter.  I use an extension method that trims and uppercases the input.

RockfordLhotka replied on Friday, December 05, 2008

I recommend creating a rule method to do this sort of thing. That way your property declarations remain untouched and standard. And really, what you are describing is a rule, so it makes sense to implement it at a rule :)

CSLA can't just trim values arbitrarily - not every property wants/needs that. I think I'm stretching it already by replacing null with "" for string values...

rsbaker0 replied on Saturday, December 06, 2008

^^^^

Yes, I ran into a similar issue before managed properties were introduced with properties whose code was generated rather than hand written. Since then, I have done such transformations in business rules that fire when these properties change.

It can be a little tricky depending on what you are doing, though, because someone can always call CheckRules manually. In the case of trimming, this is no big deal, but I've had be careful to structure such rules so that they work even when no change has really been made to underlying data. Trimming, of course, doesn't present much trouble in this case.

(CSLA 3.0 and earlier didn't know about individual dirty properties either, and a property is still considered dirty if changed back to the original value, so I'm not sure how much the newer dirty property support helps).

ajj3085 replied on Monday, December 08, 2008

Ok, I though I read you recommending that here before, but I couldn't find the post.  I'll update my implementation.

I wasn't suggesting that Csla do any sort of automatic trimming; merely that it provide a way to register a coercion callbacks, similar to validation rules, but run before validation rules. 

Wpf's DependencyProperties do something similar; you can have a callback to coerce a value, and a separate callback which validates the value. 

RockfordLhotka replied on Monday, December 08, 2008

You can use priorities to ensure that rules that mutate values run before rules the do validation.

 

Rocky

Copyright (c) Marimer LLC