The Instance Creation and Dependency Injection Mechanism

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.

The latest Enterprise Library information can be found at the Enterprise Library site.

One of the major advances in version 4.0 of Enterprise Library is the incorporation of a new and easy to use mechanism for building (or assembling) instances of objects, which may contain other dependent object instances. This feature—referred to in Enterprise Library terms as a dependency injection container and exposed through the Unity Application Block—implements common elements of the following design patterns:

  • Dependency Injection
  • Inversion of Control (IoC)
  • Service Locator
  • Service Container
  • Factory
  • Builder

ObjectBuilder and Dependency Injection in Enterprise Library

Since its inception, Enterprise Library has used the ObjectBuilder subsystem created by the Microsoft patterns & practices team to assemble and generate instances of the objects, including singleton instances, used by the Enterprise Library Application Blocks, the core system, the configuration system, and users' own applications.

ObjectBuilder implements many of the patterns listed earlier, but it is not an easy or accessible tool for most developers. It requires a significant level of knowledge and development time to fully realize the benefits it offers. Therefore, this release of Enterprise Library introduces a lightweight mechanism based on the ObjectBuilder subsystem that provides features to implement all the patterns listed in the previous section.

The new dependency injection container exposes a simple and intuitive interface, yet it supports extensibility so that developers can change the behaviour as required, and they can even use it simply as a stand-alone container or dependency injection framework in their applications. It also allows developers to take advantage of modern development techniques, such as constructor, property, and method call injection; dependency injection for referenced objects; type and interface mapping within the container; and simple configurability. In addition, it exposes an API that developers can use to interact directly with the container—especially useful when using the dependency injection container as a stand-alone dependency injection framework or when incorporating third-party dependency injection frameworks with Enterprise Library.

The Unity Application Block

The dependency injection mechanism in Enterprise Library is exposed through a new application block named The Unity Application Block. This application block implements the container mechanism, and—together with ObjectBuilder—includes all the features required to support building or assembly of object instances, injection of dependent object instances and services, and injection of property and method calls at creation time.

You can use the dependency injection container in several ways. You can use the Unity Application Block to do the following:

  • Create instances of Enterprise Library objects using a standardized common approach across all application blocks and core features.
  • Obtain references to singleton instances.
  • Apply dependency injection to instances of existing objects.
  • Create instances of your own custom objects and classes, usually where they contain information about dependencies that the framework must resolve and for which it must generate and inject other object instances or services.
  • Map interfaces to concrete types to provide abstraction for different target object types.
  • Extend the capabilities or change the behavior of the container using container extensions.
  • Use configuration sources to specify behavior or create and manipulate container instances in code.
  • Create nested container hierarchies where appropriate.
  • Use third-party dependency injection frameworks, such as Castle, Windsor, or Spring, with the Unity container to generate all types of object instances.

For more information about the capabilities and extensibility of the Unity Application Block, see Extending and Modifying the Unity Application Block.

Note

Enterprise Library does not contain extensions for third-party dependency injection frameworks. It is anticipated that these features will be part of the community activity surrounding Enterprise Library.