【发布时间】:2020-05-28 21:22:43
【问题描述】:
例如我有这个类:
public class UnitOfWork : IUnitOfWork
{
private readonly ApplicationDbContext _context;
public IProductRepository Products { get; private set; }
public ICategoryRepository Categories { get; private set; }
public IPhotoRepository Photos { get; private set; }
public UnitOfWork(ApplicationDbContext context, IProductRepository productRepository,
ICategoryRepository categoryRepository, IPhotoRepository photoRepository)
{
_context = context;
Products = productRepository;
Categories = categoryRepository;
Photos = photoRepository;
}
public int Complete()
{
return _context.SaveChanges();
}
public Task<int> CompleteAsync()
{
return _context.SaveChangesAsync();
}
public void Dispose()
{
_context.Dispose();
}
public async ValueTask DisposeAsync()
{
await _context.DisposeAsync();
}
}
有了这样的接口:
public interface IUnitOfWork : IDisposable, IAsyncDisposable
{
IProductRepository Products { get; }
ICategoryRepository Categories { get; }
IPhotoRepository Photos { get; }
int Complete();
Task<int> CompleteAsync();
}
同时拥有 Async 和 Sync 方法是否正确?例如,如果我在 asp net core 中使用 DI,那么在处理过程中会调用什么方法。
【问题讨论】:
标签: c# asp.net-core dependency-injection idisposable unit-of-work