Sharing Validation Rules

Sharing Validation Rules

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


JohnB posted on Monday, May 12, 2008

Some time ago I thought that someone posted about this topic but I have been unable to find the article I'm thinking about. Basically I would like to know if it is possible and/or advisable to share validation rules across objects.

For example, let's assume that I have several objects that have a 'LastName' property and I would like to make sure that 1) Last name is always required and 2) The length of last name can not exceed say 20 characters.

I can easily do this in one object and also copy and paste to any other objects that use LastName but I would like to create maybe one global place to store these 'common' validations.

Any ideas?

Thanks,
John

sergeyb replied on Monday, May 12, 2008

You certainly can create your own Common Rules module/class.  You can even take CommonRules.StringRequired as a basis for your own rule “LastNameRule”.  I personally think that this is a good idea.  The only thing I would suggest is not to make these rules any BO specific, but create them generically just like Common Rules.  This way you will not couple unrelated objects.

 

Ex:

 

Public Function LastNameRule( _

      ByVal target As Object, ByVal e As RuleArgs) As Boolean

 

      Dim value As String = _

        CStr(CallByName(target, e.PropertyName, CallType.Get))

      If Len(value) = 0 Then

        e.Description = _

          String.Format(My.Resources.StringRequiredRule, RuleArgs.GetPropertyName(e))

        Return False

 

      Else

                If value.Lenght>20

                                e. Description = My.Resource.LastNameRuleTooLong ‘ or some other message

                                Return False

                Else

                                Return True

                End If

       End If

 

    End Function

 

Sergey Barskiy

Senior Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: JohnB [mailto:cslanet@lhotka.net]
Sent: Monday, May 12, 2008 3:13 PM
To: Sergey Barskiy
Subject: [CSLA .NET] Sharing Validation Rules

 

Some time ago I thought that someone posted about this topic but I have been unable to find the article I'm thinking about. Basically I would like to know if it is possible and/or advisable to share validation rules across objects.

For example, let's assume that I have several objects that have a 'LastName' property and I would like to make sure that 1) Last name is always required and 2) The length of last name can not exceed say 20 characters.

I can easily do this in one object and also copy and paste to any other objects that use LastName but I would like to create maybe one global place to store these 'common' validations.

Any ideas?

Thanks,


JohnB replied on Monday, May 12, 2008

Thanks Sergey. I do have another question though. I am using VB but I am always hesitant to use "old" VB methods like CallByName. Is there any particular reason you chose this method instead of using reflection?

Thanks,
John

sergeyb replied on Monday, May 12, 2008

Not really.  I just copied code from CSLA version I am using.  Feel free to replace them with reflection calls.  I think you were thinking of target.GetType.GetProperty(e.PropertyName).GetValue(target, Nothing)…   Sounds good to me.

 

Sergey Barskiy

Senior Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: JohnB [mailto:cslanet@lhotka.net]
Sent: Monday, May 12, 2008 3:33 PM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: Sharing Validation Rules

 

Thanks Sergey. I do have another question though. I am using VB but I am always hesitant to use "old" VB methods like CallByName. Is there any particular reason you chose this method instead of using reflection?

Thanks,
John


JohnB replied on Monday, May 12, 2008

Thanks again.

JoeFallon1 replied on Monday, May 12, 2008

One nice thing about CallByName is that it is case insensitive so if you make a casing mistake in one of your strings (Lastname vs LastName) then CallByName still works. Reflection is case sensitive. Although there is probably a parameter somewhere that will make it case insentive - I just haven't looked.

Joe

 

JohnB replied on Monday, May 12, 2008

Thanks Joe,

I believe the parameter is BindingFlags.IgnoreCase.

John

RockfordLhotka replied on Tuesday, May 13, 2008

I use CallByName because I don't like to reinvent the wheel. Though I was forced to reinvent the wheel in the C# code, because I had to replicate parts of CallByName.

Another way to look at it is that I recommend abstracting the use of reflection. Not that I'm perfect in this regard, but I'm slowly consolidating all use of reflection into classes in Csla.Reflection. Normal code simply shouldn't contain advanced concepts like reflection - which means you need some layer of abstraction.

So if you don't use Microsoft's well-tested CallByName, I recommend writing your own equivalent. At which point (if you are a VB user) you've got to ask yourself why would you reinvent the wheel?

sergeyb replied on Monday, May 12, 2008

You are correct, there is.  If you use binding flags parameter for GetType.GetProperty, one of the binding flags is case-insensitive flag.

 

 

Sergey Barskiy

Senior Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: JoeFallon1 [mailto:cslanet@lhotka.net]
Sent: Monday, May 12, 2008 4:51 PM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: RE: Sharing Validation Rules

 

One nice thing about CallByName is that it is case insensitive so if you make a casing mistake in one of your strings (Lastname vs LastName) then CallByName still works. Reflection is case sensitive. Although there is probably a parameter somewhere that will make it case insentive - I just haven't looked.

Joe

 



smiley riley replied on Wednesday, May 14, 2008

I cannot talk from a CSLA standpoint but I have always found a good way to do this is to create a validation library. This way other projects can also share common validation rules (telephone numbers e-mail names, salutations etc.

To do this create a Strategy of Validation objects.

http://en.wikipedia.org/wiki/Strategy_pattern

From this you can tag on a composite pattern that allows you to in essence reuse existing concrete strategies, and assign the concrete strategies at runtime propably by use of a factory. This requires no use of reflection although you may use reflection to load an assembly at runtime if you needed to add a validation strategy without re-compiling. Just a 'little' extra code in the Factory.

 

jgamba replied on Saturday, October 25, 2008

Hello, This last idea I find very interesting, please could provide some code?

I'm also involved in another similar post

http://forums.lhotka.net/forums/thread/25852.aspx

Jorge

rsbaker0 replied on Sunday, October 26, 2008

Yes, I think shared validation rules that have a simple basis like this (required or not, maximum length) are a great idea.

Our CSLA implementation interrogates the database schema to do just this. Non-nullable fields are dynamically flagged as required, and we use the field size for character-type fields to generate maximum length rules for string fields.

Copyright (c) Marimer LLC