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
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; }
}
}
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
Copyright (c) Marimer LLC