Trouble passing parent to child objects

Trouble passing parent to child objects

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


JonM posted on Tuesday, April 10, 2007

I need to access parent objects for validation.  I've setup a parent property and a SetParent method on all of my classes of the approriate type.  However, when I try to do something like:

public void SetParent(ref CSortProfileDB Parent)

{

m_Parent = Parent;

foreach (CChild m_Child in this.Items)

{

m_Child.SetParent(ref this);

}

}

It doesn't work. I get a compiler error:

Error 4 Cannot pass '<this>' as a ref or out argument because it is read-only C:\Development\Source\depot\EIIConsole\USPSSortProfileEditor\CSLA Classes\cSortCases.cs 32 42 USPSSortProfileEditor

xal replied on Tuesday, April 10, 2007

There's no need to mark the parameter as "ref", an actual reference to the object will be passed anyway, not a copy if that was your concern...

Also, make sure that your m_Parent variable is marked [NonSerialized(), NotUndoable()]!! And of course, override OnDeserialized and set the parent again for all your children.

Are you by any chance using cslagen?? I ask because of the m_ prefix.. Big Smile [:D] If so, it already has an option to do this.. (although it's not strongly typed...)


Andrés

JonM replied on Tuesday, April 10, 2007

I'm not using CSLAgen, the m_ is part of our coding standards.  I noticed that CSLA already provides a Parent property in the business base class.  Whenever I try to use it, it is set to null.  Do I have to set the Parent object in the business base?

RockfordLhotka replied on Tuesday, April 10, 2007

The parent property is only set on child objects contained within a BusinessListBase collection.

However, if your parent object directly contains a child, your parent object can call _child.SetParent(Me) to set the parent reference. To do this properly, you must make this call after any time you set the value of your _child field AND after deserialization (override the OnDeserialized() method).

JonM replied on Wednesday, April 11, 2007

Rocky,

That makes sense to me.  For some reason my classes that derive from BusinessListBase or BusinessBase do not contain a SetParent method.  Do I have to create it?

Note: I'm using the c# version of CSLA.net 2.1.4.

RockfordLhotka replied on Wednesday, April 11, 2007

It is part of the IEditableBusinessObject interface – you just need to cast your child object to that interface to call the method.

 

Rocky

 

From: JonM [mailto:cslanet@lhotka.net]
Sent: Wednesday, April 11, 2007 10:17 AM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Trouble passing parent to child objects

 

Rocky,

That makes sense to me.  For some reason my classes that derive from BusinessListBase or BusinessBase do not contain a SetParent method.  Do I have to create it?

Note: I'm using the c# version of CSLA.net 2.1.4.



JonM replied on Wednesday, April 11, 2007

Okay, casting it to IEditableBusinessObject now lets me call SetParent.  Now for some reason I'm getting an 'object not set to instance of object error'.  Even though my class has been intialized.  Basically, I've got an editable root object with to child collections that I'm trying to do a .SetParent(this); on.  I've implemented the IParent interface on my root object.  Should I be calling this from the Dataportal_Fetch method?  I'm running in local mode (no dataportal).

RockfordLhotka replied on Wednesday, April 11, 2007

I'm not sure I follow, but I might.

So you have

Root
 contains Col1
   contains ChildA objects
 contains Col2
    contains ChildB objects

CSLA will automatically make sure all the ChildA and ChildB objects' parent values are set. You want to set a parent property on Col1 and Col2?

That isn't directly built into CSLA - you'll have to do that yourself by declaring your own private/protected field in your collection, and implementing your own SetParent() method.

The primary trick here is to make sure to mark the _parent field as NonSerializable and NotUndoable so it is safely ignored by both serialization and n-level undo.

JonM replied on Wednesday, April 11, 2007

Rocky,

First of all, thanks again for all of your help on this issue, I really appreciate your help and support for us amateurs. Next, you are basically correct, my model is as follows:

Editable Root

            contains Editable Child Collection1
                        contains Editable Child Objects

            contains Editable Child Collection2
                        contains Editable Child Objects
                                    contains editable Child Collection 2a
                                                contains editable child objects

            The main reason I need the parent relationship is that one of the fields in the grandchild object in child collection 2a needs to validate against the entries in child collection1. My UI design keeps me from breaking the 2 main child collections into seperate root child collections. Therefore I need to walk up to the parent object and over to the editable child collection1 to validate entries in the grand child collection 2a. So far this is the only way I can think of to accomplish this. Is there a better way?

Thanks again,

 

Jon

 

RockfordLhotka replied on Thursday, April 12, 2007

I think you are on the right track by implementing a Parent property on your collection. As I note in my previous post, that’s not too hard as long as you remember to attribute the field properly, and remember to reset the parent reference (from the editable root) after deserialization.

 

Rocky

 

 

From: JonM [mailto:cslanet@lhotka.net]
Sent: Wednesday, April 11, 2007 10:17 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] RE: Trouble passing parent to child objects

 

Rocky,

First of all, thanks again for all of your help on this issue, I really appreciate your help and support for us amateurs. Next, you are basically correct, my model is as follows:

Editable Root

            contains Editable Child Collection1
                        contains Editable Child Objects

            contains Editable Child Collection2
                        contains Editable Child Objects
               &nb sp;                    contains editable Child Collection 2a
                                                contains editable child objects

            The main reason I need the parent relationship is that one of the fields in the grandchild object in child collection 2a needs to validate against the entries in child collection1. My UI design keeps me from breaking the 2 main child collections into seperate root child collections. Therefore I need to walk up to the parent object and over to the editable child collection1 to val idate entries in the grand child collection 2a. So far this is the only way I can think of to accomplish this. Is there a better way?

Thanks again,

 

Jon

 



Copyright (c) Marimer LLC