Does/Can CSLA instantiate a parent's children objects automatically?

Does/Can CSLA instantiate a parent's children objects automatically?

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


luterguy posted on Thursday, September 21, 2006

Does/Can CSLA instantiate a parent's children objects automatically?

I have an ASP.net form using a parent object with a child (not a collection- it is one-to-one). The form creates the parent object, however the child does not get instatitated. I have access to the child properties, however it balks when I try to set them (saying that their is no object). I have tried forcing the istantiation of the children both in the UI and within the parent (DataPortal_Create), however neither have been hugely successful. Does anyone have experience, advice, information, tips for this situation?

I currently don't want to roll the child into the parent, but that is a last ditch effort option.

Essentially I hoping to be able to instantiate a child whenever the Parent is instantiated. If you look at the DataPortal_Fetch and DataPortal_Update you will see that children are updated and fetched. However, on the DataPortal_Create the children are not created.

I first looked to instantiate the children in the DataPortal_Create (like Fetch and Update), however that seemed to cause issues. I also did try to instantiate them in the UI (which I would rather not do) with similar issues. I hoped that since I had access to the Parent's child properties that I could set them (it would automatically instantiate) and that the save/update would take care of the rest. This was not tha case.

I have used parents and children in the manner they are usually described in the book and in examples: A user (through the UI) selects add (collection) to add a child to the collection. This works fine. In this case I want the child to behave just like the parent (be instantiated when it is).

As I said I did try to force instantiate the childin the Parent's DataPortal_Create and in the UI (tried seperately) however I never had the success I had hoped for.  I still am working on this to see if I can get it to work. However, I think I could use the help, experience of someone who has tried this before.

Does anyone have suggestions, input, advice?

Thanks in advance,
luterguy

Q Johnson replied on Thursday, September 21, 2006

Have you tried doing it in the Parent's constructor?

skagen00 replied on Thursday, September 21, 2006

Am I misunderstanding the question?

Isn't it just a matter of:

public class Parent : BusinessBase<Parent>

{

private Child _myChild = Child.NewChild();

public Child MyChild

{

get { return _myChild; }

}

}

 

 

JoeFallon1 replied on Thursday, September 21, 2006

I agree. That is the way I have it set up in one of my BOs.

The only slight difference is that I have decide to skip the initialization in the private member variable and use the lazy loading pattern in the getter instead.

    Get
        If mChild Is Nothing Then
          mChild = Child.NewChild ()
        End If
        Return mChild 
    End Get

It does not make any difference in this case.

But for collections which get Fetched it avoids creating them twice: once in the private variable initialization and once in the fetch.

Joe

 

 

ajj3085 replied on Friday, September 22, 2006

That's valid, but I do it in the DataPortal_XXX methods, because even internally I use factory methods.

protected override void DataPortal_Create() {
      MyChild = ChildBO.NewChild();
}

private void DataPortal_Fetch( object criteria ) {
    MyChild = ChildBO.GetChild( /* arguments which will load child */ );
}

Finally, one other point.  You should have the parent listening to its child events.  so your property setting should look like this (even if you go the route of creating the child via a ctor):

public ChildBO MyChild {
    get { return myChild; }
    private set {
           if ( myChild != value ) {
                  if (myChild != null ) {
                       myChild.PropertyChanged -= new PropertyChangedEventHandler( handler );
                  }

                  myChild = value;

                  if (myChild != null ) {
                       myChild.PropertyChanged += new PropertyChangedEventHandler( handler );
                  }
     }
}


private void handler( object sender, PropertyChangedEventArgs e ) {
    PropertyHasChanged( "MyChild" );
}

Copyright (c) Marimer LLC