The Instance Creation and Dependency Injection Mechanism

Microsoft Enterprise Library 5.0

DropDown image DropDownHover image Collapse image Expand image CollapseAll image ExpandAll image Copy image CopyHover image

Enterprise Library incorporates an easy-to-use mechanism for creating and wiring instances of objects (which may contain other dependent objects), and managing their lifetimes. This feature is the Unity container and the following design patterns are used:

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

Unity and Enterprise Library

Enterprise Library uses a dependency injection container to build up all of its objects. The dependency injection mechanism in Enterprise Library, by default, uses the Unity Dependency Injection and Interception mechanism This implements the dependency injection container mechanism that provides all the features required by Enterprise Library to support building object instances, injection of dependent object instances and services, and injection of property and method calls at creation time.


Note:
You can use any dependency injection container with Enterprise Library if you have an appropriate Enterprise Library configurator.
For more information on creating objects and using configurators see Creating and Referencing Enterprise Library Objects.


Using dependency injection provides considerable advantages. Modern architectural designs favor a decoupled approach, especially for managing crosscutting concerns, and dependency injection is a proven technique for achieving maximum decoupling and reusability for both the Enterprise Library objects and your own custom objects. The adoption of a dependency injection-driven approach in Enterprise Library provides the following benefits:

  • It enables you to take advantage of automatic injection of objects into the parameters of constructors and methods, and to set the values of properties, without requiring additional code. These objects or values, which may be Enterprise Library objects or your own custom types, are resolved through the container and you can control the actual concrete types that it returns.
  • It enables you to resolve instances of objects from the container by specifying an interface or base type. The container generates or returns an instance of the appropriate concrete type automatically.
  • It simplifies the task of adapting an application for different scenarios or environments. You can change the registrations and mappings in the container and it will generate or return the new type automatically.
  • It enables you to register your own custom types and mappings, or mappings for other objects you use, in the container. You can then resolve the appropriate types at run time based on these registrations and mappings.
  • It enables you to replace the default Unity container used by Enterprise Library with an alternative dependency injection container if required, and provided that you have a suitable configurator plug-in for that container. This may be useful if your application already uses a different dependency injection mechanism.

The Unity dependency injection container exposes a simple and intuitive interface, yet it supports extensibility so that developers can change the behavior as required, and they can even use it simply as a stand-alone container or dependency injection framework in their applications. It also enables developers to take advantage of modern development techniques, such as interception; constructor, property, and method call injection; dependency injection for referenced objects; type and interface mapping within the container; and simple configurability.

You can use Unity to do the following:

  • Create instances of configured Enterprise Library objects using a standardized common approach across all application blocks and core features. See Creating and Referencing Enterprise Library Objects.
  • Obtain references to singleton instances. See Creating and Referencing Enterprise Library Objects.
  • 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.
  • Create nested container hierarchies where appropriate.
  • Intercept calls to objects and create a chain of behaviors that access and perform management for crosscutting concerns such as logging, caching, and validation. See Interception with Unity.
  • Extend the capabilities or change the behavior of the container using container extensions. See Creating and Using Container Extensions.
  • Use configuration sources to specify behavior or create and manipulate container instances in code. See Configuring Unity.

For more information about the integration of Enterprise Library and Unity, and using Enterprise Library with other containers, see The Dependency Injection Model. For more information about the capabilities and extensibility of Unity, see Extending and Modifying Unity.


Note:
You can use third-party dependency injection frameworks, such as Castle, Windsor, or Spring.Net, instead of the default Unity container to generate all types of object instances, and to expose Enterprise Library configuration. Enterprise Library does not contain extensions for third-party dependency injection frameworks; however, the patterns & practices: Enterprise Library Contrib Web site may have extensions that members of the users' community have made available.