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 6860371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:24:02+00:00 2026-05-27T02:24:02+00:00

Using: ASP.NET MVC3 Ninject 2 Fluent nHibernate I have 2 databases (DB1 & DB2).

  • 0

Using:
ASP.NET MVC3
Ninject 2
Fluent nHibernate

I have 2 databases (DB1 & DB2). I have one base repository class (Repository) and many Controllers (Controller1, Controller2).

public MyController(IRepository<SomeModelFromDB1> someModelFromDB1Repository, IRepository<SomeModelFromDB2> someModelFromDB2Repository)
{
    [...]
}

public class Repository<T> : IRepository<T> where T : Entity
{
    private readonly ISession _session;

    public Repository(ISessionFactory sessionFactory)
    {
        _session = sessionFactory.OpenSession();
    }
}

public class DB1SessionFactory : ISessionFactory
{
    private readonly NHibernate.ISessionFactory _sessionFactory;

    private ISession _session;

    public DB1SessionFactory()
    {
        [...]
    }
}

public class DB2SessionFactory : ISessionFactory
{
    private readonly NHibernate.ISessionFactory _sessionFactory;

    private ISession _session;

    public DB2SessionFactory()
    {
        [...]
    }
}

Now when I create MyController. I want my repository injected, but that repository should use DB1 (or DB2, depending on the model) SessionFactory.

I cannot figure how to properly inject it all… When I had only a single SessionFactory (DB1), here what I had with NINJECT:

kernel.Bind<ISessionFactory>().To<DB1SessionFactory>()
            .InRequestScope();

kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>));

Edit:

The best would be that the right session is injected in the repository depending on the model. Since some models are from DB1 and others from DB2, the choice should be dependent from it. It would be great also if the controller/view developer not have to bother with anything (like having [Named] in front of the repository) but if it’s what it takes. Even with [Named], I couldn’t figure how to inject the right session in the repository based on the [Named] repository in the controller…

  • 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-27T02:24:03+00:00Added an answer on May 27, 2026 at 2:24 am

    First of all, you should define the session factory in singelton scope and have the session in request scope.

    Do the configuration like this:

    .Bind<ISessionFactory>().To<DB1SessionFactory>().Named("DB1")
         .InSingletonScope();
    
    .Bind<ISessionFactory>().To<DB2SessionFactory>().Named("DB2")
         .InSingletonScope();
    
    private bool IsOnDB(IRequest request, string dbName)
    {
        var repositoryType = request.ParentRequest.Service;
        var modelType = repositoryType.GetGenericArguments()[0];
        var databaseName = this.GetDatabaseForModel(modelType);
    
        return databaseName == dbName;
    }
    
    .Bind<ISession>()
        .ToMethod(ctx => ctx.Kernel.Get<ISessionProvider>("DB1").OpenSession())
        .When(r => this.IsOnDb(r, "DB1"))
        .InRequestScope();
    .Bind<ISession>()
        .ToMethod(ctx => ctx.Kernel.Get<ISessionProvider>("DB2").OpenSession())
        .When(r => this.IsOnDb(r, "DB2"))
        .InRequestScope();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Ninject, NHibernate, ASP.NET MVC3 and repository pattern. The module binding in
I have an ASP.NET MVC3 website setup using fluent validation and ninject. The validation
I am using: Asp.net MVC3 Ninject Repository pattern with Entity Framework 4.0 On an
I have been using Ninject IoC container in my ASP.NET MVC3 portal. Whenever I've
I am using Asp.net mvc3 and displaying the .pdf file on my one of
I had created a webpage using asp.net mvc3 razor. I have two master pages
I've got a simple web application using ASP.NET MVC3 and Ninject.Web.MVC (the MVC3 version).
I have a web project using asp.net mvc3. Now clients ask for a security
I'm playing around with using ASP.NET MVC3 and have a very simple site that
I have a webpage I am working on using asp.net mvc3. I am deploying

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.