Parent is null!

Parent is null!

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


shahram posted on Friday, July 07, 2006

Hi!

When I create a new child object I eant it to read a id property from parent object

so I do like this

[RunLocal]

protected override void DataPortal_Create(Object criteria)

{

MarkAsChild();

this.ClientID = ((Client)this.parent).ClientID;

}

but the result is a exception because parent is null!

Any idea?

Regards

odie replied on Friday, July 07, 2006

The parent is not set until the child has been added to the collection, which means you can't obtain the parent reference when you first create the child. Secondly, if I am not mistaken the parent refers to the childs collection class not the parent it was created under.

eg.
Client.Childs.Child - the parent to child would be Childs not Client

If you are requiring the ClientID from client it us usually passed as a reference when you persist the data.

shahram replied on Friday, July 07, 2006

Thank you for your answer.

But I must ask about this:

"parent refers to the childs collection class not the parent it was created under"

"Client.Childs.Child - the parent to child would be Childs not Client"

I have a Root editable object called Client now this Client has a child editable list called ContactCollection which is a collection of child object Contact

now which one is correct:

1-Client is parent for boyh ContactCollection and Contact

2-Client is parent for ContactCollection and ContactCollection is parent to Contact

Because I am using the codesmith to generate CSLA code and when you create a clid collection or child object it aslks for type of parent and I always have given Client as parent for both Contact and contactcollection and the generated code has worked correct!

Regards

tetranz replied on Friday, July 07, 2006

shahram:
I have a Root editable object called Client now this Client has a child editable list called ContactCollection which is a collection of child object Contact

now which one is correct:

1-Client is parent for boyh ContactCollection and Contact

2-Client is parent for ContactCollection and ContactCollection is parent to Contact

Because I am using the codesmith to generate CSLA code and when you create a clid collection or child object it aslks for type of parent and I always have given Client as parent for both Contact and contactcollection and the generated code has worked correct!

I don't know what your CodeSmith templates are doing but if Contact is derived from BusinessBase then it will have a protected property called Parent. That is a reference to ContactCollection. You need to do something extra if you want a reference to Client. As I suggested in my previous post, I pass a reference to Client into ContactCollection, expose that Client as a property and then within Contact, I can get Client with something like.

Client client = ((ContactCollection) this.Parent).Client

Ross

tetranz replied on Friday, July 07, 2006

Hi shahram

I think the problem here is that in DataPortal_Create, the child has not yet been added to the collection so therefore its parent is null. If you need to do things that involves the parent, you need to do this after the child has been added to the collection.

I hit this problem when I have some validation rules that depend on knowing the parent. I solve it by having an AddNewItem() method on my collection something like this:

public MyChild AddNewChild()
{
    MyChild newChild = MyChild.NewMyChild();
    this.Add(newChild);

// the child now knows its parent so do stuff here
    newChild.DoStuff();

    return newChild;      // maybe useful to return reference to new child.
}

I just saw Odie's post. That's correct, the parent is the collection. I usually pass the real parent into the collection and expose it as a property so inside the child I need to do something like this to get the real parent.

MyParent myParent = ((MyCollection) this.parent).ParentObject
Its a bit messy but ...

Ross

tetranz replied on Friday, July 07, 2006

Just a reminder. If you give your collection a reference to its parent, be sure to mark it as [NotUndoable()] otherwise you'll end up with an infinite loop when you do a BeginEdit(). I lost some hair  after a few hours chasing that one Smile [:)]

Copyright (c) Marimer LLC