I'm using UnitOfWork in my asp.net 5 project as shown below:
public class UnitOfWork : IUnitOfWork
{
private readonly BaseContext _context;
private IAsyncRepository<CategoryEntity> _categoryRepository;
private IAsyncRepository<ItemEntity> _itemRepository;
public UnitOfWork(BaseContext context)
{
_context = context;
}
public IAsyncRepository<CategoryEntity> CategoryRepository
{
get
{
return _categoryRepository ??= new CategoryRepository(_context);
}
}
public IAsyncRepository<ItemEntity> ItemRepository
{
get
{
return _itemRepository ??= new ItemRepository(_context);
}
}
}
Is there any way to lazy inject my CategoryRepository : IAsyncRepository or ItemRepository : IAsyncRepository using dependency injection in such a way that it will only be instantiated only when I'm accessing the particular repository and also same DbContext needs to be shared between Repositories? This could help to remove the tight coupling. Please assist.