This section shows how you can use the methods listed in the previous table to get instances of Enterprise Library application block objects. The first stage is to create a Unity container and add the Enterprise Library core configuration extension to it, as shown in the following code.
IUnityContainer uContainer = new UnityContainer();
uContainer.AddNewExtension<EnterpriseLibraryCoreExtension>();
Dim uContainer As IUnityContainer = New UnityContainer()
uContainer.AddNewExtension(Of EnterpriseLibraryCoreExtension)()
By default, this extension will read its configuration from the application configuration file (App.config or Web.config). If you want to provide a custom configuration source, you create this first and pass it to the EnterpriseLibraryCoreExtension as shown in the following code.
IUnityContainer uContainer = new UnityContainer();
IConfigurationSource configSource = new MyCustomConfigSource();
// ... populate configuration source as required here
uContainer.AddExtension(new EnterpriseLibraryCoreExtension(configSource));
Dim uContainer As IUnityContainer = New UnityContainer()
Dim configSource As IConfigurationSource = New MyCustomConfigSource()
' ... populate configuration source as required here
uContainer.AddExtension(Of New EnterpriseLibraryCoreExtension(configSource))
Note: |
| You must add the EnterpriseLibraryCoreExtension to the container before you add any other Enterprise Library extension (you can add other extensions that are not part of Enterprise Library at any stage). If you have an existing container that has Enterprise Library extensions, you can remove them using the RemoveAllExtensions method and then add them back in the correct order. |
The next step is to add to the container the specific extension for the application block you want to use. You can load more than one application block extension if required. The name of the extension you add for each block is made up of the Application Block name followed by "BlockExtension" – such as DataAccessBlockExtension, LoggingBlockExtension, and ExceptionHandlingBlockExtension. The following code shows how to load the configuration extension for the Caching Application Block.
uContainer.AddNewExtension<CachingBlockExtension>();
uContainer.AddNewExtension(Of CachingBlockExtension)()
Note: |
| When you use an application block that has a dependency on other application blocks, you must add the appropriate application block extensions as well as the extension for the application block you actually want to use. For example, to use the Caching Application Block to store cached data in a database, you must add both the CachingBlockExtension and the DataAccessBlockExtension; and, if you want to encrypt cached data, you must also add the CryptographyBlockExtension. The application block extensions available in this release of Enterprise Library are DataAccessBlockExtension, CachingBlockExtension, SecurityBlockExtension, CryptographyBlockExtension, ExceptionHandlingBlockExtension, LoggingBlockExtension and Policy Injection Application Block. |
You can now retrieve instances of providers or other objects defined in the standard configuration of Enterprise Library using the Resolve method of the container. For example, the following code shows how you can retrieve a CacheManager instance for use in your applications.
// Get a reference to the default CacheManager as defined in configuration
ICacheManager cacheMgr = uContainer.Resolve<ICacheManager>();
// Get a reference to a specific named CacheManager defined in configuration
ICacheManager namedCacheMgr = uContainer.Resolve<ICacheManager>("MyCache");
' Get a reference to the default CacheManager as defined in configuration
Dim cacheMgr As ICacheManager = uContainer.Resolve(Of ICacheManager)()
' Get a reference to a specific named CacheManager defined in configuration
Dim namedCacheMgr As ICacheManager = uContainer.Resolve(Of ICacheManager)("MyCache")
Note: |
| The Enterprise Library configuration generated by the container extensions will be lost when the container is disposed. Therefore, to use the application block objects, you must keep a strong reference to the container in your code. In addition, the objects you create using the application block extensions and the Resolve method of the container are singleton instances whose lifetime is controlled by the Unity container. They will be disposed when the container is disposed or goes out of scope. |
Specifying Unity Integration through Configuration
An alternative approach is to specify the configuration requirements in your configuration source. You can configure either or both of the following:
- You can specify the block extensions you want to use in the Unity configuration.
- You can specify how Unity should build Enterprise Library objects using the type configuration element in the Unity configuration to define mappings between types (for example, a mapping that returns a SqlDatabase instance when you call the Resolve method or specify injection of the Database type). You can also specify the lifetime and other construction policies of objects using this approach.
The Enterprise Library configuration schema and the Unity Application Block schema are independent. However, you can include a configuration section for the Unity Application Block (where you can specify the configuration for one or more Unity containers; and the types, mappings, instances, and extensions for each container) in the same file as the Enterprise Library configuration. For information on the Unity configuration schema and examples of its use, see Entering Configuration Information in the documentation for The Unity Application Block.
Using the Enterprise Library Integration Facades
You can use Unity to inject instances of Enterprise Library objects into your custom objects and classes. For more information, see the following section Injecting Enterprise Library Objects into Custom Business Objects.
However, some features of Enterprise Library are provided by static façades that cannot be injected. Instead, you can use the new non-static façades provided with the application blocks. The following table shows the façades you can use and the corresponding static Enterprise Library façade that they replace.
|
Existing façade
|
New façade for use with Unity
|
|
ExceptionPolicy
|
ExceptionManager
|
|
Tracer
|
TraceManager
|
|
Cryptographer
|
CryptographyManager
|