Construction of ASP.net mvc4+ef5+easyui+unity2.x injection background Management system (6)

Source: Internet
Author: User
Tags constructor httpcontext visual studio

Unity 2.x Dependency Injection by run-time injection [attach source]

Unity 2.x Dependency Injection (control reversal) IOC, these are unfamiliar nouns for children's shoes without experience in major projects, and even some students stay at the stage of pulling controls.

You can visit http://unity.codeplex.com/releases to get the latest version of Unity now. Of course, if you have the NuGet Package Manager installed in your Visual Studio, you can get the latest version of unity directly in NuGet. Looks like the latest is 3, and the 5th tells us that the bad code shows how the interface

Here http://unity.codeplex.com/documentation we found the Help document you can download and see

We are using constructor injection, runtime injection.

This piece of the concept can be counted as the most fuzzy system, we should understand well, blog Park, a prawn

"ASP.net MVC3" using unity to realize dependency injection everyone inside to see

I'm not going to elaborate on that.

Just post the code and tell everyone what to do.

Download Http://files.cnblogs.com/ymnets/Microsoft.Practices.Unity.rar

In the app.admin create the library put in, the future we want to use the class library are put here, unless the description of the referenced class library are open source.

App.core references Microsoft.Practices.Unity.dll, SYSTEM.WEB.MVC, system.web,3 class library and 4. Bll,app.ibll,app.dal,app.idal 4 class Libraries

Add the following 2 classes

 using System; using System.Collections.Generic; using System.Linq; using system.web; using APP.BLL; using App.dal; u
Sing App.ibll;
Using App.idal;
    
Using Microsoft.Practices.Unity; Namespace App.core {public class Dependencyregistertype {//System injection public static void Container_sys
        (Ref UnityContainer Container) {container. REGISTERTYPE<ISYSSAMPLEBLL, syssamplebll> ();//Sample container.
        Registertype<isyssamplerepository, syssamplerepository> (); }
    }
}
Using System;
Using System.Collections.Generic;
Using System.Web;
Using SYSTEM.WEB.MVC;
    
Using Microsoft.Practices.Unity; Namespace App.core {public class Unitydependencyresolver:idependencyresolver {Private Const string HTT
    
        Pcontextkey = "Perrequestcontainer";
    
        Private ReadOnly Iunitycontainer _container;
        Public unitydependencyresolver (Iunitycontainer container) {_container = container; The public object GetService (Type servicetype) {if typeof (IController).
            IsAssignableFrom (servicetype)) {return childcontainer.resolve (servicetype); return isregistered (servicetype)?            
        childContainer.Resolve (servicetype): null; Public ienumerable<object> getservices (Type servicetype) {if isregistered (servi Cetype)) {yield return childcontainer.resOlve (servicetype); foreach (var service in Childcontainer.resolveall (servicetype)) {yield ret
            URN Service;
                }} protected Iunitycontainer Childcontainer {get {
    
                var childcontainer = Httpcontext.current.items[httpcontextkey] as Iunitycontainer; if (Childcontainer = = null) {Httpcontext.current.items[httpcontextkey] = Childcontain ER = _container.
                Createchildcontainer ();
            return childcontainer; }} public static void Disposeofchildcontainer () {var childcontainer = H
    
            Ttpcontext.current.items[httpcontextkey] as Iunitycontainer;
            if (childcontainer!= null) {childcontainer.dispose (); } private bool Isregistered (Type TypetochEck) {var isregistered = true; if (Typetocheck.isinterface | | typetocheck.isabstract) {isregistered = Childcontainer.isregis
    
                tered (Typetocheck); if (!isregistered && typetocheck.isgenerictype) {var opengenerictype = TypeTo
    
                    Check.getgenerictypedefinition ();
                isregistered = childcontainer.isregistered (Opengenerictype);
        } return isregistered; }}} UnityDependencyResolver.cs

We inject the constructor when the system starts to run. So we're going to write code
&NBSP in the global file;

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.Http;
Using SYSTEM.WEB.MVC;
Using System.Web.Optimization;
Using System.Web.Routing;
Using App.core;
    
Using Microsoft.Practices.Unity; namespace App.admin {//NOTE: For instructions on enabling IIS6 or IIS7 Classic mode,//please visit http://go.microsoft.com/? linkid=9394801 public class MvcApplication:System.Web.HttpApplication {protected void Application_
    
            Start () {arearegistration.registerallareas ();
            Webapiconfig.register (globalconfiguration.configuration);
            Filterconfig.registerglobalfilters (globalfilters.filters);
            Routeconfig.registerroutes (routetable.routes);
            Enable compression bundletable.enableoptimizations = true;
            Bundleconfig.registerbundles (Bundletable.bundles);
    
            Authconfig.registerauth ();
            Injected Ioc var container = new UnityContainer (); DependenCyregistertype.container_sys (ref Container);
        Dependencyresolver.setresolver (new Unitydependencyresolver (container)); }}} Global.asax.cs

Well, we've got

Related Article

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.