Dynamic Validation Rules Based On Property Value

Dynamic Validation Rules Based On Property Value

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


comp1mp posted on Tuesday, November 15, 2011

First, I know the following question is the result of a poorly designed object model, Unfortunately, I am stuck using it. I just want to know if it is possible.

In overrding AddBusinessRules, I need the code to be dynamic based upon the value of a property in the BO instance

if (ReadProperty<TypeEnum>(TypeProperty) == TypeEnum.Value)

         ValidationRules.AddRule

This introduces a problem when creating a new instance as AddBusinessRules is called before my DataAccess Child_Create code (which has a parameter to set the property value in question). (For all I know, it may be an issue when fetching an existing object also.)

Is there anyway I can accomplish my goal of dynamically building the rules collection based upon an instances property value when creating (or fetching) a BO.

Thanks,

Matthew

StefanCop replied on Tuesday, November 15, 2011

Rules are per design per type and not per instance. So you cannot add rules to one instance but not to another.

However, your rule can be smart, such that it takes our TypeProperty as another property and later in the Execute decides on the value of this other property, if it should do the actual check.

Btw: you can also test for all metastate (i.e. IsNew, (ITrackStatus)context.Target ) to get the actual check of a rule not/only runned if IsNew or IsDeleted.

 

comp1mp replied on Wednesday, November 16, 2011

stefan cop

Rules are per design per type and not per instance. So you cannot add rules to one instance but not to another.

Actually Csla 3.8.3 does support per instance rules.  My situation is a little bit different in that it would be every instance that has a property value of X, add these business rules.

I used something similar to the following pseudo code in Child_Create data access code which has a SIngleCriteria parameter that I am interested in.

private void Child_Create(SingleCriteria value)

     if (value== x)

          ValidationRules.AddRule(1)

          ValidationRule.AddRule(2)......

 

Again, I understand that having to do this is a function of a poorly designed object model, and does not follow the prescribed pattern for setting business rules in the AddBusinessRules overrides, thus decreasing maintainability. However, my boss disagrees :).

The above appears to work, but I am concerned that some subtle bug may be introduced by going outside of the AddBusinessRules override. Has anyone done something similar and has it worked in productiion?

 

JonnyBee replied on Wednesday, November 16, 2011

Well,

You could get it to work with CSLA 3.8 but it would certainly block your migration path to CSLA 4.

You would have to stick with:

private void Child_Create(SingleCriteria value)

     if (value== x)

          ValidationRules.AddInstanceRule(1)

          ValidationRules.AddInstanceRule(2).....

so that the rules are added as instance rules.

comp1mp replied on Wednesday, November 16, 2011

Thanks! Now I will try and convince my boss as to why its evil :).

Copyright (c) Marimer LLC