MVC4 controllerfactory based on Unity IOC framework

Source: Internet
Author: User

First, the untiy framework is introduced. Can be installed directly in the NuGet Package Manager.

New unitycontrollerfactory for inheriting defaultcontrollerfactory:

Override virtual method GetControllerInstance.

    Public classUnitycontrollerfactory:defaultcontrollerfactory { PublicIunitycontainer UnityContainer {Get;Private Set; }  Publicunitycontrollerfactory (Iunitycontainer unitycontainer) { This. UnityContainer =UnityContainer; }        protected OverrideIController getcontrollerinstance (RequestContext requestcontext, Type controllertype) {if(NULL==Controllertype) {                return NULL; }            return(IController) This.        Unitycontainer.resolve (Controllertype); }    }

Then the unity is activated in Global.asax:

protected voidApplication_Start () {arearegistration.registerallareas ();            Webapiconfig.register (globalconfiguration.configuration);            Filterconfig.registerglobalfilters (globalfilters.filters);            Routeconfig.registerroutes (routetable.routes); //as followsUnityContainer UnityContainer =NewUnityContainer (); //Iemployeerepository is the storage layer that needs to be relied uponUnitycontainer.registertype<iemployeerepository, employeerepository>(); Unitycontrollerfactory controllerfactory=Newunitycontrollerfactory (UnityContainer);        ControllerBuilder.Current.SetControllerFactory (controllerfactory); }

Next we need to develop a dependency property injection method in the controller.

The dependency feature is to tell us this dependency property

[Dependency]  Public Get set; }

There is also an activation method based on the Ninject IOC framework through Icontrolleractivator or Idependencyresolver.

...

MVC4 controllerfactory based on Unity IOC framework

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.