CSLA .NET

From Rockford Lhotka's Expert C# 2005 and VB 2005 Business Objects books

Welcome to CSLA .NET Sign in | Join | Help
in Search

Getting the root object from a child

Last post 09-05-2008, 9:52 AM by dg78. 4 replies.
Sort Posts: Previous Next
  •  08-28-2008, 2:45 PM 25828

    Getting the root object from a child

    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

     

  •  08-28-2008, 4:59 PM 25833 in reply to 25828

    Re: Getting the root object from a child

    FYI, I opted to add an interface to my Csla extension classes to GetParent.
  •  09-05-2008, 8:34 AM 25960 in reply to 25833

    Re: Getting the root object from a child

    Chris,

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

    Thanks

    Dominique

     

  •  09-05-2008, 8:40 AM 25961 in reply to 25960

    Re: Getting the root object from a child

    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;

    }

     

  •  09-05-2008, 9:52 AM 25967 in reply to 25961

    Re: Getting the root object from a child

    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

     

View as RSS news feed in XML

Please contact Magenic for your .NET consulting and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Powered by Community Server, by Telligent Systems