CSLA .NET

From Rockford Lhotka's Expert C# 2005 and VB 2005 Business Objects books

Welcome to CSLA .NET Sign in | Join | Help
in Search

Sharing Validation Rules

Last post 05-14-2008, 9:53 AM by smiley riley. 9 replies.
Sort Posts: Previous Next
  •  05-12-2008, 2:11 PM 23523

    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,
    John
  •  05-12-2008, 2:23 PM 23525 in reply to 23523

    RE: Sharing Validation Rules

    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,


  •  05-12-2008, 2:32 PM 23526 in reply to 23525

    Re: 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
  •  05-12-2008, 2:57 PM 23527 in reply to 23526

    RE: RE: Sharing Validation Rules

    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


  •  05-12-2008, 2:59 PM 23528 in reply to 23527

    Re: RE: RE: Sharing Validation Rules

    Thanks again.
  •  05-12-2008, 3:50 PM 23529 in reply to 23528

    Re: 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

     

  •  05-12-2008, 3:53 PM 23530 in reply to 23529

    Re: RE: RE: Sharing Validation Rules

    Thanks Joe,

    I believe the parameter is BindingFlags.IgnoreCase.

    John
  •  05-12-2008, 3:55 PM 23531 in reply to 23529

    RE: RE: RE: Sharing Validation Rules

    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

     



  •  05-13-2008, 2:02 PM 23548 in reply to 23530

    Re: RE: RE: Sharing Validation Rules

    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?


    Rocky
  •  05-14-2008, 9:53 AM 23571 in reply to 23523

    Re: Sharing Validation Rules

    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.

     

View as RSS news feed in XML

Please contact Magenic for your .NET consulting and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Powered by Community Server, by Telligent Systems