c# - Selecting string value to inject based on active controller -
c# - Selecting string value to inject based on active controller -
i attempting configure web service have 2 controllers, each referencing repository in turn references database connection context. connection string used in context depends on controller beingness used. controllers know respective repositories, repositories know database context, context knows connection string.
if there way know controller in utilize in instanceperapirequest write logic in composition root it, doesn't appear possible.
since instanceperapicontrollertype works supported services, i'm not able e.g.
container.register(c => new mydbcontext("my string")) .instanceperapicontrollertype(typeof(documentcontroller)) .as<mybasedbcontext>(); container.register(c => new mydbcontext("my other string")) .instanceperapicontrollertype(typeof(workflowcontroller)) .as<mybasedbcontext>();
this setup:
var builder = new containerbuilder(); var config = globalconfiguration.configuration; builder.registerhttprequestmessage(config); builder.registerwebapifilterprovider(config); var container = builder.build(); var configurator = new autofacconfigurator(); configurator.configure(builder); var resolver = new autofacwebapidependencyresolver(container); globalconfiguration.configuration.dependencyresolver = resolver; // controllers container.registerapicontrollers(typeof(documentcontroller).assembly); container.registerapicontrollers(typeof(workflowcontroller).assembly); // wire context string each controller here // document container.register(c => { var connectionstring = configurationmanager.appsettings[appsettingsnames.databaseconnection]; var newconnectioncontext = new sqlserverconnectioncontext(connectionstring) { productid = productid }; newconnectioncontext.open(); homecoming newconnectioncontext; }).instanceperapirequest() .as<isqlserverconnectioncontext>() .as<iconnectioncontext>(); // workflow container.register(c => { var connectionstring = configurationmanager.appsettings[appsettingsnames.workflowdatabaseconnection]; var newconnectioncontext = new sqlserverconnectioncontext(connectionstring) { productid = productid }; newconnectioncontext.open(); homecoming newconnectioncontext; }).instanceperapirequest() .as<isqlserverconnectioncontext>() .as<iconnectioncontext>();
obviously code gives me sec connection string since overwrites first configuration.
i've started looking @ lifetime scopes, haven't been able figure out how or configure them create things work, if they'll work solve problem.
i had solved problem ninject using ihttpcontrolleractivator
took container in constructor , used replace binding of database context object 1 using right connection string. problem database context beingness "magically" set perspective of composition root. i'm hoping avoid autofac, it's looking might need explore similar solution. at to the lowest degree autofac give me alternative of having 1 ihttpcontrolleractivator
per controller, making things little more declarative, rather having logic in there. tried that, haven't been able working (yet?).
is there better/easier way this?
update:
maybe these simpler classes might create construction clearer. in case want repositorya connectiona , inject controller a, , repositoryb gets connectionb , injected controllerb:
interface icontroller { string go(); } interface irepository { string connect(); } interface iconnection { string connectionstring { get; set; } } class controllera : icontroller { private irepository _repository; public controllera(irepository repository) { _repository = repository; } public string go() { homecoming "controller a" + _repository.connect(); } } class controllerb : icontroller { private irepository _repository; public controllerb(irepository repository) { _repository = repository; } public string go() { homecoming "controller b" + _repository.connect(); } } class repositorya : irepository { private iconnection _connection; public repositorya(iconnection connection) { _connection = connection; } public string connect() { homecoming string.format("repository connected {0}", _connection.connectionstring); } } class repositoryb : irepository { private iconnection _connection; public repositoryb(iconnection connection) { _connection = connection; } public string connect() { homecoming string.format("repository b connected {0}", _connection.connectionstring); } }
c# asp.net-web-api dependency-injection autofac
Comments
Post a Comment