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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:25:23+00:00 2026-06-06T18:25:23+00:00

In the simplified example below I have a DataContext and Repository which I think

  • 0

In the simplified example below I have a DataContext and Repository which I think is defined in a fairly reasonably way:

public interface IUnitOfWork 
{
   int SaveChanges();
}

public class DataContext : DbContext, IUnitOfWork 
{
    public DbSet<Car> Cars { get ; set; }
}

public interface ICarsRepository 
{
    Car Find(int id);
    void Add(Car car);
}

public class SqlCarsRepository : ICarsRepository 
{
    private DataContext  _context;
    public SqlCarsRepository(DataContext context)
    {
       _context = context;
    }

    public Car Find(int id) 
    {
        return _context.Cars.Find(id);
    }

    //etc
}

I am struggling to work out how to use DI and the abstract factory pattern to achieve what I would like. In an MVC application this would be easy to setup – the Controller would require instances of implementations of IUnitOfWork and ICarsRepository in its constructor. I could configure the container to give me the same DataContext instance per Http request, using a different controller factory. Somehow it seems that here the disposable dependencies are disposed correctly.

However I would like to use the same repository within a windows service. This is multi threaded and each thread when started needs to have access to its own repository, and each thread should have its own DataContext / UnitOfWork. But I don’t know how to do this:

  • The composite root of the application is when the service starts, so dependencies cannot be resolved for each thread then (the threads are started on demand).
  • I’m not sure how I can use the abstract factory pattern. The thread needs instances of IUnitOfWork and ICarsRepository but sharing the same DataContext. I can make an abstract factory to create these both in one call, and pass that into the thread, but then I don’t know how to dispose of the DataContext. I don’t want the thread to have to care that the dependencies to the implementation of ICarsRepository it are disposable. I definitely don’t want the thread to know that the ICarsRepository has a a dependency on DataContext because then it seems pointless having an interface- the thread could just depend on SqlCarsRespository.
  • I don’t want to make SqlCarsRepository disposable and have it dispose the DataContext because there may be others using the DataContext, and it didn’t create it in the first place.
  • I thought that I could create a CarsService which hides the IUnitOfWork and ICarsRepository (and get instances of that using abstract factory), but I still then don’t know how to dispose of the DataContext dependency

What is the best way to do what I am attempting?

  • 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-06T18:25:24+00:00Added an answer on June 6, 2026 at 6:25 pm

    so dependencies cannot be resolved for each thread

    They can, and in fact, they should. You should resolve a new object graph at the beginning of each thread. Not doing this, means that you can only use thread-safe dependencies, which isn’t the case in your situation.

    I’m not sure how I can use the abstract factory pattern

    I think at first, try to define your DataContext as Per Web Request in your MVC application and as Per Lifetime Scope (or whatever available in the container you use) in your windows service. In your windows service, each thread will get it’s own lifetime scope. Defining a scope often allows you to let instances be disposed when the scope ends.

    I don’t want the thread to have to care that the dependencies to the implementation of ICarsRepository it are disposable

    Your thread should care about this, but your business logic shouldn’t. When starting up new threads, you will have to have some infrastructure code that allows starting and ending a scope, and resolving and using the root type of the graph. This code should be part of your composition root, so the rest of the application should be oblivious about this. If you registered some types with a Per Lifetime Scope (or some other explicit lifetime), your container will know when to dispose the instances. The infrastructure code only has to tell the container, that the scope has ended.

    I don’t want to make SqlCarsRepository disposable

    The SqlCarsRepository should depend on an interface that does not implement IDisposable, and in that case there is nothing to dispose. It should be the container responsible of disposing the DataContext, and with the proper registration you can do this.

    What is the best way to do what I am attempting?

    Your design sounds reasonable, but here are some other SO questions, that might give you more to work with:

    • One DbContext per web request…why?
    • Dependency injection in thread that create objects
    • Working with DI in multi-threaded applications.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As a simplified example, I have the following data classes: public class Employee {
I have a Grid that looks something like the simplified example showed below. There
OK, below is a simplified example of what I have to do. So far
I've simplified my JavaScript code to the example below; this code is giving me
Here's a simplified example of an object I have. What I would like to
I have some fairly generic code which uses preprocessor macros to add a certain
I have placed below the simplest example I could come up with to demonstrate
I have a page with more than one form. One form (see below example)
Below is a simplified example of a stored procedure I've created. DELIMITER // CREATE
I have an XML file which contains lists of stores, a simplified version is

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.