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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:16:16+00:00 2026-06-03T03:16:16+00:00

I have an application that uses a generic repository pattern for data access. As

  • 0

I have an application that uses a generic repository pattern for data access. As the application requires the use of several different databases I’ve implemented a repository factory using the Ninject Factory Extension. This allows me to create repositories for the various databases on demand. I simply pass the DBContext to the factory as follows:

    private readonly IRepository database1;
    private readonly IRepository database2;

    public MembershipService(IRepositoryFactory repositoryFactory)
    {
        this.database1 = repositoryFactory.CreateRepository(new Context("Database1"));
        this.database2 = repositoryFactory.CreateRepository(new Context("Database2"));
    }

What’s annoying with this code is the string required to create a Context. The strings “Database1” and “Database2” in the example above. When the above code is used many, many times throughout the project it’s far too easy for a simple typo to cause major problems.

How can I remedy this situation? Should I create a context factory? That would still require the database name. Could I somehow use Enums? I’ve been tossing around a bunch of ideas but nothing seems to fit quite right

  • 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-06-03T03:16:17+00:00Added an answer on June 3, 2026 at 3:16 am

    Put a constructor that accepts DbContext on the deriving class of your IRepositoryFactory

    class RepositoryFactory : IRepositoryFactory
    {
        DbContext _dbc;
        public RepositoryFactory(DbContext db)
        {
           _dbc = db;
        }
    
        public IRepository CreateRepository()
        {
            return new Repository(_dbc);
        }
    }
    

    Then you bind that DbContext on your Ninject injection bindings, put this along with your other bindings:

    ninjectKernel.Bind<DbContext>().To<EfDbContext>().InRequestScope();
    

    I’m just taking a guess here, this is how your injection bindings for RepositoryFactory looks like:

    ninjectKernel.Bind<IRepositoryFactory<Person>>().To<RepositoryFactory<Person>>();
    

    If you made those two bindings next to each other (order is not important), Ninject shall be able to do an injection of value to the DbContext parameter of your RepositoryFactory’s constructor.


    An example

    Look at line 60 to 76 here: http://code.google.com/p/flexigrid-crud-example/source/browse/trunk/FlexigridCrudDemo/FlexigridCrudDemo/NinjectDependencyResolver.cs

    See the repository pattern for EF here, line 22: http://code.google.com/p/to-the-efnh-x/source/browse/trunk/ToTheEfnhX/Ienablemuch.ToTheEfnhX.EntityFramework/EfRepository.cs

    See the repository pattern for NHibernate here, line 24: http://code.google.com/p/to-the-efnh-x/source/browse/trunk/ToTheEfnhX/Ienablemuch.ToTheEfnhX.NHibernate/NhRepository.cs

    And how I abstracted those two dissimilar ORMs with repository pattern, the depency injection for connections(DbContext for Entity Framework, Session for NHibernate) is facilitated by Ninject:

    int target = 1; // memory, nhibernate, entity framework
    
    switch (target)
    {
        case 0:
            ninjectKernel.Bind<IRepository<Person>>().ToMethod(x =>
            {
                var person = new MemoryRepository<Person>();
                person.Save(new Person { Username = "Hello", Firstname = "Yo", FavoriteNumber = 9 }, null);
                person.Save(new Person { Username= "See", Firstname = "Nice" }, null);
    
                return person;
            }
            ).InSingletonScope();
    
    
            break;
    
        case 1:
            ninjectKernel.Bind<ISession>().ToMethod(x => ModelsMapper.GetSessionFactory().OpenSession()).InRequestScope();
            ninjectKernel.Bind<Ienablemuch.ToTheEfnhX.IRepository<Person>>().To<Ienablemuch.ToTheEfnhX.NHibernate.NhRepository<Person>>();
            ninjectKernel.Bind<Ienablemuch.ToTheEfnhX.IRepository<Country>>().To<Ienablemuch.ToTheEfnhX.NHibernate.NhRepository<Country>>();
    
    
            break;
    
    
    
        case 2:
    
            ninjectKernel.Bind<DbContext>().To<EfDbContext>().InRequestScope();
            ninjectKernel.Bind<Ienablemuch.ToTheEfnhX.IRepository<Person>>().To<Ienablemuch.ToTheEfnhX.EntityFramework.EfRepository<Person>>();
            ninjectKernel.Bind<Ienablemuch.ToTheEfnhX.IRepository<Country>>().To<Ienablemuch.ToTheEfnhX.EntityFramework.EfRepository<Country>>();
    
            break;
    
        default:
            break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have application that uses direct data access to it's Database. I have nothing
I have client application that uses WCF service to insert some data to backend
I have an application that uses databases through jdbc API (in fact spring jdbc
I have a Java application (generic) that uses a database via hibernate. I ship
I have an application that uses richfaces 4.1 components and relies on an old,
I have an application that uses itextsharp to fill PDF form fields. I got
I have an application that uses NHibernate as its ORM and sometimes it experiences
I have an application that uses the accelerometer. Sometimes, the application will launch without
I have an application that uses window.open() to generate dynamic popups. Unfortunately, I've had
I have an application that uses a cron like job to update a set

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.