Export (0) Print
Expand All

The Injection Model

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.

The construction and disposal of object instances is a common process in most applications, particularly in business applications such as those built using Enterprise Library. Enterprise Library uses a custom underlying system named ObjectBuilder to inject instances of objects of the appropriate type and with preset properties into the application at run time.

Enterprise Library also includes the Unity Application Block (Unity), which also uses ObjectBuilder, to provide a lightweight, extensible dependency injection container with support for constructor, property, and method call injection. You can use Unity as a standalone dependency injection framework to generate and inject instances of objects into your own custom business objects and classes.

You can also use Unity to generate and inject instances of Enterprise Library objects into your applications, business objects, and custom classes. For example, you can inject instances of a configured CryptographyManager into your applications where you want to use the features of the Cryptography Application Block. The following sections describe the design of the Unity injection model and integration with Enterprise Library. For more information about using the Unity integration features, see Creating Objects Using the Unity Application Block. For general information about the Unity Application Block, see The Unity Application Block.

The EnterpriseLibraryCoreExtension class implements a Unity container extension that adds core policies and strategies to the container context so that the underlying ObjectBuilder mechanism can generate instances of Enterprise Library objects as required.

The EnterpriseLibraryCoreExtension constructor accepts an instance of a class that implements the IConfigurationSource interface, which allows you to create custom configuration sources or use the Enterprise Library configuration sources such as the ManageableConfigurationProvider. If you do not specify a configuration source, the EnterpriseLibraryCoreExtension extension will use the default FileConfigurationSource and read the configuration from the App.config or Web.config file.

Many of the application blocks include a Unity container extension that adds and exposes the specific configuration information for that block. The class name is made up of the application block name followed by "BlockExtension". In this release of Enterprise Library, the following application block extensions are available:

  • CachingBlockExtension
  • CryptographyBlockExtension
  • DataAccessBlockExtension
  • ExceptionHandlingBlockExtension
  • LoggingBlockExtension
  • SecurityBlockExtension

Each of the block extensions inherits from the EnterpriseLibraryBlockExtension base class, uses the functionality exposed by the base class, and adds code to perform tasks specific to the application block. The EnterpriseLibraryBlockExtension base class is itself a Unity container extension, and inherits from UnityContainerExtension.

Each of the application block extensions creates the policies required for all of the providers and the other objects specified in the configuration section for the block (in the configuration source for the core extension). For example, the LoggingBlockExtension class, which is one of the more complex application blocks, creates policies that manage the generation of formatters, filters, trace listeners, trace sources, log writers, and the TraceManager façade.

To create and apply these policies, the block extensions override the Initialize method of the underlying UnityContainerExtension class. If appropriate, they can call one of the overrides of the CreateProvidersPolicies method of the EnterpriseLibraryBlockExtension base class to create policies, or simply create the required policies using code. The CreateProvidersPolicies method is a convenient way to avoid repeated code.

The block extensions also contain routines that generate policies specific to that block, and register any required types with the Unity container. For example, the LoggingBlockExtension class resolves settings for log filters and log sources from the Unity container, and registers types with the container, so that it can support all the options available in the Logging Application Block.

Some of the block extensions override the virtual GetDefaultContainerPolicyCreator method of the EnterpriseLibraryBlockExtension base class in order to manage policy creation for polymorphic providers such as trace listeners. This is necessary if the default type mapping behavior cannot generate the appropriate type; for example, when a property of the configuration object specifies the type to create.

To resolve the correct type, the code first looks for the ContainerPolicyCreator attribute on the configuration object type. If this attribute is not found, the code uses the default type mapping behavior, which works only with simple types (the default type mapping behavior is to use the constructor with the most arguments, where the argument names match the names of properties in the configuration object with a case insensitive comparison and have compatible types).

However, if the configuration object does contain the ContainerPolicyCreator attribute, the code obtains an instance of a class that implements the IContainerPolicyCreator interface, which can build the required policies. Overriding the default policy creator is only necessary if you want to use a different matching mechanism. In this release of Enterprise Library, only the Logging Application Block extension does this, and only for trace listeners.

The block extension mechanism takes advantage of several new features in version 3.5 of the .NET Framework. The PolicyBuilder helper class translates code into policies using Linq expressions to achieve type safety, and uses the static Resolve class to deal with references.

You can use the Unity Application Block to inject instances of a class (including most Enterprise Library objects) into another class through constructor, property, or method injection. However, some of the static façades in Enterprise Library are not suitable for dependency injection.

In this release, Enterprise Library contains several new façade classes that either mirror the behavior of the corresponding (usually static) class, or provide methods that achieve equivalent results. The following table shows the façades you can use and the corresponding existing Enterprise Library façade that they replace.

Existing façade

New façade for use with Unity

ExceptionPolicy

ExceptionManager

Tracer

TraceManager

Cryptographer

CryptographyManager


The design and interface of each of the new façades depends on the implementation of the corresponding existing façade. For example, the CryptographyManager façade exposes the same interface as the Cryptographer static class; while the ExceptionManager façade exposes the same overrides of the HandleException method as the ExceptionPolicy class with the addition of the Process method that executes a specified delegate and handles any exception it throws.

However, the TraceManager façade provides a simple API that allows application to create traces using a LogWriter. This API consists of two constructors that accept a LogWriter instance, two overrides of the StartTrace method, and properties that expose the LogWriter and the instrumentation listener.


Show:
© 2014 Microsoft