はい、UnitOfWork.cs
クラス内で実行する必要があります。その後
UnitOfWork.cs
public sealed class UnitOfWork : IUnitOfWork
{
private DbContext _dbContext;
public UnitOfWork(DbContext context)
{
_dbContext = context;
}
public int Commit()
{
return _dbContext.SaveChanges();// Save changes
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing)
{
if (_dbContext != null)
{
_dbContext.Dispose();
_dbContext = null;
}
}
}
}
あなたはサービス層内のUnitOfWorkのCommit
メソッドを呼び出すことができます。
EntityService.cs
public abstract class EntityService<T> : IEntityService<T> where T : BaseEntity
{
IUnitOfWork _unitOfWork;
IGenericRepository<T> _repository;
public EntityService(IUnitOfWork unitOfWork, IGenericRepository<T> repository)
{
_unitOfWork = unitOfWork;
_repository = repository;
}
public virtual void Create(T entity)
{
_repository.Add(entity);
_unitOfWork.Commit(); //saving point
}
}
あなたはこのパターンについての詳細を学ぶことができます:Generic Repository and Unit of Work Pattern
私はSOのスレッドの数十は、異なる意見を持つことに既に存在しているとして、これを閉じるために投票しています。 –