LoadProperty on derived child and business rules

LoadProperty on derived child and business rules

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

RumbleCow posted on Friday, December 17, 2010


I can't get this right.  If I use the LoadProperty  in the DataPortalXYZ on the derived object the rules are applied but not seeing property values in the derived class.  If I don't use LoadProperty method and instead set the property directly I see the property value but the business rules are not applied.  I'm guessing it has to do with the property registration but have tried several things without success.  Please help. 


Where should the property registration and business rules reside in the abstract class?  In the below



    public abstract class PurchaseOrderBase : BusinessBase<PurchaseOrderBase>


        private static int _dummy;


        // static field initializer

        // http://msdn.microsoft.com/en-us/library/aa645758(VS.71).aspx

        public PurchaseOrderBase()


            _dummy = 0;



        public static readonly PropertyInfo<string> PurchaseOrderNumberProperty = RegisterProperty<string>(p => p.PurchaseOrderNumber, "Purchase Order Number");

        public string PurchaseOrderNumber { get; protected set; }

        public static readonly PropertyInfo<int> RegionNumberProperty = RegisterProperty<int>(c => c.RegionNumber, "Region Number");

        public int RegionNumber { get; protected set; }


        protected override void AddBusinessRules()


            BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(PurchaseOrderNumberProperty));

            //BusinessRules.AddRule(new Csla.Rules.CommonRules.RegExMatch(PurchaseOrderNumberProperty, CommonRegExPatterns.POPatternCharFirst));


            BusinessRules.AddRule(new Csla.Rules.CommonRules.MinValue<int>(RegionNumberProperty, 1));

            BusinessRules.AddRule(new Csla.Rules.CommonRules.MinValue<int>(SourceNumberProperty, 1));

            BusinessRules.AddRule(new Csla.Rules.CommonRules.MinValue<int>(LocationIdProperty, 1));



RockfordLhotka replied on Friday, December 17, 2010

I discuss this in some detail in the Using CSLA 4: Creating Business Objects ebook (http://store.lhotka.net).

LoadProperty (at least by default) doesn't run business rules or raise PropertyChanged. It is just like setting a field directly (or as close as you can get with managed backing fields).

SetProperty (at least by default) runs rules and raises changed events.

If you suppress rule checking (use BypassPropertyChecks or one of the other techniques), SetProperty works exactly like LoadProperty.

Most DAL code either uses LoadProperty or suppresses rule checking. If rules are run, they are run with a BusinessRules.CheckRules call after the object has been loaded with data. This happens by default in DataPortal_Create, and requires an explicit call by you in DataPortal_Fetch.

RumbleCow replied on Friday, December 17, 2010

I understand the differences between LoadProperty and SetProperty.  I am having trouble with the property registration, business rules and abstract classes. Should I put the rules in the abstract class and register the properties there as well or should the property registration be in the derived class?

RockfordLhotka replied on Friday, December 17, 2010

In the end, properties are registered to the derived class - or at least they should be. If they aren't, then things won't work :)  The field manager ultimately creates one list of properties for each derived class, and to create that list it includes any properties registered to types higher in the inheritance hierarchy.

The AddBusinessRules method is invoked once per derived class per AppDomain. It is never explicitly called (by CSLA) on a base class. So your AddBusinessRules overload needs to call base.AddBusinessRules - which it should be doing, otherwise you won't get DataAnnotations as expected.

So your abstract class can register rules in AddBusinessRules - and they'll get attached to each derived class. In the end, the business rules are only registered to derived types, never to base types.

RumbleCow replied on Friday, December 17, 2010

Thanks  Rocky.  I'll give that a try

Copyright (c) Marimer LLC