AddBusinessRules override not called

AddBusinessRules override not called

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


Dogua posted on Tuesday, July 02, 2013

Hi,

I use CSLA.Net 4, and I've a problem with a classe.

In m'y project, I've a lot of classe that uses the BusinessRules system correctly. But in a new classe, the AddBusinessRules override was not called when the DataPortal creates a new child()...

Here is my code :

 

[Serializable]

    public class Application : BusinessBase<Application>
    {
        #region Fields

        private static readonly PropertyInfo<Guid> m_IdProperty = RegisterProperty<Guid>(c => c.Id);
        private static readonly PropertyInfo<string> m_NameProperty = RegisterProperty<string>(c => c.Name);

        #endregion

        #region Constructor

        private Application()
        {
            // Private for Factory methods...
        }

        #endregion

        #region Properties

        public Guid Id
        {
            get { return GetProperty(m_IdProperty); }
            set { SetProperty(m_IdProperty, value); }
        }

        public string Name
        {
            get { return GetProperty(m_NameProperty); }
            set { SetProperty(m_NameProperty, value); }
        }

        #endregion

        #region Factory Methods

        public static Application NewApplicationChild()
        {
            return DataPortal.CreateChild<Application>();
        }

        public static Application GetApplication(Guid id)
        {
            return DataPortal.Fetch<Application>(id);
        }

        internal static Application GetApplication(DAL.Application app)
        {
            return DataPortal.FetchChild<Application>(app);
        }

        #endregion

        #region Data Access

        protected override void Child_Create()
        {
            SetProperty(m_IdProperty, Guid.NewGuid());
        }

        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            base.AddBusinessRules();
            BusinessRules.AddRule(new Required(m_NameProperty));
        }

        protected override void OnValidationComplete()
        {
            base.OnValidationComplete();
            OnPropertyChanged(@"IsValid");
            OnPropertyChanged(@"IsSelfValid");
        }

        #endregion

 

JonnyBee replied on Tuesday, July 02, 2013

Hi,

In Csla 4 there is only Type rules - IE: The AddBusinessRules method is only called ONCE per BO type.

This handled/done in the base class constructor. So is it likely that the reason is AddBusinessRules has already been called? 

Dogua replied on Tuesday, July 02, 2013

Thanks for reply,

But unfortunately, in this case, the AddBusinessRules isn't called in constructor...

I placed a breakpoint on the method but it's never called and I don't understand why

EDIT : 

Ok, now I've understand your answer. So, it's correct, Add Business Rules has been called ONCE.

But why save() works whitout any ValidationException despite the empty Name property ?

JonnyBee replied on Tuesday, July 02, 2013

Your code is responsible for calling BusinessRules.CheckRules when a new object is created. 

the base class DataPortal:Create is like this: 

    [RunLocal]
    protected virtual void DataPortal_Create()
    {
      BusinessRules.CheckRules();
    }

    protected virtual void Child_Create()     {       BusinessRules.CheckRules();     }

So the most likely reason is that your code does not call base.Child_Create() in your Child_Create method.

The other key is that:

'Save method does NOT call CheckRules - rather asks for the state of the object and that is obviously not broken.

lazaroms replied on Thursday, September 26, 2013

Is the AddBusinessRules override called before DataPortal_Create?

JonnyBee replied on Thursday, September 26, 2013

Yes.

Copyright (c) Marimer LLC