在一个模拟解决方案中,我创建了3个模拟项目来实现分层架构设计,主要是表示层(Web) -域模型层-基础结构层。我还没有添加应用程序服务层(Web )。
为了简单起见,我只有两个域模型实体: Customer.cs和Category.cs。在域模型层中,我还放置了存储库接口(包括自定义的和通用的)。
在基础设施层,我有dbcontext、Work (包括它的接口)和存储库类(它们实现了存储库接口)。
下面是我的通用存储库接口
public interface IRepositoryGeneric<TEntity> where TEntity : class
{
// CRD
void Add(TEntity entity);
void Remove(TEntity entity);
TEntity Get(object Id);
IEnumerable<TEntity> GetAll();
void Save();
}
泛型仓库类
public class RepositoryGeneric<T> : IRepositoryGeneric<T> where T : class
{
protected readonly AppDbContext db;
public RepositoryGeneric(AppDbContext db)
{
this.db = db;
}
public void Add(T entity)
{
db.Set<T>().Add(entity);
}
public T Get(object Id)
{
return db.Set<T>().Find(Id);
}
public IEnumerable<T> GetAll()
{
return db.Set<T>().ToList();
}
public void Remove(T entity)
{
db.Set<T>().Remove(entity);
}
public void Save()
{
db.SaveChanges();
}
}
工作单元接口
public interface IUnitOfWork : IDisposable
{
IRepositoryCustomer Customers { get; }
IRepositoryCategory Categories { get; }
int Save();
}
工级单位
public class UnitOfWork : IUnitOfWork
{
private readonly AppDbContext db;
public UnitOfWork()
{
db = new AppDbContext();
}
private IRepositoryCustomer _Customers;
public IRepositoryCustomer Customers
{
get
{
if (this._Customers == null)
{
this._Customers = new RepositoryCustomer(db);
}
return this._Customers;
}
}
private IRepositoryCategory _Categories;
public IRepositoryCategory Categories
{
get
{
if (_Categories == null)
{
_Categories = new RepositoryCategory(db);
}
return _Categories;
}
}
public void Dispose()
{
db.Dispose();
}
public int Save()
{
return db.SaveChanges();
}
}
我了解到,在我的通用存储库接口中不应该有更新方法。但是如果没有更新方法,我现在只能在我的演示MVC web控制器中使用编辑方法。下面,我只提取了编辑操作方法。错误出现在那一行粗体中。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "CategoryID,CategoryName,Description")] Category category)
{
if (ModelState.IsValid)
{
**db.Entry(category).State = EntityState.Modified;**
db.Save();
return RedirectToAction("Index");
}
return View(category);
}
最后,我的类是否在正确的层中创建?例如,IUnitOfWork.cs应该在基础设施层吗?
我的整个解决方案源代码都可以在我的github - https://github.com/ngaisteve1/LayeredArchitectureDesignCSharp上找到。
发布于 2019-06-05 14:28:05
通用存储接口
public interface IRepositoryGeneric<TEntity> where TEntity : class
{
// CRD
void Add(TEntity entity);
void Remove(TEntity entity);
TEntity Get(object Id);
IEnumerable<TEntity> GetAll();
void UpdateState(TEntity entity);
void Save();
}
存储库泛型类
public class RepositoryGeneric<T> : IRepositoryGeneric<T> where T : class
{
protected readonly AppDbContext db;
public RepositoryGeneric(AppDbContext db)
{
this.db = db;
}
public void Add(T entity)
{
db.Set<T>().Add(entity);
}
public T Get(object Id)
{
return db.Set<T>().Find(Id);
}
public IEnumerable<T> GetAll()
{
return db.Set<T>().ToList();
}
public void Remove(T entity)
{
db.Set<T>().Remove(entity);
}
public void UpdateState(T entity)
{
db.Entry(entity).State = EntityState.Modified;
}
public void Save()
{
db.SaveChanges();
}
}
Web控制器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "CategoryID,CategoryName,Description")] Category category)
{
if (ModelState.IsValid)
{
db.Categories.UpdateState(category);
db.Commit();
return RedirectToAction("Index");
}
return View(category);
}
https://softwareengineering.stackexchange.com/questions/392920
复制