0

I'm looking for a way to switch settings dynamically from an MVC web app which uses Unity IoC container.

I have a static instance of the container which is configured in Global.asax.cs

 private static IUnityContainer _container;

 protected void Application_Start()
 {
     AreaRegistration.RegisterAllAreas();
     RegisterRoutes(RouteTable.Routes);
     InitializeUnityContainer(); 
 }

and then a custom controller factory

protected override IController GetControllerInstance(
                       RequestContext reqContext, Type controllerType)
{
    // ... (some argument checking here)

    return _container.Resolve(controllerType) as IController;        
}

We have different teams within the company who all use their own database servers (with identical schemas). The proposed solution from management is to have multiple instances of the web app running, so you go to whichever URL for your specific team. I'd rather run only one web app and have the teams select which database (and other config settings) they want the app to connect on (ideally through a drop down or some type of menu).

I'm a bit stumped as to how I could acheive this though. The unity container would need to be configured differently for each team (maybe I'd need mutliple static instances?) and other configuration setting accessed directly in the controllers would need to change. I would guess this must be a relatively common problem, is there a standard solution?

fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253
  • Possible duplicate: http://stackoverflow.com/questions/1900415/what-is-the-best-way-to-dynamic-load-connection-strings – Mark Seemann May 09 '11 at 12:51
  • @Mark I don't believe it's a duplicate at all. My problem as I understand it is that using DI means all my connection strings are fixed on application_start. How/where can I do a lookup against a dictionary/user team or whatever? Unity is injecting my connection strings from the object root. – fearofawhackplanet May 09 '11 at 13:12
  • You need to map a run-time value to a dependency: http://stackoverflow.com/questions/1943576/is-there-a-pattern-for-initializing-objects-created-via-a-di-container/1945023#1945023 – Mark Seemann May 09 '11 at 13:18

1 Answers1

1

I don't think hosting all the teams in a single application will work well - anything you have that is not request-scoped will not work correctly - eg. any Application-scoped data or static fields could cause issues, being set by only the first visitor, or overridden constantly..

The cleanest way would definitely to have multiple Applications (even if they point to the same codebase).

Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275
  • I hadn't thought of that, that would be a great solution. How is this achieved? So I'd want to have all my DLLs, Views, JS and CSS etc files shared, but use a different config file (and possibly an additional different CSS file). Is that possible? – fearofawhackplanet May 11 '11 at 10:57
  • In IIS you can point multiple applications/sites at the same folder. Loading different config may be more tricky if it's in Web.config - not sure if you could load this from another file based on the host header (if not, you might have to wrap your config up, so access to it can check the host header) – Danny Tuppeny May 11 '11 at 11:51