Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6731239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:30:17+00:00 2026-05-26T10:30:17+00:00

I have an MVC application using Ninject to connect to a single database. Now

  • 0

I have an MVC application using Ninject to connect to a single database. Now I need to support multiple databases. Currently, my global.asax.cs file has the following definition for ninject:

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

        //Using DI for controllers - use the Ninject custom controller factor 
        ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); // Repository config is defined in ninject controller 
    }

And here is what my Ninject controller class looks like:

public class NinjectControllerFactory : DefaultControllerFactory 
{ 
    private IKernel kernel = new StandardKernel(new EriskServices()); 

    protected override IController GetControllerInstance(RequestContext context, Type controllerType) 
    { 
        if (controllerType == null) 
            return null; 
        return (IController)kernel.Get(controllerType); 
    } 

    private class EriskServices : NinjectModule 
    { 
        public override void Load() 
        { 
            Bind<IRisksRepository>().To<MySql_RisksRepository>() 
                .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["mydb1"].ConnectionString); 
        } 
    } 
}

I also have a login page that handles user authentication. It is done through LDAP and does not require database connection.

My question is: Can I bind the ninject connectionString after the user authentication login page? The user would have a dropdown list for database they want to connect to, for example “mydb1” or “mydb2” or “mydb3”. Each connection string would be defined in the web.config file.

Please help! Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:30:18+00:00Added an answer on May 26, 2026 at 10:30 am

    No, you can’t bind “after” – for one thing, web applications are stateless and you have no control over the order of events, but more importantly, Ninject modules define your IoC container and this configuration happens before almost anything else in the application or request lifecycle.

    If you’re saying that the user would pick this from a drop-down, then the act of choosing the repository is part of your application logic, not part of your IoC configuration. The way to deal with this is to create a factory interface. The implementation can be a thin wrapper around the Ninject kernel itself.

    public interface IRisksRepositoryFactory()
    {
        IRisksRepository GetRepository(string name);
        // Optional: add a GetRepositoryNames() method for populating dropdowns, etc.
    }
    
    public class NinjectRisksRepositoryFactory
    {
        private readonly IKernel kernel;
    
        public NinjectRisksRepositoryFactory(IKernel kernel)
        {
            if (kernel == null)
                throw new ArgumentNullException("kernel");
            this.kernel = kernel;
        }
    
        public IRisksRepository GetRepository(string name)
        {
            return kernel.Get<IRisksRepository>(name);
        }
    }
    

    For this particular implementation you’d want to make sure you use named bindings (although generally speaking you could also use the metadata system), and do each binding explicitly:

    Bind<IRisksRepository>()
        .To<MySqlRisksRepository>()
        .InRequestScope()
        .Named("mysql")
        .WithConstructorArgument("connectionString", GetConnectionString("mydb1"));
    Bind<IRisksRepository>()
        .To<OracleRisksRepository>()
        .InRequestScope()
        .Named("oracle")
        .WithConstructorArgument("connectionString", GetConnectionString("ordb1"));
    Bind<IRisksRepositoryFactory>()
        .To<NinjectRisksRepositoryFactory>();
    

    It’s also possible to do this without creating each binding explicitly, particularly if all targets are the same type (i.e. you only have a MySqlRisksRepository), by passing the connection string or related symbol as a parameter to the Get call and binding to a contextual method instead of a type – but I’d recommend against it, since it’s really swimming against the current as far as DI in general goes.

    One more thing: Don’t worry that this resembles the “service locator” anti-pattern, because that’s all it is – a superficial resemblance. When objects need to be able to create dependencies on the fly, creating special-purpose factories around the IoC container is the recommended solution, as it minimizes kernel exposure to a single class which could be easily replaced if you switched to a different framework.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have successfully setup a simple mvc application that lists teams. I'm using Ninject
I have an mvc intranet application using windows authentication. It currently has one controller
I have an ASP.NET MVC 3 application and am using Ninject for injecting dependencies
I have a PHP MVC application using Zend Framework. As presented in the quickstart,
I have an ASP.NET MVC application using Authorization Attributes on Controllers and Actions. This
I have a ASP.NET MVC application using NHibernate and the application runs fine when
I have a very nice MVC Beta application developed using VS2008 on a win2008
I am developing an application using MVC Preview 5. I have used typed views.
I have an ASP.Net MVC application and I'm using Forms authentication in SQL Server.
I wrote an application using ASP.NET MVC, in this application I have an Index

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.