PostSharp.ILParent/Child, Visitor and Disposable
Open sandboxFocusImprove this doc

Parent/Child, Visitor and Disposable

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

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 <xref:System.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.