What is the best way to wrap dbContext for DI?
I am thinking something like the following may work ok for injecting
dbcontext via constructor to my service layer.... Does anyone out there
have a better way?
public interface IContextFactory:IDisposable
{
DbContext Create();
}
public class ContextFactory<TContext> : IContextFactory where TContext :
DbContext, new()
{
private DbContext _context;
public DbContext Create()
{
_context = new TContext();
_context.Configuration.LazyLoadingEnabled = true;
return _context;
}
public void Dispose()
{
_context.Dispose();
}
}
No comments:
Post a Comment