ValidationRules That Modify Property Data (Csla 3.8.3)

ValidationRules That Modify Property Data (Csla 3.8.3)

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


comp1mp posted on Monday, February 06, 2012

Hi,

I was hoping to create a common validation rule which would modify the data of a property.

If the phone number provided is only 7 digits, automatically append a default area code.

If i create the rule for each class that has a phone number, this is not an issue, as I can access LoadProperty to set the new value.

Trying to create a common rule, it appears that the only way to modify the data is by calling the property setter using reflection.

Is there a mechanism for invoking LoadProperty within a Common rule?

I noticed that CallType.Get results in a call to SetProperty internally. CallType.Let appears to do nothing at all...

 

Thanks,

Matthew

RockfordLhotka replied on Monday, February 06, 2012

I don't think there was a supported way to call LoadProperty like that in 3.8, no.

But you could create a type that inherits from ObjectFactory, and have that type expose a LoadProperty method, and then call that method from your rule. I did that at one point as a workaround for something, and it worked fine :)

fwiw, CallType.Let does nothing because that really hasn't had meaning since VB6. It exists to support the Let keyword in VB, and that keyword fundamentally changed from VB6 to VB.NET - in a way that rendered it largely useless I'm afraid... Because CSLA .NET had no use for the Let concept there was no reason to implement anything for that value.

comp1mp replied on Tuesday, February 07, 2012

Thanks for the quick reply Rocky.

My initial concern was calling property setter would result in a recursive call  to the rule. As long as the developer is mindful of this, using the property setter through reflection is an acceptable solution.

RockfordLhotka replied on Tuesday, February 07, 2012

Calling the property setter WILL result in a recursive call.

I was suggesting a workaround so you can call LoadProperty in a 3.8 rule like we support in a 4.0 rule.

Alternately you can use BypassPropertyChecks and call the setter - that'd have the same result.

comp1mp replied on Tuesday, February 07, 2012

I'm sorry, I was unclear. I realize that it results in a recursive call, and was just saying it is incumbent on the developer to be mindful of that when implementing the rule.

I think there may be some confusion about the OP. I am referring to a common validation rule. One that is not in a business object class definition where I would have access to the protected LoadProperty method (or BypassPropertyChecks).

How would I refer to BypassPropertyChecks in a common rule?

public static bool ValidatePhoneNumber<T>(T target, RuleArgs e) where T : OrionCsla.BusinessBase<T>

RockfordLhotka replied on Tuesday, February 07, 2012

Sorry, I keep forgetting what isn't in 3.8...

The same technique I described about creating a subclass of Csla.Server.ObjectFactory to expose a LoadProperty method could also be used to expose a BypassPropertyChecks method.

comp1mp replied on Tuesday, February 07, 2012

No problem:). Your help is much appreciated!

Copyright (c) Marimer LLC