1

I have a small MVC application that connects to a single MYSQL database. I had it setup with Ninject to bind the connectionString during the application startup. The code looked like this:

Global.asax.cs:

protected void Application_Start()
{ 
... 
   ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); 
}

NinjectControllerFactory.cs:

public class NinjectControllerFactory : DefaultControllerFactory 
{ 
... 
   private class EriskServices : NinjectModule 
    { 
       public override void Load() 
       { 
                // Bind all the Repositories  
                Bind<IRisksRepository>().To<MySql_RisksRepository>() 
                    .WithConstructorArgument("connectionString",  
                    ConfigurationManager.ConnectionStrings["dbcMain"]
                    .ConnectionString); 
       } 
    } 
}

Today my requirements have changed and I have to now support multiple databases. I would like to have each database connection string defined in the web.config file, like how it was before. The user selects which database they want to connect to during the application login.

What would be the easiest way to bind my repositories after the login? I'm assuming I would need to code the database binding in the login controller.

I am kind of a newbie to Ninject so any examples would be much appreciated!

As always, Thanks for the time and help! .

tereško
  • 58,060
  • 25
  • 98
  • 150
John Lee
  • 1,357
  • 1
  • 13
  • 26

1 Answers1

1

I would probably Bind the repository to a Ninject.Activation.IProvider, and then create your own provider that pulls the connectionString from Session

Bind<IRisksRepository>().ToProvider<SessionConnectionProvider>();

then...

public class SessionConnectionProvider : Ninject.Activation.IProvider
{
    #region IProvider Members

    public object Create( Ninject.Activation.IContext context )
    {
        // use however you're accessing session here
        var conStr = session.ConnectionString;
        return new MySql_RisksRepository( conStr );
    }

    public Type Type
    {
        get { return typeof( IRisksRepository ); }
    }

    #endregion
}
Dave Thieben
  • 5,388
  • 2
  • 28
  • 38
  • So in this example above, the Bind to the SessisionConnectionProvider would return nothing in the begining, since the session variable wont be set until after the user logs in. Would this still work then? Other words, does that "Bind().ToProvider();" code execute for every page the user accesses? Sorry - again I'm a big newbie with Ninject and DI and trying hard to get my head around the concepts. Thanks! – John Lee Nov 04 '11 at 00:20
  • The Bind() executes only once upon app startup, and it's only creating the Binding (the configuration for Ninject). once the app is running, and a page is requested, Ninject will execute the provider any time IRiskRepository is asked for. if non-authenticated pages don't ask for it, it won't try to create it. you may want to add a check to make sure the session has the connection string, and throw an exception if it doesn't. – Dave Thieben Nov 04 '11 at 14:03