[CSLA.NET 3.5] Adding the validation rules in a separate module?

[CSLA.NET 3.5] Adding the validation rules in a separate module?

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


stefan posted on Friday, August 29, 2008

I have a module that is meant to keep all the code that is specific to contact related data in one place.
The contact data is stored in a contact table. You can think of it as the Assignment module in PTracker.Library.
What I also want it to contain, is the definition of contact specific validation rules, specifically the rules concerning the db-column definition (-> StringMaxLength...). So in case the table definition changes, I only have to look at one place.
Here is how I am  thinking it could work:
First the module:

Friend Module Contacts
  Public Interface IContacts
   ReadOnly Property FirstNameProperty() As Csla.Core.IPropertyInfo
   ...
 End Interface

#Region " Validation Rules "
  Public Sub RegisterBusinessRules(Of T As {Csla.BusinessBase(Of T), IContacts}) _
               (ByVal rules As Csla.Validation.ValidationRules, _
                ByVal bo As IContacts)
   '
   ' strFirstNameContact
   '
   rules.AddRule(Of T)(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs(bo.FirstNameProperty, 30))

 End Sub
#EndRegion
End Module

Now my business object that has contact data:

Public Class Customer
 Inherits BusinessBase(Of Customer)
Implements IContacts

 Private Shared FirstNameProperty As PropertyInfo(Of String) = RegisterProperty(New PropertyInfo(Of String)("FirstName", "First Name"))
  ...
  Friend Property _FirstNameProperty As Csla.Core.IPropertyInfo Implements IContacts.FirstNameProperty
     Return FirstNameProperty
  End Property

#Region " Validation Rules "
  Protected Overrides Sub AddBusinessRules()
     Contacts.RegisterBusinessRules(Of Customer)(ValidationRules, Me)
     ...
 End Sub
#End Region
...
End Class

Any problems that I may encounter on my route?

Stefan

stefan replied on Monday, September 01, 2008

No comments on this one?
I know that the rules I am referring to are easily generated,
but even then, in the case of the contact data, I only would have to regenerate one file!

But there could be pitfalls regarding csla's caching...?

Stefan

RockfordLhotka replied on Tuesday, September 02, 2008

The idea of calling a central method to register rules that may be common to several similar objects is a fine idea, there should be no problem doing that.

jgamba replied on Friday, October 24, 2008

Hello

If the Customer class is in another class library project (Project2), What would be the most appropriate method to use RegisterBusinessRules in the base class? doing public the readonly IPropertyInfo properties of IContacts Interface for other projects? creating a Contact base class with protected IPropertyInfo properties and inheriting the Customer class? another option? what is more behavior-centric?

Furthermore, if in this new project that contains the Customer class, I add a second class SalesPerson which also implements the IContacts interface and also called Contacts.RegisterBusinessRules, but adds behavior by calling a function similar to RegisterBusinessRules in a new module in second project, to add a rule StringRequired to the same property FirstName, how implement this?

Thanks in advance

Copyright (c) Marimer LLC