Enterprise Library-Unity Application Block learning Manual (Latest Version) Part 3

Source: Internet
Author: User
Tags configuration settings
This article demonstrates how to use the Enterprise Library-Unity Application Block dependency injection module. In this article, we will use the application configuration file to set the unity container. The configuration file to set dependency injection is similar to Part 2's API configuration. In fact, configuration settings can be considered as scripts for calling APIs. To use unity, follow these steps: 1. Create a container; 2. register the ing between interfaces and classes to the container; 3. parse the correct object from the container. This article by the http://blog.entlib.com open source ASP. Net blog platform team according to the entlib Hol manual compilation to provide, welcome to exchange. Exercise 3: Use the configuration file to set the dependency injection container )First, open the stocksticker. sln file in the labs \ lab03 \ begin \ stocksticker directory. Add reference to Microsoft. Practices. Unity. Configuration and system. Configuration assembly .. The built-in system. Configuration assembly of net framework is used to retrieve configuration information from the application configuration file. 1. Update the startup code. Use the configuration file to set container (1) Open the program. CS file and add references to the following namespace. Using system. configuration; using Microsoft. practices. unity. configuration; (2) retrieve the unity configuration section from the configuration file to configure the container instead of calling the registertype method. The Code is as follows. Using (iunitycontainer Container = new unitycontainer ()){ Unityconfigurationsection config = (Unityconfigurationsection) configurationmanager. getsection ("Unity ");If (config! = NULL) {config. containers. default. configure (container);} stockstickerpresenter presenter = container. resolve <stockstickerpresenter> (); application. run (form) presenter. view);} configurationmanager is used to retrieve configuration information from the configuration file. The preceding Code uses the unity parameter to obtain the unityconfigurationsection object from the configuration file, indicating the default container configuration. 2. Update the configuration file containing the container configuration information. Open the app. config file and add the following unity configuration node in the Visual Studio editor. (Entlib.com open-source team lead: Unfortunately, the editor attached to entlib currently does not support editing the attributes and configuration items of the Unity application block, so you need to manually edit the configuration file ). <Configsections>... <section name = "Unity" type = "Microsoft. practices. unity. configuration. unityconfigurationsection, Microsoft. practices. unity. configuration, version = 1.2.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "/> </configsections> where, the section name unity must be the same as the name specified in the preceding code. Add the unity node element as follows. </System. Diagnostics> <unity> </Unity> </configuration> the node element name must be consistent with the section name registered in configsections. Add the typealias element to the type used in the exercise, as shown in the following figure: <unity> <typealiases> <typealias alias = "string" type = "system. string, mscorlib "/> <typealias alias =" tracesource "type =" system. diagnostics. tracesource, system, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089 "/> <typealias alias =" Singleton "type =" Microsoft. practices. unity. containercontrolledlifetimemanager, Microsoft. practices. unity, version = 1.2.0.0, Culture = neutral, publickeytoken = 31bf3856ad364e35 "/> <typealias alias =" ilogger "type =" stocksticker. loggers. ilogger, stocksticker "/> <typealias alias =" consolelogger "type =" stocksticker. loggers. consolelogger, stocksticker "/> <typealias alias =" tracesourcelogger "type =" stocksticker. loggers. tracesourcelogger, stocksticker "/> <typealias alias =" istockquoteservice "type =" stocksticker. stockquoteservice S. istockquoteservice, stocksticker "/> <typealias alias =" moneycentralstockquoteservice "type =" stocksticker. stockquoteservices. moneycentralstockquoteservice, stocksticker "/> <typealias alias =" istockstickerview "type =" stocksticker. UI. istockstickerview, stocksticker "/> <typealias alias =" stockstickerform "type =" stocksticker. UI. stockstickerform, stocksticker "/> <typealias alias =" stockstickerpresente R "type =" stocksticker. UI. stockstickerpresenter, stocksticker "/> </typealiases> </Unity> although the type alias (type aliases) is not mandatory, it makes the configuration file easier to read. Each typealias element contains an alias, which can be used in the Unity configuration item to replace the specific type name. In the configuration file, add the containers and container elements as follows. </Typealiases> <containers> <container> </containers> </Unity> the default container is no different from other named container containers, besides, it is easy to obtain the configuration information through the default attribute of the configuration management object. Add the types element to the default cotainer. <Containers> <container> <types> </container> </containers> the subnode of the types element is similar to calling the registertype method, used to register a type to a container. Add the type element below to map the istockstickerview interface to the stockstickerform class: <types> <type = "istockstickerview" mapto = "stockstickerform"/> </types> note: the alias of the type name is used above. The actual type name can also be used here. Add the type element, map the istockquoteservice interface to the moneycentralstockquoteservice class, and inject the logger attribute to the child element typeconfig. <Types> <type = "istockstickerview" mapto = "stockstickerform"/> <type = "istockquoteservice" mapto = "moneycentralstockquoteservice"> <typeconfig> <property name = "logger" propertytype = "ilogger"/> </typeconfig> </type> </types> Add the type element, map the ilogger interface to the consolelogger class: <types> <type = "istockstickerview" mapto = "stockstickerform"/> <type = "istockquoteservice" mapto = "moneycentralstockquoteservice "> <Typeconfig> <property name =" logger "propertytype =" ilogger "/> </typeconfig> </type> <type =" ilogger "mapto =" consolelogger "/> </types> The following is a little complicated, add the type element ing ilogger interface to the tracesourcelogger class and name it UI. The sub-element typeconfig is used to create an object instance, define the value element, and pass in the UI string parameters. In addition, the subelement lifetime uses the singleton alias to indicate containercontrolledlifetimemanager. <Types> <type = "istockstickerview" mapto = "stockstickerform"/> <type = "istockquoteservice" mapto = "moneycentralstockquoteservice">... </type> <type = "ilogger" mapto = "consolelogger"/> <type name = "UI" type = "ilogger" mapto = "tracesourcelogger"> <lifetime type = "Singleton"/> <typeconfig> <constructor> <Param name = "sourcename" parametertype = "string"> <value = "UI"/> </param> </Constructor> </Typeconfig> </type> </types> the constructor element is the same as the injectionconstructor class in the code. Finally, add the type element to the stockstickerpresenter class, and include a sub-element typeconfig to configure the injection logger attribute, the injected keywords include the ilogger interface and the name UI (use the dependency element to specify the name UI ). <Types> <type = "istockstickerview" mapto = "stockstickerform"/> <type = "istockquoteservice" mapto = "moneycentralstockquoteservice">... </type> <type = "ilogger" mapto = "consolelogger"/> <type name = "UI" type = "ilogger" mapto = "tracesourcelogger">... </type> <type = "stockstickerpresenter"> <typeconfig> <property name = "logger" propertytype = "ilogger"> <dependency name = "UI"/> </Property> </ty Peconfig> </type> </types> is the schema of the Unity configuration item. It also includes attributes that can be set for each element. The Running Effect of the sample program is the same as that of the previous Part 1-2, including the log function. Http://www.entlib.com professional ASP. NET e-commerce platform team, welcome to continue to visit the unity Application Block learning manual. Reference:Unity Application Block hands-on labs for Enterprise Library

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.