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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:41:16+00:00 2026-05-24T08:41:16+00:00

I have these interfaces: public interface IUnitOfWork { IPersonRepository People { get; } IBookRepository

  • 0

I have these interfaces:

public interface IUnitOfWork
{
    IPersonRepository People { get; }
    IBookRepository Books { get; }
    int Commit();
}

public interface IBookRepository
{
    Book GetBookById(int id);
    IQueryable<Book> GetAllBooks();
}

public interface IPersonRepository
{
    Person GetPersonById(int id);
    IQueryable<Person> GetAllPeople();
}

I implement IUnitOfWork as follow:

public class SqlUnitOfWork : IUnitOfWork
{
    private readonly DbContext dbContext;

    public SqlUnitOfWork()
    {
        dbContext = new DbContext("name=SQLContainer");
    }

    public IPersonRepository People
    {
        get { return IoC.Container.Resolve<IPersonRepository>(new { DbContext = dbContext }); }
    }

    public IBookRepository Books
    {
        get { return IoC.Container.Resolve<IBookRepository>(new { DbContext = dbContext }); }
    }

    public int Commit()
    {
        return dbContext.SaveChanges();
    }
}

The implementations of IBookRepository and IPersonRepository uses a constructor that takes a DbContext as a parameter, and this DbContext is created in the SqlUnitOfWork (code above) and I pass this parameter using an overload of the Resolve method.

My question is, is this the right way to do it? Is this a good practice?

Thanks!

  • 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-24T08:41:17+00:00Added an answer on May 24, 2026 at 8:41 am

    Using a DI Container as a Service Locator can hardly be said to be good practice. In addition to that, passing the DbContext to the container while resolving an interface is a Leaky Abstraction because it implies that you know something about the concrete implementation that you should not.

    Instead I would recommend Constructor Injection, which would go something like this:

    public class SqlUnitOfWork : IUnitOfWork
    {
        private readonly DbContext dbContext;
        private readonly IPersonRepository personRepository;
        private readonly IBookRepository bookRepository;
    
        public SqlUnitOfWork(DbContext dbContext,
             IPersonRepository personRepository, IBookRepository bookRepository)
        {
            if (dbContext == null)
                throw new ArgumentNullException("dbContext");
            if (personRepository == null)
                throw new ArgumentNullException("personRepository");
            if (bookRepository = null)
                throw new ArgumentNullException("bookRepository");
    
            this.dbContext = dbContext;
            this.personRepository = personRepository;
            this.bookRepository = bookRepository;
        }
    
        public IPersonRepository People
        {
            get { return this.personRepository; }
        }
    
        public IBookRepository Books
        {
            get { return this.bookRepository; }
        }
    
        public int Commit()
        {
            return this.dbContext.SaveChanges();
        }
    }
    

    Even though there’s no explicit sharing of DbContext, this can be configured through the container. Since the context of this question indicates that Castle Windsor is the container being used, the default lifetime is already Singleton, so you don’t have to explicitly set this up. With Castle Windsor, the DbContext will automatically be shared between the SqlUnitOfWork class and both repositories.

    However, you can also explicitly configure the context to be shared, like this:

    container.Register(Component.For<DbContext>().LifeStyle.Singleton);
    

    If you were to use another DI Container, the API would be different, but the concept is the same.

    Bonus info: I don’t know what the overall context is, but if this is to be used in a web application and DbContext is an Entity Framework or LINQ to SQL context, the correct lifetime configuration would instead be PerWebRequest as none of those context classes are thread-safe:

    container.Register(Component.For<DbContext>().LifeStyle.PerWebRequest);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two interfaces like these: public interface IMyInterface1 { string prop1 { get;
Suppose I have these interfaces: public interface I1 { void foo(); } public interface
I have these base interfaces and providers in one assembly (Assembly1): public interface IEntity
Can't figure out this generics problem. I have these interfaces: public interface LoadableObject {
Say, I have three interfaces: public interface I1 { void XYZ(); } public interface
I have two interfaces: public interface A { void aMethod(); } public interface B
I have the following interfaces and classes: public interface ILoggingService { ... } public
I have an object that implements two interfaces... The interfaces are: public interface IObject
I have a class that implements multiple interfaces. I would like to register these
In c#, we have interfaces. Where did these come from? They didn't exist in

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.