dependency injection - ASP.NET MVC4 Unity - Resolve dependencies on another project -


i have configuration in project mvc4 unity , generic configucion drivers.

bootstrapper.cs

namespace myproyect.web {     public static class bootstrapper     {         public static void initialise()         {             var container = buildunitycontainer();             servicelocator.setlocatorprovider(() => new unityservicelocator(container));             dependencyresolver.setresolver(new unitydependencyresolver(container));             globalconfiguration.configuration.dependencyresolver = new unity.webapi.unitydependencyresolver(container);         }          private static iunitycontainer buildunitycontainer()         {             var container = new unitycontainer();              container.registertype<myproyect.model.dataaccesscontract.icountrydao, myproyect.dataaccess.countrydao>();             container.registertype<myproyect.model.dataaccesscontract.icontactdao, myproyect.dataaccess.contactdao>();              return container;         }     } } 

global.asax:

    protected void application_start()     {         ...           bootstrapper.initialise();         ...       } 

genericmodelbinder.cs:

namespace myproyect.web.modelbinder {     public class genericmodelbinder : defaultmodelbinder//, ivalueprovider     {         protected override object createmodel(controllercontext controllercontext, modelbindingcontext bindingcontext, type modeltype)         {             var resolver = (unity.mvc4.unitydependencyresolver)dependencyresolver.current;             if (modeltype == null)             {                 return base.createmodel(controllercontext, bindingcontext, null);             }              return resolver.getservice(modeltype);         }     } } 

now need resolve dependency in project within solution. question how can recognize settings unity in project?. have class current configuration not bring in mvc project.

namespace myproyect.model.listdata {     public abstract class listdatagenericresolver     {         protected t resolvertype<t>()         {             iunitycontainer container = new unitycontainer();             unityservicelocator locator = new unityservicelocator(container);             servicelocator.setlocatorprovider(() => locator);              //return unity.resolve<t>();             return (t)container.resolve(typeof(t));         }     } } 

this example how use listdatagenericresolver:

namespace myproyect.model.listdata.types {     public class countrylistdata : listdatagenericresolver, igetlistdata     {         private readonly icountrydao countrydao;         private string defaultvalue;          public countrylistdata(object defaultvalue)         {             // resolver             this.countrydao = this.resolvertype<icountrydao>();             this.defaultvalue = defaultvalue == null ? string.empty : defaultvalue.tostring();         }          public ilist<selectdata> getdata()         {             var data = this.countrydao.getallcountry(new entity.parameters.countrygetallparameters());             return data.select(d => new selectdata                 {                     value = d.countryid.tostring(),                     text = d.description,                     selected = this.defaultvalue == d.countryid.tostring()                 }).tolist();         }     } } 

thank you.

here how this.

in application_start, create unity container. have custom library use of mvc projects import through nuget, make call configure method, call other project's configure() methods. omit custom library , add code in here well. of keeps application_start nice , clean.

protected void application_start() {     // standard mvc setup     // <removed>      // application configuration      var container = new unitycontainer();     new companyname.mvc.unitybootstrap().configure(container);     new appname.projectname1.unitybootstrap().configure(container);     new appname.projectname2.unitybootstrap().configure(container);      // <removed> } 

this code custom mvc library's unitybootstrap class

namespace companyname.mvc {     /// <summary>     /// bootstraps <see cref="companyname.mvc"/> unity container.     /// </summary>     public class unitybootstrap : iunitybootstrap     {         /// <inheritdoc />         public iunitycontainer configure(iunitycontainer container)         {             // convenience registration authentication             container.registertype<iprincipal>(new injectionfactory(c => httpcontext.current.user));              // integrate mvc unity             container.registerfilterprovider();             dependencyresolver.setresolver(new unitydependencyresolver(container));              return container;         }     } } 

then, in other projects, have unitybootstrap there, called application_start:

projectname1:

namespace appname.projectname1 {     public class unitybootstrap : iunitybootstrap     {         public iunitycontainer configure(iunitycontainer container)         {             return container.registertype<idocumentroutingconfiguration, documentroutingconfiguration>();         }     } } 

projectname2: - , can see in here, 1 depends on other projects in library , calling configure() methods them set too...

namespace appname.projectname2 {     public class unitybootstrap : iunitybootstrap     {         public iunitycontainer configure(iunitycontainer container)         {             new companyname.security.unitybootstrap().configure(container);             new companyname.data.unitybootstrap().configure(container);              container.registersecureservices<authorizationrulesengine>(typeof(unitybootstrap).assembly);              return container                 .registertype<iauthorizationrulesengine, authorizationrulesengine>()                 .registertype<idatetimefactory, datetimefactory>()                 .registertype<idirectoryinfofactory, directoryinfofactory>()                 .registertype<idirectorywrapper, directorywrapper>()                 .registertype<iemailservice, emailservice>()                 .registertype<ientrypointservice, entrypointservice>();         }     } } 

here iunitybootstrap interface used throughout code above (for reference)

/// <summary> /// defines standard interface bootstrapping assembly unity container. /// </summary> public interface iunitybootstrap {     /// <summary>     /// registers of assembly's classes public interfaces , performs other necessary configuration.     /// </summary>     /// <param name="container">the unity container instance configure.</param>     /// <returns>the same iunitycontainer object method called on.</returns>     iunitycontainer configure(iunitycontainer container); } 

i hope helps out.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -