I want to implement the Unit of Work pattern in ASP.NET EF Core.
Now, my question is whether to use the DBContextFactory or DbContext instance in the Unit of Work constructor. Which approach is better and why?
Code sample:
use contextFactory:
public sealed class UnitOfWork : IUnitOfWork
{
private AppDbContext _dbContext;
public UnitOfWork(IContextFactory contextFactory)
{
_dbContext = contextFactory.DbContext;
}
}
use context:
public sealed class UnitOfWork : IUnitOfWork
{
private AppDbContext _dbContext;
public UnitOfWork(AppDbContext dbContext)
{
_dbContext = dbContext;
}
}
While Entity Framework’s DbContext can be considered a unit of work in the context of managing database operations, I want to implement my own Unit of Work pattern for managing feature plans.