c# 4.0 - Microsoft Unity - How to register connectionstring as a parameter to repository constructor when it can vary by client? -
i relatively new ioc containers apologize in advance ignorance.
my application asp.net 4.0 mvc app uses entity framework repository layer on top of that. multi tenant application connection string used varies logged in client.
the connection string determined 'key' gets passed in part of route indicates client. route data present on first request of user's session.
the route looks kind of this: http://{host}/login/dev/
where 'dev' indicates using dev database.
currently ioc container registering dependencies in global.asax application_start event handler , have 'key' hardcoded follows:
var cnstring = commonservices.getdbconnection("dev"); container.registertype<irequestmgmtrecipientrepository, requestmgmtrecipientrepository>( new injectionconstructor(cnstring)); is there way unity dynamically register repository based on logged in client using route data supplied initially?
note: not manually resolving repositories. getting constructed container when controllers instantiated. stumped.
thanks!
quick assumption, can use host identify tenant.
the following article has different approach http://www.agileatwork.com/bolt-on-multi-tenancy-in-asp-net-mvc-with-unity-and-nhibernate-part-ii-commingled-data/, using nh, usable.
based on above hacked code may work (not tried/complied following, not of unity user, more of windsor person :) )
container.registertype<irequestmgmtrecipientrepository, requestmgmtrecipientrepository>(new injectionfactory(c => { //the following can via static class //httpcontext.current.request.url.host, if remember correctly var context = c.resolve<httpcontextbase>(); var host = context.request.headers["host"] ?? context.request.url.host; var connstr = commonservices.getdbconnection("dev_" + host); //assumed return new requestmgmtrecipientrepository(connstr); })); scenario 2 (i not think case)
if client identifies tenant (not host, ie http: //host1), suggests need access database access client information? in case database holds client information, need have enough information identify tenant.
the issue senario 2 arise around anon uses, tenant being accessed.
assuming senario 2, injectionfactory should still work.
hope helps
Comments
Post a Comment