Validation RuleSets How to?

Validation RuleSets How to?

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


DamReev posted on Friday, August 15, 2008

Hi,

I'm in a situation where we have a single business object which is now being used in a wizard and we need to perform per wizard page validation. It does not seem like CSLA supports the concept of RuleSets (kind of like the Validation Application Block does), but since our objects are all CSLA objects, I was wondering if there is a way to do this either natively or with custom code. The basics of what I need to do is only check a certain set of property validations at a time, and I'm trying to do it without having to corrupt my validation rules with checks in every rule against some sort of state (i.e. if State = Start then enforce no rules, if State = State1 enforce State1 rules etc.).

I know I can expose methods which call specific property validations but I'm not a fan of this method, and I'd choose to use the VAB rather than doing this, but I'd prefer to stick to one validation methodology in the application.

Any insight or help would be much appreciated.

JohnB replied on Friday, August 15, 2008

DamReev,

I just finished working on a user account wizard where I different rules that would apply based on the user account type being created. What I did was to create a delegate method that takes into account the user account type to decide which rules apply. Here is a partial example of my solution:

Protected Overrides Sub AddBusinessRules()

            ValidationRules.AddRule(AddressOf UserInformationRequired, New RuleArgs("FirstName", "First Name", RuleSeverity.Error), 0)
            ValidationRules.AddRule(AddressOf UserInformationRequired, New RuleArgs("LastName", "Last Name", RuleSeverity.Error), 0)

End Sub

Private Shared Function UserInformationRequired(ByVal target As Object, ByVal e As Csla.Validation.RuleArgs) As Boolean

            Dim pUserAccount As UserAccount = DirectCast(target, UserAccount)

            If pUserAccount._userType = UserTypes.Customer Then
                If pUserAccount._userType = UserTypes.Employee Then
                    If e.PropertyName.Equals("FirstName") Then
                        If pUserAccount.FirstName.Length = 0 Then
                            e.Description = "First Name is required."
                            Return False
                        End If
                    End If

                    If e.PropertyName.Equals("LastName") Then
                        If pUserAccount.LastName.Length = 0 Then
                            e.Description = "Last Name is required."
                            Return False
                        End If
                    End If
                End If
            End If
            Return True
End Function

Hope this helps.
John

ajj3085 replied on Friday, August 15, 2008

Hi,

Just a crazy idea off the top of my head..

Your object is designed to be used in a wizard, so maybe it should know what step it's on as well.  So maybe it could have a NextStep and PreviousStep methods.  They could just track using a number the current state. 

You could then assign priorities to match the state.. and then run only the rules at that priority or lower.  Either that or you could always run the rules in NextStep, and the rules themselves take into account the current "step" of the object.

HTH
Andy

DamReev replied on Friday, August 15, 2008

Thanks guys for the ideas. Both aren't bad. I was trying to stay away from maintaining state in the business object (this was going to be controlled by the presenter), but the idea of using priority is very intriguing and I will look into that.

Thanks again.

ajj3085 replied on Friday, August 15, 2008

Well, if it helps, you're designed the BO to fit a certain use case, and that use case is a wizard style 'workflow,' so having the BO help manage those transitions makes sense.  I know workflow isn't be best word to use for what I mean, but I can't think of a better work and I think it gets the point across.

Just my $0.02 though.. like I said, it could be a crazy idea!

Copyright (c) Marimer LLC