Microsoft enterprise database 4.1 Study Notes (7) Create object sequel 1

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.