Composition and Validation Rules

Composition and Validation Rules

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


josh.kodroff posted on Monday, July 23, 2007

Hey All,

I have the following object structure:

Public Class A
    Private B as ClassB
    Private C as ClassC

    Public Property SomeRequiredField as String
    Get
       return B.someRequiredField
    End Get
    Set
       B.someRequiredField = value
    End Set

Public ClassB
    someRequiredField as String = ""

ClassB is a writeable object with validation rules.  ClassC is read-only information.  I have a valid case (I think) to use composition here: ClassB's behavior is used in multiple places in the app, but not necessarily in conjunction with ClassC.

If I bound a list of ClassB's to a DGV in a Windows Form and created a new B, my buddy Mr. ErrorProvider would point out that someRequiredField is required. 

However, I want to bind a list of ClassA's to a DGV.

Question 1: As I understand it, the only way to get ClassA to bind to a DGV is to expose the properties of Class B and Class C through Class A.  Is this correct, or is there a better way?

Question 2: How do I get ClassB's validation rules to work in a grid of ClassA's just like they would in a grid of ClassB's,?  e.g. I create a new Class A, and Mr. ErrorProvider tells me that someRequiredField is required.  (I want to do this without duplicating code if possible.)

Thanks for any help you can provide.
JK

PS  - My validation is more complex than string required, otherwise I'd just duplicate the few lines of validation for the sake of getting things done.

RockfordLhotka replied on Monday, July 23, 2007

If RuleX() is needed by both ClassA and ClassB, then you need a ClassR that contains RuleX(), so both A and B can simply collaborate with R.

That's at the design level, using a concept called normalization of behavior. You normalize RuleX() out of the place where it would have been duplicated into somewhere central.

At implementation time things are a little different, because ClassR is probably a static class (C#) or Module (VB). So your instances of ClassA and ClassB actually "collaborate" with ClassR directly, rather than creating an instance of ClassR. But that's an implementation detail and does not impact the design concept.

josh.kodroff replied on Monday, July 23, 2007

e.g.:

ClassR
    Friend Function MyRule (sender as object, e as someArgs)

ClassA
    Sub AddBusinessRules
       ValidationRules.AddRule(addressof ClassR.MyRule, "MyProperty")

Is that the idea?

RockfordLhotka replied on Tuesday, July 24, 2007

Exactly, though the MyRule() method should be Shared or in a Module.

 

Rocky

 

 

From: josh.kodroff [mailto:cslanet@lhotka.net]
Sent: Monday, July 23, 2007 9:23 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Composition and Validation Rules

 

e.g.:

ClassR
    Friend Function MyRule (sender as object, e as someArgs)

ClassA
    Sub AddBusinessRules
       ValidationRules.AddRule(addressof ClassR.MyRule, "MyProperty")

Is that the idea?


Copyright (c) Marimer LLC