Mutable and Immutable Representations
CCI Metadata has two related representations:
- The CCI Metadata model is expressed in the form of interfaces, such as IAssembly,
ITypeDefinition and IMethodDefinition, which are implemented by a variety of concrete classes that serve different purposes. For example, the PE Reader object uses concrete classes that are thin wrappers for metadata table rows and signature blobs.
- The Mutable Metadata model is the underlying collection of concrete classes that, for the most part, passively implement the CCI Metadata interfaces in a straightforward way.
The CCI Metadata model is an immutable data model. The CCI Metadata interfaces are read-only, so you cannot change the underlying concrete objects through the interfaces. Immutability is useful for a variety of purposes, including:
- When multiple threads might be traversing the object model concurrently. Because the threads interact with the model through the CCI Metadata interfaces, they cannot change the concrete objects and do not have to synchronize with each other to see a consistent
- When an application consists of components that were written at different times by different people without knowledge of each other. Since no component can mutate the model, the components cannot interfere with each other.
- When different components do not fully trust each other.
There is thus an implicit requirement that any object that implements the CCI Metadata interfaces must not change in any observable way. Components that construct concrete objects for a model must be careful to ensure that the model is complete and consistent
before exposing outside the domain of the construction code. Construction is, after all, a form of mutation and any code that uses the object model must not be subject to mutations.
In some circumstance, the immutability requirement applies even to the construction code. For example, if the construction code traverses the object model while it is being constructed, it might encounter mutation in progress and discover that many standard
traversal algorithms and helper methods, such as those in MetadataHelper.dll, do not work.