In the previous article, I briefly introduced some background knowledge about unity, why should we use unity, and what are the advantages of using unity. We will continue to learn about unity today, mainly to understand some common usage methods of unity.
This articleArticleWe will mainly introduce:
Basic usage of unitycontainer in unity, including common methods of the unitycontainer class, and several small examples are used to introduce specific usage methods.CodeAnd configuration files.
From the name of the unitycontainer (Unity container) class, we can see that it is the most important class of Unity. unitycontainer is like a headquarters of unity, and objects are like soldiers, every soldier must use unitycontainer to manage the dependencies between all objects. All objects are created through the Unity container. It can also be said to be a foreign ministry, for our developers, we don't need to care about how internal implementation is implemented. We just need to set the relationship between objects in advance, and then tell unitycontainer what I need when necessary, unitycontainer will send us what we need directly. (These metaphors may be incorrect, but they are the best one I can think)
Use code to achieve object Association Registration:
First, let's look at a simple example:
Public interface iclass {void showinfo ();} public class myclass: iclass {public myclass () {} public void showinfo () {console. writeline ("this is my class");} specific call:
Static void main (string [] ARGs) {iclass classinfo = new myclass (); classinfo. showinfo ();}
This is the most common interface and its implementation class usage. It defines an interface and then defines a class to implement this interface. Then, in the specific process of use, the New Keyword can be used to instantiate the specific implementation interface. Although there is no syntax problem, this will cause tight coupling. If the specific implementation class changes, you need to modify the code, and if there are many similar codes, it will lead to changes to the entire project, or even exceptions, so we need to use IOC for decoupling. The specific code is as follows:
Public static void containercode () {iunitycontainer Container = new unitycontainer (); container. registertype <iclass, myclass> (); // another registration method, but there is no registertype <> () method for convenience // container. registertype (typeof (iclass), typeof (myclass); iclass classinfo = container. resolve <iclass> (); // another method for getting a specific object through container // iclass classinfo = container. resolve (typeof (iclass); classinfo. showinfo ();}
You can use unity to manage the relationship between objects in the following steps:
1. Create an unitycontainer object.
2. Use the registertype method of the unitycontainer object to register the relationship between the object and the object.
3. Use the Resolve Method of the unitycontainer object to obtain the object associated with the specified object.
Use the configuration file to register object relations:
The above code is used to register the relationship between objects, but for a project, after the formal deployment, because the code is compiled into a DLL, if you want to modify the dependency, you need to modify the code and re-compile the Code. This is too troublesome. Therefore, unity also provides the configuration file to configure the relationship between objects. The configuration is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <configsections> <section name = "Unity" type = "Microsoft. practices. unity. configuration. unityconfigurationsection, Microsoft. practices. unity. configuration "/> </configsections> <unity xmlns =" http://schemas.microsoft.com/practices/2010/unity "> <alias =" iclass "type =" unitystudyconsole. idemo. iclass, unitystudyconsole "/> <alias =" myclass "type =" unitystudyconsole. demo. myclass, unitystudyconsole "/> <container> <register type =" iclass "name =" configclass "mapto =" myclass "/> </container> </Unity> </configuration>
The code is called as follows:
Public static void containerconfiguration () {iunitycontainer Container = new unitycontainer (); // gets the configuration section unityconfigurationsection = (unityconfigurationsection) configurationmanager for the specified name. getsection ("Unity"); // default method. By default, the container configuration information under the "Unity" configuration section is obtained. loadconfiguration (); // obtain the configuration information container under the named configuration section <container name = "firstclass">. loadconfiguration ("firstclass"); // obtain the configuration information container in the specific configuration section. loadconfiguration (section); // obtain the configuration information container under the named configuration section <container name = "firstclass"> in the specific configuration section. loadconfiguration (section, "firstclass"); iclass classinfo = container. resolve <iclass> ("configclass"); classinfo. showinfo ();}
To configure the unity information through the configuration file, follow these steps:
1. Register unity in the <configsections> Configuration section of the configuration file.
2. Add unity configuration information in the <configuration> Configuration section.
3. Read the configuration information in the Code and load the configuration into the unitycontainer.
The configuration file can be used to configure object information. Although the dependency between objects can be changed during deployment, if the system is too complex, the configuration file will increase, therefore, we need to extract the unity configuration information from the app. config or web. config is separated, but the method called in the previous code is invalid. Now we need to modify the existing code:
Public static void containerconfigurationfromfile (string configfile) {// obtain the specified config file var filemap = new execonfigurationfilemap {execonfigfilename = configfile} Based on the file name }; // read configuration information from the config file configuration = configurationmanager. openmappedexeconfiguration (filemap, configurationuserlevel. none); var unitysection = (unityconfigurationsection) configuration. getsection ("Unity"); var Container = new unitycontainer (). loadconfiguration (unitysection, "firstclass"); iclass classinfo = container. resolve <iclass> ("configclass"); classinfo. showinfo ();}
Since the unity configuration is relatively complex, it is difficult to get started at once, and unlike other modules in the Enterprise Library, the configuration tool can be used for configuration. Therefore, the P & P team can configure unity through the configuration file for convenience, unityconfigurationconfigurationconfigurationconfigurxsd has been built in the installation package of Enterprise Library 5.0. You can find it under X: \ Program Files \ Microsoft Visual Studio x.0 \ XML \ schemas, in this way, you can only prompt When configuring unity in the configuration file, such:
Note that if you want to see this prompt, you need to add an xmlnsHttp://schemas.microsoft.com/practices/2010/unity. (If entlib5 is not installed, so there is no unityconfigurationconfigurationconfigurxsd, you can find this XSD in the following sample code .)
The above is all the content in this article. It mainly introduces the basic usage of unitycontainer-how to implement the relationship between objects through code and configuration files. If you have any questions, please kindly advise!
Download Sample Code: Click here to download
Index of a series of articles on the learning path of Microsoft enterprise database 5.0:
Step 1: getting started
Step 2: Use the vs2010 + data access module to create a multi-database project
Step 3: Add exception handling to the project (record to the database using custom extension)
Step 4: Use the cache to improve the website's performance (entlib caching)
Step 5: Introduce the entlib. validation module information, the implementation level of the validators, and the use of various built-in validators-Part 1
Step 5: Introduce the entlib. validation module information, the implementation level of the validators, and the use of various built-in validators-Part 1
Step 5: Introduce the entlib. validation module information, the implementation level of the validators, and the use of various built-in validators-Part 2
Step 6: Use the validation module for server-side data verification
Step 7: Simple Analysis of the cryptographer encryption module, custom encryption interfaces, and usage-Part 1
Step 7: Simple Analysis of the cryptographer encryption module, custom encryption interfaces, and usage-Part 2
Step 8. Use the configuration setting module and other methods to classify and manage enterprise database configuration information
Step 9: Use the policyinjection module for AOP-PART1-basic usage
Step 9: Use the policyinjection module for AOP-PART2-custom matching rule
Step 9: Use the policyinjection module for AOP-PART3 -- Introduction to built-in call Handler
Step 9: Use the policyinjection module for AOP-PART4 -- create a custom call handler to achieve user operation Logging
Step 10: Use unity to decouple your system-Part1-Why use unity?
Step 10: Use unity to decouple your system-Part2-learn how to use Unity (1)
Step 10. Use unity to decouple your system-Part2-learn how to use Unity (2)
Step 10: Use unity to decouple your system-Part2-learn how to use Unity (3)
Step 10: Use unity to decouple your system-Part3-dependency Injection
Step 10: Use unity to decouple your system-part4 -- unity & piab
Extended learning:
Extended learning and dependency injection in libraries (rebuilding Microsoft Enterprise Library) [go]