Overview
Unity is a lightweight and extensible dependency injection container that supports constructors, properties, and method invocation injections. Unity can handle the problems faced by developers who work on component-based software engineering. The key to building a successful application is to implement a very loosely coupled design. Loosely coupled applications are more flexible and easier to maintain. Such a program is also easier to test during development. You can simulate objects that have strong specific dependencies on shims (lightweight simulation implementations), such as database connections, network connections, ERP connections, and rich user interface components. For example, an object that handles customer information might depend on the data store that other objects access, validate the information, and check whether the user is authorized to perform the update. Dependency injection technology ensures that the client class correctly instantiates and populates all of these objects, especially if the dependency may be abstract.
Unity configuration file
<?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 "> <container> <!--register Type=" full class name, Namespace "- <register type=" unitytest.isqlhelper,unitytest "mapto=" Unitytest.mysqlhelper,unitytest " > <lifetime type= "singleton"/> </register> </container> </unity> </configuration>
Note that the values of type and mapto, separated by commas, are all of the classes, including the namespace, and the second namespace.
Well, there are other methods, first set up the namespace, then directly write the class name, this will not say.
Here is the simple configuration, detailed configuration of the self-search.
Download and Reference
To the official download: http://unity.codeplex.com/
referencing DLLs in projects
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Configuration.dll
Program
Assuming that the database operation class is replaced, it establishes an interface for the Operation class, and the specific implementation leaves the derived class.
Operation class Interface
Using system;using system.collections.generic;using system.linq;using system.text;namespace UnityTest{public Interface Isqlhelper { string SqlConnection (); } Public interface Iotherhelper { string getsqlconnection ();} }
Derived class One: Ms SQL Server
Using system;using system.collections.generic;using system.linq;using system.text;namespace UnityTest{public Class Mssqlhelper:isqlhelper {public string SqlConnection () { return ' this MSSQL. '; }} }
Derived class Two: MySQL
Using system;using system.collections.generic;using system.linq;using system.text;namespace UnityTest{public Class Mysqlhelper:isqlhelper {public string SqlConnection () { return ' this MySQL. '; }} }
Other classes
Using system;using system.collections.generic;using system.linq;using system.text;namespace UnityTest{public Class Myotherhelper:iotherhelper { isqlhelper sql; Public myotherhelper (Isqlhelper sql) { this.sql = SQL; } public string getsqlconnection () { return this.sql.SqlConnection ();}} }
Main program Call
Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code
Here, it's over.
Copy your own code to run once, I believe you will be able to understand more deeply.
C # 's use of lightweight (IoC Container) Dependency Injection Unity