Composition Containers

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies.
This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

A composition container is a run-time container for services and managed objects. The Composite Web Application Block uses composition containers to create instances of, and manage lifetimes of, both shared and independent components. A composition container keeps references to the components it creates, which prevents them from being disposed and collected, even when no other object has a reference to the component.

Every Web Client Software Factory application has a single root composition container. This root container is used to create shared components. The application block also creates a composition container for every business module. The WebClientApplication class creates the root composition container when the application starts, and then it creates a composition container for each business module. The root composition container has access to the collection of module composition containers, and each module composition container can access the root composition container, as illustrated in Figure 1.

Ff709880.a21b97ed-35af-472c-9dbf-35348b6c45ea(en-us,PandP.10).png

Figure 11

Application composition container hierarchy in application state

The CompositionContainer and ServiceCollection classes both use an ObjectBuilder locator (IReadWriteLocator) and lifetime container (ILifeTimeContainer) to support the discovery and lifetime management of the objects they contain. The locator acts as a dictionary that maps instance names to objects. In the following code example, a module requests a service of type IAuthorizationRulesService. The ServiceCollection class (available as the Services property of the root composition container) uses the locator to find the requested service.

IAuthorizarionRulesService authorizationRulesService = rootContainer.Services.Get<IAuthorizarionRulesService>();

The lifetime container keeps object references in use and prevents the objects from being garbage collected until the lifetime container is disposed.