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

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

Last post 10-24-2008, 6:25 PM by jgamba. 3 replies.
Sort Posts: Previous Next
  •  08-29-2008, 1:57 PM 25852

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

    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

  •  09-01-2008, 9:45 AM 25887 in reply to 25852

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

    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
  •  09-02-2008, 9:58 PM 25901 in reply to 25852

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

    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.
    Rocky
  •  10-24-2008, 6:25 PM 27624 in reply to 25901

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

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