CSLA 2.0 Rules not breaking?

CSLA 2.0 Rules not breaking?

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


KickTheSky posted on Monday, February 11, 2008

 Hi,

I am trying to set up a prototype of a CSLA project so that we can use this for an upcoming project.  Everything works really well except for one thing.  The rules never break on the business objects I am creating.  Here is an example of some rules I declared for a simple state/country class...

protected override void AddBusinessRules()
        {
            ValidationRules.AddRule(new RuleHandler(CommonRules.StringRequired), "StateCode");
            ValidationRules.AddRule(new RuleHandler(CommonRules.StringMaxLength),
              new CommonRules.MaxLengthRuleArgs("StateCode", 5));
            ValidationRules.AddRule(new RuleHandler(CommonRules.StringRequired), "Name");
            ValidationRules.AddRule(new RuleHandler(CommonRules.StringMaxLength),
              new CommonRules.MaxLengthRuleArgs("Name", 50));
            ValidationRules.AddRule(new RuleHandler(CommonRules.StringRequired), "CountryCode");
            ValidationRules.AddRule(new RuleHandler(CommonRules.StringMaxLength),
              new CommonRules.MaxLengthRuleArgs("CountryCode", 5));
        }

When NewState() is called, none of the rules are broken.  If I make an edit to one of the fields and the others are still blank, it is still valid and no rules are broken.  What am I missing?

webjedi replied on Monday, February 11, 2008

Generally the rules are only checked at time the object is created (using the ctor). Check that in your DataPortal_Create for the object you are asserting the rules like this:

ValidationRules.CheckRules();

After the object has been created you could call this as the last step of the set part of the property.  I think...haven't tried that myself.

JoeFallon1 replied on Tuesday, February 12, 2008

In addition to the reply above make sure your properties are coded correctly: (note that they are case sensitive so your Strings must match the case of the Property name.)

Public Overridable Property Statecode() As String
  Get
    Return mStatecode
  End Get

  Set(ByVal Value As String)
   
If mStatecode<> Value Then
     
mStatecode= Value
      PropertyHasChanged(
"Statecode")
   
End If
 
 End Set
End Property

Copyright (c) Marimer LLC