Business rules run in first business object Instance only

Business rules run in first business object Instance only

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


somia posted on Sunday, February 19, 2012

We are working with CSLA 4.2 and we have an issue with the business rules.we set business Rules (for example default value for Date property)

EXP:

protected override void AddBusinessRules()
        {
            base.AddBusinessRules();

             Date = DateTime.Now;

        }

 

when we create new business object like:

 BL.DailyFlight df = BL.DailyFlight.New();

 the date property set correctly in first time (DateTime.Now) but once create another instance from same Business object the date property not set to (DateTime.Now).

 

how we can resolve this issue?

 


JonnyBee replied on Sunday, February 19, 2012

Hi,

Do NOT set default values in AddBusinessRules. This method should ONLY be used to define the business rules for the type.

Business Rules is defined per type so AddBusinessRules is only called the first time an object of this type is created. (Hence you only get the value in the first object). IE: AddBusinessRules is only for registering rules for a type - not for execution of rules.

You should set default values in DataPortal_Create or the object factory Create method, depending on your choice of Data Access.

Default values will also be set the first time your code get a value from an "non-initialized" field or when a rule is called on that field.

BusinessRules will run when you call BusinessRules.CheckRules or (after the object is databound) -  <bo>.PropertyHasChanged or <bo>.CheckObjectRules.

somia replied on Monday, February 20, 2012

Many thanks Jonny

Copyright (c) Marimer LLC