Implementing business (validation) rules by calling a rules object

Implementing business (validation) rules by calling a rules object

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


gajit posted on Sunday, March 18, 2012

Hi Guys,

I am progressing quite well with the 4.2.2 developing a new Windows forms application and I need to pick some brains out there.

I have a need to implement some field validation based on RegularExpression values. The siomplest way for mwe to do this, is to store them in the database and retrieve them as needed.

In my old CSLA2 application, I did this type of thing, within my BO code:

        Dim ruleList As RulesList = Nothing

        If Not mCUSTOMERNO Is Nothing Then

            ruleList = RulesList.GetRulesList("CONTACT", mCUSTOMERNO)

            For Each rule As RulesInfo In ruleList

                Select Case rule.TYPE
                    Case Is = "StringRequired"
                        If rule.VALUE = "Y" Then
                            ValidationRules.AddInstanceRule(AddressOf Validation.CommonRules.StringRequired, rule.PROPERTYNAME)
                        End If
                    Case Is = "RegExMatch"
                        ValidationRules.AddInstanceRule(AddressOf Validation.CommonRules.RegExMatch, New CommonRules.RegExRuleArgs(rule.PROPERTYNAME, rule.VALUE))
                    Case Is = "Phone"
                        ValidationRules.AddInstanceRule(AddressOf Validation.CommonRules.RegExMatch, New CommonRules.RegExRuleArgs(rule.PROPERTYNAME, rule.VALUE))
                    Case Is = "Email"
                        ValidationRules.AddInstanceRule(AddressOf EmailIsValid, rule.PROPERTYNAME)
                End Select

            Next

        End If

    End Sub

Which worked - and quite nicely. I was able to apply custom validation, based on passed values.

My question is, is this a recommended approach? Do I lose anything by iomplementing this way - and will it ultimately port to another development platform if I go in that direction? (E.g. Silverlight, WP7).

I have yet to implement this in my CSLA4.2.2 objects, but want to be sure I'm not creating many problems for myself before I do so.

I await any advice or suggestions you may have.

Thanks,

Graham

 

JonnyBee replied on Sunday, March 18, 2012

No more instance rules in CSLA  - only type rules. 

Which means that you may register on ore more rules for each property and process these by either "metainfo in the object" or a "rule meta cache" to execute the rule or call inner rules.

gajit replied on Sunday, March 18, 2012

I realize the rules implementation has changed - hence saying I did this type of thing in CSLA2.

My focus is on whether it is reasonable to invoke data access via another business object during the creation of the business object.

I am not familiar with "metainfo in the object" or "rule meta cache". I would just like to be able to retrieve the list of rule objects from the datasource at BO create time.

Thanks,

Graham

JonnyBee replied on Sunday, March 18, 2012

 

gajit
I would just like to be able to retrieve the list of rule objects from the datasource at BO create time.

That must be done as "metadata" - properties/values in the business object. Rules is only registered once - on each type/computer.
Take f.ex a Silverlight object with a .NET ApplicationServer. Rule instances will exist on both computers - and must be registered when the first instance is created locally. 

How the rules behave must be controlled by "metadata" in the BO to make the rules perform the correct rule handling. "metadata" will typically be internal/private properties in the BO to instruct the rules on how to validate fields.

IE: Add the rules for all validation fields in AddBusinessRules.
Load "metadata" into business object for rules to check how to validate fields.

 

gajit replied on Sunday, March 18, 2012

Do you have an example?

JonnyBee replied on Tuesday, March 20, 2012

Hi Graham,

Sorry, I do not have an example for that ready.

 

gajit replied on Tuesday, March 20, 2012

OK.

I find it much more efficient to store validation rules in the database, so that's why I've done this in the past.

If you consider a new contact record for a customer, I need to, based on a number of qualifying fields, how that data is validated for data-entry. E.g. If my customer is north american, my phone format is likely (999) 999-9999. If however, he is a UK customer, the format might be different. Similarly Postal/Zip code formats would be different.

I can't for the life of me think how I can implement this without having to create a "ValidateThisField" command on each property. Which, would involve repetitive and expensive calls to the database.

I think you've answered my question here Jonny. But I'm probably going to post another question in the forums to see if anyone else has found a workable solution to this, that allows a single database call to load validation rules.

Thanks,

Graham

JonnyBee replied on Tuesday, March 20, 2012

Well, that would imply that your rule for validation of PhoneNumber would know that it should rerun the rule(s) when Country is changed. (Dependency or InputProperty).

You can do a database call in AddBusinessRules (and this has indeed been discussed on the forum) to load the type rules. Just be aware that ALL rules for the type must be loaded in that method. And if there is an Exception the builtin Exception handler in the base class will clear the TypeRules and try to reload the rules the next time an instance is created to avoid an incomplete set of rules being stuck for the type.

 

 

Copyright (c) Marimer LLC