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

The Archive Base Latest Questions

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

I have a BaseRepository that depends a DbContext to perform the database operations: public

  • 0

I have a BaseRepository that depends a DbContext to perform the database operations:

public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : Entity
{
   ...
}

I don’t want to insert this dependency using Constructor Dependency Injection, because if I use I’ll need to pass these dependency in constructors of derived repositories. I don’t want to use Property/Setter Dependency Injection too because Property/Setter Dependency Injection indicates dependency is optional, which is not the case.

My DbContext inherit from IDbContext interface, where is my UnitOfWork Pattern:

public class DbContext : System.Data.Entity.DbContext, IDbContext
{
   ...
}

And I’ve set my IDbContext using Ninject:

public override void Load()
{
   Bind<IDbContext>().To<DbContext>().InRequestScope();
}

My question is how I inject DbContext in Base Repository, and I need a single instance of DbContext in a requestScope. (Using a Factory?)

  • 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-26T07:20:31+00:00Added an answer on May 26, 2026 at 7:20 am

    I found the solution using the Service Locator of Ninject and took back the instance of DbContext:

    public class ExampleClass()
    {
        protected DbContext DbContext
        {
            get
            {
                                                                            //Here I do the trick I wanted
                return DependencyResolverFactory.Instance.Get<IDbContext>() as DbContext;
            }
        }
    
        ...
    }
    

    And my Dependency Resolver class:

    public static class DependencyResolver
    {
        private static IKernel _kernel;
    
        static DependencyResolver()
        {
            _kernel = new StandardKernel();
            _kernel.Load(Assembly.GetExecutingAssembly());
        }
    
        public static IKernel GetCurrentKernel()
        {
            return _kernel;
        }
    
        public static void RegisterKernel(IKernel kernel)
        {
            _kernel = kernel;
        }
    
        public static T Get<T>()
        {
            return _kernel.Get<T>();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this: public class DbContext : System.Data.Entity.DbContext, IDbContext { } My Ninject configuration:
I have a base class that has a repository injected into the constructor, now
I have an object where I maintain a relationship to another object: public class
Have some dates in my local Oracle 11g database that are in this format:
I have a generic base repository class for LINQ-to-Entities that can do things like
a question about class design. Currently I have the following structure: abstract Base Repository
I have a persistence ignorant domain model that uses abstract repositories to load domain
I have such a class: public static class CacheManager { static object lockObject =
I have a class divided in two partial files, like this: public partial class
I have a generic abstract base class with a service reference. When I try

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.