Getting the root object from a child

Getting the root object from a child

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


skagen00 posted on Thursday, August 28, 2008

If I have a child business object, be it a businesslistbase or businessbase, what is the most efficient way to get the ultimate root object?

I don't normally like the idea of looking up the object graph, but this is an instance where I want to.

Leveraging the parent property seems appealing to me, but since parent is protected, this sort of logic would be difficult when dealing with grandchildren or even children in a child collection, etc. Thought about providing an interface for my business objects that might provide public access for parent, but I'd rather not add it if I don't have to -- reflection seems like it might be a possible route too but I'm not sold on that either.

Hoping I'm missing an obvious solution to getting the root object from a child object somewhere in the root's object graph.

Chris

 

skagen00 replied on Thursday, August 28, 2008

FYI, I opted to add an interface to my Csla extension classes to GetParent.

dg78 replied on Friday, September 05, 2008

Chris,

Is it possible you post some code about this interface and its implementation ?

Thanks

Dominique

 

skagen00 replied on Friday, September 05, 2008

The interface:

/// <summary>

/// IChild provides for a means to get a Csla object's parent. Parent is a protected property,

/// so this interface simply provides for a public means to get it.

/// </summary>

public interface IChild

{

/// <summary>

/// Gets the parent object of this object.

/// </summary>

/// <returns>This object's parent.</returns>

object GetParent();

}

My business base AND my business list base (inheriting from CSLA objects) implement IChild and provide for GetRootObject.

#region IChild Implementation

/// <summary>

/// Gets the parent object of this object.

/// </summary>

/// <returns>This object's parent.</returns>

object IChild.GetParent()

{

return this.Parent;

}

#endregion

 

/// <summary>

/// Gets the current object's root object.

/// </summary>

/// <returns>The current object's root object.</returns>

public object GetRootObject()

{

object index = this;

while (index is IChild && ((IChild)index).GetParent() != null)

{

index = ((IChild)index).GetParent();

}

return index;

}

 

dg78 replied on Friday, September 05, 2008

Many thanks for your code and your very quick answer.

Here your code in VB, I translated it wth SharpDevelop.
If it can help VB people.

''' <summary>

''' IChild provides for a means to get a Csla object's parent. Parent is a protected property,

''' so this interface simply provides for a public means to get it.

''' </summary>

Public Interface IChild

       ''' <summary>

       ''' Gets the parent object of this object.

       ''' </summary>

       ''' <returns>This object's parent.</returns>

       Function GetParent() As Object Implements INotifiedView.Initialize

End Interface

 

'''My business base AND my business list base (inheriting from CSLA objects) implement IChild and provide for GetRootObject.

 

#Region "IChild Implementation"

''' <summary>

''' Gets the parent object of this object.

''' </summary>

''' <returns>This object's parent.</returns>

Private Function IChild_GetParent() As Object Implements IChild.GetParent

       Return Me.Parent

End Function

#End Region

 

''' <summary>

''' Gets the current object's root object.

''' </summary>

''' <returns>The current object's root object.</returns>

Public Function GetRootObject() As Object

       Dim index As Object = Me

       While TypeOf index Is IChild AndAlso DirectCast(index, IChild).GetParent() IsNot Nothing

             index = DirectCast(index, IChild).GetParent()

       End While

       Return index

End Function

 

Copyright (c) Marimer LLC