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?