DataPortal.Create not initializing BO on subsequent object instantiation

DataPortal.Create not initializing BO on subsequent object instantiation

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


kyuze posted on Tuesday, September 27, 2011

Hi.

I have a win. form (Form2) that is bound to a BusinessBase type object.  When Form2 is initially opened, it receives the correct values that I am sending it from another form, Form1.  When I close Form2 (form2.close()), I return to Form1 to update some values in order to send them back to Form2.  However, Form2 does not reflect the updated values.  I stepped through the code and found that on the initial open, DataPortal.Create() runs through every property in the business object and sets them correctly; AddBusinessRules() is also called at this point. But on subsequent calls to  DataPortal.Create() i.e. private MyBusinessObject _myBusObj = CrossDockSourceProduct.NewEditableRootParent(); it skips all the getters and setters, as well as AddBusinessRules().  

I am new to CSLA and probably missing something very basic. I would appreciate your help.  Thanks.

public partial class Form2 : Form

    {

        private Product _product = Product.NewEditableRootParent();

        public Form1()

        {

            InitializeComponent();

            this.form1BindingSource.DataSource = this._product;

        }

BO
    [Serializable]
    public class Product : BusinessBase<Product>
    {
 
        #region Business Methods
        public static readonly PropertyInfo<Int32> CustomerNumberProperty = RegisterProperty<Int32>(c => c.CustomerNumber, "Customer Number",Defaults.CustomerNumber);
        public Int32 CustomerNumber
        {
            get { return GetProperty(CustomerNumberProperty); }
            set { SetProperty(CustomerNumberProperty, value); }
        }
        #endregion
      
        #region Business Rules
        protected override void AddBusinessRules()
        {
            BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(CustomerNumberProperty));
        }
       #endregion
        #region Factory Methods
        public static Product NewEditableRootParent()
        {
            return DataPortal.Create<Product>();
        }
       #endregion
 
}

RockfordLhotka replied on Tuesday, September 27, 2011

I think the initialization you are seeing is of the static fields and business rules. That only occurs the first time an object of any given type is created, and the results are cached for use as the application runs.

To initialize property values of an object instance you need to implement a DataPortal_Create method. The data portal will invoke this method after creating the object, allowing you to initialize the object instance as necessary.

kyuze replied on Thursday, September 29, 2011

Thanks for your response, Rocky.

DataPortal.Create() is invoked when I call private Product _product = Product.NewEditableRootParent();.  After this initialization, I open a UI form which displays the values in the object instantiated by DataPortal.Create().  The problem is that upon closing this UI form and reopening it (this time with updated values), the form is still displaying the old values that the original object's properties were initialized with.  I would expect that that object would have been released and a new one instantiated with the statement, private Product _product = Product.NewEditableRootParent();  What am I missing?

RockfordLhotka replied on Thursday, September 29, 2011

It sounds like the issue is with your UI code or behaviors then. Perhaps when you close the first UI form, the form is somehow being reused, so it doesn't get initialized again? Perhaps you need to make sure that a new instance of the form is created when you reopen it?

kyuze replied on Thursday, September 29, 2011

That was my initial inclination, but then I stepped through the code and saw that it was stepping into:

public partial class Form2 : Form

    {

        private Product _product = Product.NewEditableRootParent();

        public Form1()

        {

            InitializeComponent();

            this.form1BindingSource.DataSource = this._product;

        }

and actually invoking DataPortal.Create() by virtue of private Product _product = Product.NewEditableRootParent();  When it invokes Data.Portal.Create() after the first time, it literally skips all my PropertyInfo type declarations (using RegisterProperty(...) as well as AddBusinessRules().  Make sense?

 

kyuze replied on Thursday, September 29, 2011

p.s. I am sending default values to RegisterProperty(...)

RockfordLhotka replied on Thursday, September 29, 2011

This is what I was saying originally. RegisterProperty runs exactly one time per AppDomain, and the results are cached. To initialize a new object, you need to override DataPortal_Create.

kyuze replied on Thursday, September 29, 2011

Thought so, wanted to be sure I understood.  Thanks very much.

Copyright (c) Marimer LLC