MVC5 + EF + UOW +服务层,在其中调用SaveChanges()并避免多次访问DB
本文关键字:DB 访问 SaveChanges 服务 UOW EF 调用 在其中 MVC5 | 更新日期: 2023-09-27 18:11:53
我正在使用:MVC5和EF Code First与Repository和Unit of Work模式构建web应用程序。到目前为止,我有3个图层:
- 包含存储库的"数据层",UOW。
- "服务层",引用UOW实现业务逻辑和业务验证。
- "Web层",负责通过与服务层通信来显示数据。
我的域/业务对象在另一个项目中分离。所以基本上我是遵循John Papa的CodeCamper结构,除了添加了"服务层"。
数据/合同/IRepository.cs
public interface IRepository<T> where T : class
{
IQueryable<T> GetAll();
T GetById(int id);
void Add(T entity);
void Update(T entity);
void Delete(T entity);
void Delete(int id);
}
数据/合同/IUnitOfWork.cs
public interface IUnitOfWork
{
// Save pending changes to the data store.
void Commit();
// Repositories
IRepository<Event> Events { get; }
IRepository<Candidate> Candidates { get; }
}
数据/EFRepository.cs
/// <summary>
/// The EF-dependent, generic repository for data access
/// </summary>
/// <typeparam name="T">Type of entity for this Repository.</typeparam>
public class EFRepository<T> : IRepository<T> where T : class
{
public EFRepository(DbContext dbContext)
{
if (dbContext == null)
throw new ArgumentNullException("dbContext");
DbContext = dbContext;
DbSet = DbContext.Set<T>();
}
protected DbContext DbContext { get; set; }
protected DbSet<T> DbSet { get; set; }
public virtual IQueryable<T> GetAll()
{
return DbSet;
}
public virtual T GetById(int id)
{
//return DbSet.FirstOrDefault(PredicateBuilder.GetByIdPredicate<T>(id));
return DbSet.Find(id);
}
public virtual void Add(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State != EntityState.Detached)
{
dbEntityEntry.State = EntityState.Added;
}
else
{
DbSet.Add(entity);
}
}
public virtual void Update(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State == EntityState.Detached)
{
DbSet.Attach(entity);
}
dbEntityEntry.State = EntityState.Modified;
}
public virtual void Delete(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State != EntityState.Deleted)
{
dbEntityEntry.State = EntityState.Deleted;
}
else
{
DbSet.Attach(entity);
DbSet.Remove(entity);
}
}
public virtual void Delete(int id)
{
var entity = GetById(id);
if (entity == null) return; // not found; assume already deleted.
Delete(entity);
}
}
数据/UnitOfWork.cs
/// <summary>
/// The "Unit of Work"
/// 1) decouples the repos from the controllers
/// 2) decouples the DbContext and EF from the controllers
/// 3) manages the UoW
/// </summary>
/// <remarks>
/// This class implements the "Unit of Work" pattern in which
/// the "UoW" serves as a facade for querying and saving to the database.
/// Querying is delegated to "repositories".
/// Each repository serves as a container dedicated to a particular
/// root entity type such as a <see cref="Url"/>.
/// A repository typically exposes "Get" methods for querying and
/// will offer add, update, and delete methods if those features are supported.
/// The repositories rely on their parent UoW to provide the interface to the
/// data layer (which is the EF DbContext in this example).
/// </remarks>
public class UnitOfWork : IUnitOfWork, IDisposable
{
public UnitOfWork(IRepositoryProvider repositoryProvider)
{
CreateDbContext();
repositoryProvider.DbContext = DbContext;
RepositoryProvider = repositoryProvider;
}
// Repositories
public IRepository<Student> Students { get { return GetStandardRepo<Event>(); } }
public IRepository<Course> Courses { get { return GetStandardRepo<Course>(); } }
/// <summary>
/// Save pending changes to the database
/// </summary>
public void Commit()
{
//System.Diagnostics.Debug.WriteLine("Committed");
DbContext.SaveChanges();
}
protected void CreateDbContext()
{
DbContext = new UnicornsContext();
// Do NOT enable proxied entities, else serialization fails
DbContext.Configuration.ProxyCreationEnabled = false;
// Load navigation properties explicitly (avoid serialization trouble)
DbContext.Configuration.LazyLoadingEnabled = false;
// Because Web API will perform validation, I don't need/want EF to do so
DbContext.Configuration.ValidateOnSaveEnabled = false;
}
protected IRepositoryProvider RepositoryProvider { get; set; }
private IRepository<T> GetStandardRepo<T>() where T : class
{
return RepositoryProvider.GetRepositoryForEntityType<T>();
}
private T GetRepo<T>() where T : class
{
return RepositoryProvider.GetRepository<T>();
}
private UnicornsContext DbContext { get; set; }
#region IDisposable
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (DbContext != null)
{
DbContext.Dispose();
}
}
}
#endregion
}
,然后最后使用Ninject来解析依赖:
kernel.Bind<RepositoryFactories>().To<RepositoryFactories>().InSingletonScope();
kernel.Bind<IRepositoryProvider>().To<RepositoryProvider>();
kernel.Bind<IUnitOfWork>().To<UnitOfWork>();
我应该在哪里调用UOW.Commit()
,以便我可以在其他服务中重用特定服务的实现逻辑,而不是重新编写它?
- 控制器应该负责提交变更吗? 或者服务本身应该提交更改?
就我在Stack Overflow上读到的,选项(1)更简单,但违反了单一责任原则,或者如果我想集成移动/桌面应用程序的话。
选项(2):这里我必须在每个服务函数调用中调用commit,因此我将无法重用函数,因为这可能导致多次访问DB
在我的项目中,我在控制器中调用Save(),因为我想尽可能多地重用我的方法,并且当它们调用Save()自己时,将多个方法"粘合"在一起是困难的。更糟糕的是,如果在控制器级别上有错误,你可能想要避免调用save(),例如,一些方法创建了一堆小对象,这些对象在必须提交的主对象中使用。同时提交两者似乎比在createSMallObject()和createMasterObject()中进行硬提交更好,以防两者之间发生某些事情。