I. Overview
IOC: English full name: Inversion of Control, Chinese name: controlled inversion, it also has a name called Dependency injection (Dependency injection).
Function: The objects of each layer are loosely coupled and decoupled, and the invocation of each layer object is completely interface-oriented. When the system is refactored, the amount of code rewriting will be greatly reduced.
Dependency Injection: When an instance of a class requires an instance assistance from another class, in a traditional program design process, there is usually a caller to create an instance of the callee. However, in a dependency injection approach, the creation of the callee's work is no longer done by the caller, so called control inversion, the work of creating the callee's instance is done by the IOC container, which is then injected into the caller, hence also called dependency injection.
Unity is a lightweight, extensible dependency Injection (Dependency injection) container developed by the Microsoft patterns & practices team;
It supports three commonly used methods of dependency Injection: constructor injection (Constructor injection), attribute injection (property injection), and method invocation injection (methods call injection).
Now with the latest version of Unity 4.0 version, you can download the latest release version at Microsoft's Open source site https://github.com/unitycontainer/unity.
By using unity, we can easily build loosely coupled programs that make the entire program framework clear and easy to maintain.
Second, a simple example
1. Example 1
Create a MVC5 project WEB.MVC, and then create a business class library, add the class library reference to the WEB.MVC project, add unity.mvc5 from the WEB.MVC project through NuGet
Class Library Code
A, Business class library defines an interface Iuserservice
namespace business{ //<summary>/// display information /// </summary> Public Interface Iuserservice { string Display (string mes);} }
B, Business Class library implementation interface
namespacebusiness{ Public classUserservice:iuserservice {/// <summary> ///Display Information/// </summary> /// <param name= "Mes" ></param> Public stringDisplay (stringmes) { return "I say:"+mes; } }}
C, registration relies on the use of dependency injection to take effect
Registering with the Global.asax of the WEB.MVC project
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingSystem.Web.Optimization;usingSystem.Web.Routing;usingBusiness ;usingMicrosoft.Practices.Unity;usingunity.mvc5;namespaceweb.mvc{ Public classMvcApplication:System.Web.HttpApplication {protected voidApplication_Start () {arearegistration.registerallareas (); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (Bundletable.bundles); //Inject Ioc varcontainer = This. Buildunitycontainer (); Dependencyresolver.setresolver (NewUnitydependencyresolver (container)); } Iunitycontainer Buildunitycontainer () {varcontainer =NewUnityContainer (); Container. Registertype<Iuserservice,UserService>(); returncontainer; } }}
D. Use
namespaceweb.mvc.controllers{ Public classHomecontroller:controller {PrivateIuserservice UserService; PublicHomeController (Iuserservice userservice) { This. UserService =UserService; } PublicActionResult Index () {viewbag.msg= Userservice.display ("hahaha"); returnView (); } }}
E, in index.cshtml find a place to write @viewbag.msg can, such as
Finally run the project to achieve the effect
Asp.net-i Say:hahahaReference URL:
Http://www.cnblogs.com/zhangchenliang/archive/2013/01/08/2850970.html
Https://www.lanhusoft.com/Article/108.html
One of the ASP. NET IOC framework get started Unity