Parent/Child Relationships

The parent-child relationship is a foundational concept of object oriented design. There are three kinds of object relationships in the UML specification:

Despite its importance, C# and VB have no keyword to represent aggregation. All C# and VB object references correspond to an association. Therefore, most applications and frameworks tend to re-implement the aggregation relationship, resulting in boilerplate code and defects. For instance, UI frameworks such as WinForms and WPF rely on a parent-child structure.

PostSharp implements the Aggregatable pattern thanks to the AggregatableAttribute aspect, together with the ChildAttribute, ReferenceAttribute and ParentAttribute custom attributes.

The Aggregatable pattern is used by other PostSharp aspects, including all threading models (ThreadAwareAttribute), DisposableAttribute and RecordableAttribute. You can also use the aspect to automatically implement a parent-child relationship in your own code.

In this chapter



Walkthrough: Annotating an Object Model for Parent-Child Relationships

This section shows how to prepare a class so that it can participate in a parent-child relationship.

Walkthrough: Enumerating Child Objects

This section describes how to enumerate the children of an object thanks to the visitor pattern.

Walkthrough: Automatically Disposing Children Objects

This section shows how to automatically implement the IDisposable interface so that children objects are disposed when the parent object is disposed.

Annotating an Object Model programmatically

This section describes how to annotate aggregatable object models programatically.

Working With Collections

This section covers advanced topics related to collections in aggregatable object models.

Using Immutable Collections

This section descirbes usage of immutable collections in aggregatable object models.