C # 's use of lightweight (IoC Container) Dependency Injection Unity

Source: Internet
Author: User

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

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.