DbContext 就是工作单元,因其内置变更追踪、原子提交、事务一致性等能力;仅当需解耦ORM、共享上下文、统一事务或便于Mock时,才需额外抽象IUnitOfWork接口。
EF Core 本身已经内置了工作单元(Unit of Work)模式的核心行为——DbContext 就是一个天然的工作单元。它自动跟踪实体状态、批量提交变更、保证事务一致性,无需额外封装即可直接使用。所谓“实现工作单元模式”,更多是按需进行合理抽象和组织,而非从零造轮子。
DbContext 在生命周期内:
当你有以下需求时,可考虑封装一层 UoW 接口:
注意:过度抽象可能增加复杂度,小项目通常直接用 DbContext 更清晰。
定义接口:
public interface IUnitOfWork : IDisposable
{
DbContext Context { get; }
Task SaveChangesAsync(CancellationToken cancellationToken = default);
void BeginTransaction();
void Commit();
void Rollback();
}
实现类(复用现有 DbContext):
public class UnitOfWork : IUnitOfWork
{
private readonly DbContext _context;
private IDbContextTransaction _transaction;
public UnitOfWork(DbContext context) => _context = context;
public DbContext Context => _context;
public async Task SaveChangesAsync(CancellationToken cancellationToken = default)
=> await _context.SaveChangesAsync(cancellationToken);
public void BeginTransaction() =>
_transaction ??= _context.Database.BeginTransaction();
public void Commit()
{
_transaction?.Commit();
_transaction?.Dispose();
_transaction = null;
}
public void Rollback()
{
_transaction?.Rollback();
_transaction?.Dispose();
_transaction = null;
}
public void Dispose() => _transaction?.Dispose();
}
注册为 Scoped 服务(Startup.cs 或 Program.cs):
services.AddScoped(); services.AddDbContext(options => options.UseSqlServer(connectionString));
确保多个 Repository 共享同一个 DbContext(即同一个 UoW):
public class OrderService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IOrderRepository _orderRepo;
private readonly IProductRepository _productRepo;
public OrderService(
IUnitOfWork unitOfWork,
IOrderRepository orderRepo,
IProductRepository pro
ductRepo)
{
_unitOfWork = unitOfWork;
_orderRepo = orderRepo;
_productRepo = productRepo;
}
public async Task PlaceOrderAsync(Order order)
{
_unitOfWork.BeginTransaction();
try
{
await _orderRepo.AddAsync(order);
var product = await _productRepo.GetByIdAsync(order.ProductId);
product.Stock -= order.Quantity;
await _unitOfWork.SaveChangesAsync();
_unitOfWork.Commit();
}
catch
{
_unitOfWork.Rollback();
throw;
}
}
}
关键点:所有仓储构造函数应接收 DbContext 或 IUnitOfWork,而不是自行 new DbContext。
基本上就这些。EF Core 的工作单元不复杂但容易忽略其本质——它不是必须封装的“设计模式”,而是你每天都在用的 DbContext 的自然能力。