3.2 use the unity module to create an Enterprise Library object
The following describes how to use the previous method to obtain an instance of the Enterprise Library object. Sample Code:
Iunitycontainer containter = new unitycontainer ();
Containter. addnewextension <enterpriselibrarycoreextension> ();
First, create a unity container and add the enterprise library CORE configuration extension.
By default, this extension reads configuration information from the application configuration file (App. config and web. config. If you use a custom configuration source, you can use the following code to create a container and add configuration extensions.
Code
Iunitycontainer ucontainer = new unitycontainer ();
Iconfigurationsource source = new fileconfigurationsource (@ "C: \ productapplication. config ");
Ucontainer. addextension (New enterpriselibrarycoreextension (source ));
Before you add any enterprise library extensions (you can add other extensions that are not Enterprise Library extensions at any stage), you must add the enterpriselibrarycoreextension in the container. If an enterprise Library Extension already exists in the container, you can use removeallextensions to delete them and add them in the correct order.
The next step is to add the module extension corresponding to the module you want to use in the container. If you need it again, you can add multiple modules. The extension name is the module name + blockextension, such as dataaccessblockextension and loggingblockextension. Sample Code
Ucontainer. addnewextension <cachingblockextension> ();
If the module you are using is dependent on other modules, you need to add blockextension of the dependent module. The current version of blockextension includes: enterpriselibraryblockextension, dataaccessblockextension, cachingblockextension, securityblockextension, cryptographyblockextension, exceptionhandlingblockextension, and loggingblockextension.
Now you can use the container's resolve to create the provider object and the object defined in the Enterprise Library standard configuration. The following code creates a cachemananger instance.
Icachemanager cachemanager = ucontainer. Resolve <icachemanager> ();
After containers are recycled, the Enterprise Library configurations generated by container extensions are lost. Therefore, there must be a strong reference to the container in the code. In addition, the objects you create with the Enterprise Library Extension and obtained using the resolve method are all in singleton mode, and the lifecycle is controlled by the Unity container. When the container is recycled dispose or exceeds the variable range, they also disappear.
Not complete ................................
Http://www.cnblogs.com/virusswb/archive/2010/04/16/1713193.html