Business Objects That Support a Parent/Child Relationship in .NET

This posting describes how to provide a re-useable Parent/Child mapping for your business objects.

Natively, in .NET, your business objects are just plain classes where you include things like properties.  To add additional functionality that can be used across all your business objects to support a parent/child mapping, I’ve created the BusinessObjectBase and BusinessObjectBindingList(Of T) classes.  These classes allow us to extend common functionality and common features across your business objects.  In this posting I’ll go over the Parent/Child relationship feature that is being used by our business objects that inherit BusinessObjectBase or BusinessObjectBindingList(Of T).  The Parent/Child relationship is useful in situations where there is a need for the child object to get a reference to its parent or its siblings. 

BusinessObjectBindingList

  • This class represents a list of a particular type of business object as specified by T; it works similarly to the ComponentModel.BindingList(Of T) with the additional functionality specified below
  • The ComponentModel.BindingList object allows native data binding to grid controls such as the Infragistic WinGrid
  • Inherits ComponentModel.BindingList(Of T)
    • T has to be of type BusinessObjectBase
    • Retains and uses all functionality of the existing BindingList class
  • Has two New constructor implementations
    • Both of these constructor implementations require a Parent object to be passed in that is of type BusinessObjectBase
    • Internally keeps a reference to a Parent in order to maintain a parent/child relationship
  • The InsertItem method is overridden, so that the ParentList and Parent property of the BusinessObjectBase (see below) object are set correctly for newly added list items
    • The BusinessObjectBase object will now be able to access the parent list and also be able to access its siblings
    • It was not necessary to implement this functionality in overridden versions of Add, AddNew or other similar methods because these methods call InsertItem internally anyway; this ensures that the parent property is set for every new object

BusinessObjectBase

  • Inherited by your business object classes
  • Has Edit Mode, CancelEdit, and IsNew functionality (this will be described in detail in a future posting)
  • Has a ParentList property that references an IList
    • If this is object is an item within a BusinessObjectBindingList then this will return that list object; this property would also have access to all of its siblings through the ParentList property
  • Has a Parent property that references its parent object

Usage Scenario

With this functionality all of your business objects can benefit.  In a typical Supplier/Part business scenario you could have a list of suppliers (the list is of type BusinessObjectBindingList; and the supplier object inherits BusinessObjectBase) that belong to a specified geographical region.  The geographical region would be the parent to the supplier list. 

Now,

  • The supplier object can easily find out about the region that it belongs to, and even use some of this information if it needed to
  • The supplier object could get a count of how many other suppliers were in the same region and even reference any of the individual supplier objects that were in the list with it

Although, this is a fairly simple and there isn’t a lof ot code involved, it gave us a huge advantage in functionality that we can easily re-use across our business objects as required.

The Code

Here is the source code for the BusinessObjectBase and BusinessObjectBindingList classes.

(for simplicity, I’ve stripped out code from the BusinessObjectBase class, such as Edit/Cancel functionality, that isn’t relevant to this example) See Business Objects That Support a Parent/Child Relationship in .NET for follow up.


''' Customized implementation of BindingList collection.  List is used for objects of BusinessObjectBase type
''' and provides the objects in the list full access to the object path (Parent, ParentList)
Public Class BusinessObjectBindingList(Of T As {BusinessObjectBase})
    Inherits ComponentModel.BindingList(Of T)
    Dim _Parent As BusinessObjectBase

    Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As T)
        MyBase.InsertItem(index, item)
        item.ParentList = Me
        item.Parent = _Parent
        Me.AddNew()
    End Sub

    Public Sub New(ByVal Parent As BusinessObjectBase)
        MyBase.New()
        _Parent = Parent
    End Sub

    Public Sub New(ByVal list As System.Collections.Generic.IList(Of T), ByVal Parent As BusinessObjectBase)
        MyBase.New(list)
        _Parent = Parent
        For Each Item As T In Me
            Item.ParentList = Me
            Item.Parent = _Parent
        Next
    End Sub
End Class

''' Class that business objects in this project should inherit from
''' This class will provide functionality to allow full access to the object chain (ParentList, Parent).
''' It provides CancelEdit functionality to automatically rollback non committed changes to the business object
Public MustInherit Class BusinessObjectBase
    ''' The IList collection object that this object is part of.  If this object is not a child object in an IList object then Nothing is returned.

    Public ParentList As System.Collections.IList

    ''' The parent object.  If this is a top level object then Nothing is returned.
    Public Parent As BusinessObjectBase ' BusinessObjectBase
End Class

This article has been followed up with Business Objects That Support a Parent/Child Relationship in .NET

Advertisements

About dandouglas
Dan Douglas is based in Toronto, Ontario, Canada and is a professional independent Software Consultant and an experienced and proven subject matter expert, decision maker, and leader in the area of Software Development and Architecture. His professional experience represents over 15 years of architecting and developing highly successful large scale solutions. Dan also believes that properly empowering teams with trust and responsibility yields the greatest results. | For inquiries about Dan's software consulting practice, please see the contact page.  Dan has also built up a network of highly successful and professional Software Developers and Architects who are highly respected and can be called upon and used in conjunction with his own consulting practice to handle the largest of consulting projects.

One Response to Business Objects That Support a Parent/Child Relationship in .NET

  1. Pingback: Add Reusable (Inheritable) Commit/Rollback (Cancel Edit) Functionality To Your .NET Business Objects by Implementing IEditableObject « IT is Possible – Dan Douglas is blogging

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s