CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Forum has moved

New location: CSLA .NET forum


CSLA .NET Resources:
  • CSLA .NET forum
  • CSLA .NET home page
  • AddBusinessRules override not called

    rated by 0 users
    Answered (Verified) This post has 2 verified answers | 5 Replies | 1 Follower

    Not Ranked
    4 Posts
    Dogua posted on Tue, Jul 2 2013 5:28 AM

    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

     

    Answered (Verified) Verified Answer

    Top 10 Contributor
    2,279 Posts
    Verified by Dogua

    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? 

    Jonny Bekkum, Norway CslaContrib Coordinator

    Top 10 Contributor
    2,279 Posts
    Verified by Dogua

    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:

    • rules is checked when a field value is changed (obviously not happened here)
    • or when your code call BusinessRules.CheckRules()

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

    Jonny Bekkum, Norway CslaContrib Coordinator

    All Replies

    Top 10 Contributor
    2,279 Posts
    Verified by Dogua

    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? 

    Jonny Bekkum, Norway CslaContrib Coordinator

    Not Ranked
    4 Posts
    Dogua replied on Tue, Jul 2 2013 6:43 AM

    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 ?

    Top 10 Contributor
    2,279 Posts
    Verified by Dogua

    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:

    • rules is checked when a field value is changed (obviously not happened here)
    • or when your code call BusinessRules.CheckRules()

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

    Jonny Bekkum, Norway CslaContrib Coordinator

    Top 150 Contributor
    55 Posts

    Is the AddBusinessRules override called before DataPortal_Create?

    Top 10 Contributor
    2,279 Posts
    JonnyBee replied on Thu, Sep 26 2013 10:50 PM

    Yes.

    Jonny Bekkum, Norway CslaContrib Coordinator

    Page 1 of 1 (6 items) | RSS

    Copyright (c) 2006-2014 Marimer LLC. All rights reserved.
    Email admin@lhotka.net for support.
    Powered by Community Server (Non-Commercial Edition), by Telligent Systems