IOC container feature injection sixth: Using MVC injection points to start the container

Source: Internet
Author: User

Here is the use of MVC three injection points: Idependencyresolver for Injection

The Global.asax code in the global class is as follows:

    #region MVC Inject            System.Web.Mvc.DependencyResolver.SetResolver (new  DaHua.Sites.DependencyResolve (DaHua.Common.Runtime.EngineContext.Current, System.Web.Mvc.DependencyResolver.Current));             #endregion

Implementation class:

    /// <summary>    ///MVC injection Point/// </summary>     Public classDependencyresolve:idependencyresolver {#regionFieldsPrivateIengine _engine;//Container engine        PrivateIdependencyresolver _innerresolver;//internal dependency parsing interface        #endregion        #region. ctor PublicDependencyresolve (Iengine engine, Idependencyresolver innerresolver) { This. _engine =engine;  This. _innerresolver =Innerresolver; }        #endregion        #regionMethods Public ObjectGetService (Type servicetype) {varService =_engine.            Tryresolve (servicetype); if(Service = =NULL) {Service=_innerresolver.getservice (servicetype); }            returnService; }         Publicienumerable<Object>getservices (Type servicetype) {varServices =_engine.            ResolveAll (servicetype); if(Services = =NULL) {Services=_innerresolver.getservices (servicetype); }            returnservices; }        #endregion    }

Well, to this the main, the entire process is injected, can be in the Controller's constructor to obtain services can also take advantage of Tryresolve to obtain services.

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.