The Dependency Injection Model

This topic describes the mechanism used by Enterprise Library to create and manage the lifetime of objects within the core and the application blocks. Enterprise Library uses a dependency injection mechanism to create and manage the lifetime of all the Enterprise Library objects it creates. Depending on the style of your application, it may use the same dependency injection container that Enterprise Library uses. This topic contains the following sections:

  • Application and Container Initialization
  • Using an Alternative Dependency Injection Container
  • More Information

To learn how to use the Unity dependency injection approach when writing application code, see Using Enterprise Library in Applications.

Application and Container Initialization

When you initialize Enterprise Library, it loads all of the required type information for the blocks you are using, and for the core features of Enterprise Library, into the container. You can also use this container to register your own types and mappings if you wish. The following schematic shows a high-level overview of the configuration mechanism and the way that the container allows you to create and retrieve instances of objects that you require in your code. For information and examples on creating and retrieving objects see Creating and Referencing Enterprise Library Objects .

Ff664757.7af7096f-3e15-4cb3-9d54-f3ef1c920459(en-us,PandP.50).png

The EnterpriseLibraryCoreExtension container extension for Unity provides an easy mechanism to configure the EnterpriseLibraryContainer with Unity. It uses a UnityContainerConfigurator to load the configuration information from an Enterprise Library configuration source into the Unity dependency injection container.

The EnterpriseLibraryContainer Class

A fundamental feature of the Enterprise Library configuration system is the EnterpriseLibraryContainer class. This class is the entry point for correctly associating a container with Enterprise Library. It exposes the ConfigureContainer methods to configure any container given a container configurator and a configuration source. Enterprise Library contains default and more specialized configuration sources that you can configure for your application. For more information about configuration sources, see Advanced Configuration Scenarios. In addition, the EnterpriseLibraryContainer provides the ability to set and access the underlying container as an IServiceLocator by using the static Current property.

The Current Property and IServiceLocator Interface

Once a container is configured, it can be wrapped with a service locator and set as the current container by using the Current static property. By default, when using Unity as the container, it is wrapped in a UnityServiceLocator instance. Other configuration containers must be wrapped in an appropriate implementation of the IServiceLocator interface.

The EnterpriseLibraryContainer class exposes a static reference to the current container through its Current property as an instance of this service locator. The IServiceLocator interface is part of the Common Service Locator library, Microsoft.Practices.ServiceLocation.dll, which specifies an interface for object resolution and service location that application and framework developers can reference.

Note

If you reference the Current property, Enterprise Library will create a Unity container, configure it, wrap it with a UnityServiceLocator, and set it as the current property.

The Common Service Locator library provides an abstraction over IoC containers and service locators, and allows an application to access the capabilities of any container that supports IServiceLocator without relying on a hard reference to a specific container type. For example, it exposes overloads of the GetInstance method that applications can use to resolve an instance of a service from any suitably enabled container.

For information about the IServiceLocator interface, see CommonServiceLocator on CodePlex.

For more information and examples on creating and retrieving objects see Creating and Referencing Enterprise Library Objects.

Using an Alternative Dependency Injection Container

You can use a dependency injection mechanism other than Unity in your applications. The configuration mechanism that loads the container with the required Enterprise Library type registrations and mappings is extensible. In the case where you are using the Unity container, the configurator sets up injection policies and mappings.

To use an alternative dependency injection mechanism, you must create (or obtain) the appropriate configurator class for the container you want to use. This class translates the container-agnostic injection representation created by Enterprise Library for each block into container-specific directives.

The fundamental mechanism to set up a container is to use a container configurator and provide this to the EnterpriseLibraryContainer. After the container has been configured, it can be set as the current container by setting the static Current property with an IServiceLocator wrapper that references the container type you wish to use, see the " Initializing and Setting a Default Unity Container" section in the Creating and Referencing Enterprise Library Objects topic.

At this point the container can be used to resolve any Enterprise Library object that is set up in the configuration source. If you want to use a different dependency injection container, you create an instance of that container type and the appropriate configurator, and then use it to apply the configuration to the container.

More Information

For more information about the Unity dependency injection mechanism, see Unity Dependency Injection and Interception.

For information about configuring Enterprise Library, see Configuring Enterprise Library.

For information about initializing the container and resolving instances of objects, see Using Enterprise Library in Applications.

For information about the current release of Enterprise Library, and additional guidance such as videos, articles, and blog posts, see patterns & practices – Enterprise Library on CodePlex.

For information about new community and third-party extensions to Enterprise Library, see the Enterprise Library Contrib Web site on CodePlex.

For information about the IServiceLocator interface, and implementations of container configurators for Enterprise Library and other containers, see CommonServiceLocator on CodePlex.