The parent-child relationship is a foundational concept of object oriented design. There are three kinds of object relationships in the UML specification:
Aggregation is the parent-child (also named whole-part) relationship. It is implemented in PostSharp by the AggregatableAttribute aspect described in Annotating an Object Model for Parent/Child Relationships (Aggregatable).
Composition is an aggregation relationship where the parent controls the lifetime their children. It is implemented in PostSharp by the DisposableAttribute aspect pattern, which relies on the AggregatableAttribute aspect. For details, see Automatically Disposing Children Objects (Disposable).
Association is a simple reference between two objects.
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
Section | Description |
---|---|
Annotating an Object Model for Parent/Child Relationships (Aggregatable) | This section shows how to prepare a class so that it can participate in a parent-child relationship. |
Enumerating Child Objects (Visitor) | This section describes how to enumerate the children of an object thanks to the visitor pattern. |
Automatically Disposing Children Objects (Disposable) | This section shows how to automatically implement the IDisposable interface so that children objects are disposed when the parent object is disposed. |
Working With Child Collections | This section covers advanced topics related to collections in aggregatable object models. |