C# 如何使用EntityFramework为CRUD操作实现通用MVC web控制器和通用视图
我正在设计一个.NETMVCWeb应用程序,我已经决定要管理33个表(到目前为止)。所谓管理,我指的是典型的SQL操作(插入、更新、删除和查询) 在这33个表中,有16个是需要用典型积垢处理的参考表。我所说的参考表是指被其他表引用并在组合中使用的表(即国家、州、城市、货币等) 我对.NET中的MVC非常陌生,但我已经看到,当你搭建脚手架时,你会得到具有典型方法的控制器:C# 如何使用EntityFramework为CRUD操作实现通用MVC web控制器和通用视图,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我正在设计一个.NETMVCWeb应用程序,我已经决定要管理33个表(到目前为止)。所谓管理,我指的是典型的SQL操作(插入、更新、删除和查询) 在这33个表中,有16个是需要用典型积垢处理的参考表。我所说的参考表是指被其他表引用并在组合中使用的表(即国家、州、城市、货币等) 我对.NET中的MVC非常陌生,但我已经看到,当你搭建脚手架时,你会得到具有典型方法的控制器: 索引(get) 详细信息(获取) 创建(获取+发布) 编辑(获取+发布) 删除(获取+发布) 在数据库方面,我还计划使用E
- 索引(get)
- 详细信息(获取)
- 创建(获取+发布)
- 编辑(获取+发布)
- 删除(获取+发布)
单一主控制器
来路由或管理所有16个实体,并调用实体所需的操作?
是否可以使用一组通用的视图(创建、删除、详细信息、编辑和索引)来处理相同的16个实体?
如果上述两种方法都可行,我将如何在web项目的配置中连接路由,以便仍然能够为这16个实体中的每一个实体(/Countries/Edit/5、/States/Edit/5等等)提供特定的URL?
我认为有一种方法可以实现这一点:
- 为所有16个实体提供抽象/公共基类或接口,这些实体具有管理EntityFramework上下文(即IManageData)的Save、GetAll和Delete数据库操作逻辑
- 让EntityDao使用EntityFramework调用实现IManageData并执行操作的任何类的方法李>
- 拥有一个抽象/公共基类(web控制器),其中包含所有16个实体的CRUD操作的公共逻辑,这些实体可以调用IManageData的方法
我在上一个项目中也遇到过类似的情况。我是这样做的。这里是每个实体(表)的通用接口
公共接口IRepository
{
无效添加(T实体、用户发起人);
无效更新(T实体、用户发起人);
无效删除(T实体);
作废删除(int entityId);
IEnumerable GetAll();
T GetById(int entityId);
}
这个通用数据层可以通过实体框架(例如底部的代码示例)或使用不同的方法(如我的示例中的存储过程)轻松实现
我实际上没有任何额外的业务逻辑,所以我的web项目可以直接访问数据存储库。所以它是我的通用基本控制器
public abstract class BaseController<T, M> : Controller
where M : new()
{
public BaseController(IRepository<T> repository)
{
this._repository = repository;
ViewBag.CurrentUser = CurrentUser;
}
protected User CurrentUser
{
get
{
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
return Mapper.Map<User>(System.Web.HttpContext.Current.User.Identity);
}
return null;
}
}
protected virtual int PageSize
{
get { return 5; }
}
protected IRepository<T> _repository;
public virtual ActionResult Index(int? currentPage)
{
var entities = _repository.GetAll();
List<M> model = new List<M>();
foreach (var currentEntity in entities)
{
model.Add(Mapper.Map<M>(currentEntity));
}
int pageNumber = (currentPage ?? 1);
return View(model.ToPagedList(pageNumber, PageSize));
}
[HttpGet]
public virtual ActionResult Add()
{
return View(new M());
}
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public virtual ActionResult Add(M model)
{
if (ModelState.IsValid)
{
_repository.Add(Mapper.Map<T>(model), CurrentUser);
return RedirectToAction("Index");
}
return View(model);
}
[HttpGet]
public virtual ActionResult Update(int modelId)
{
T domainModelEntity = _repository.GetById(modelId);
M model = Mapper.Map<M>(domainModelEntity);
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public virtual ActionResult Update(M model)
{
if (ModelState.IsValid)
{
_repository.Update(Mapper.Map<T>(model), CurrentUser);
return RedirectToAction("Index");
}
return View(model);
}
public virtual ActionResult Delete(int modelId)
{
_repository.Delete(modelId);
return RedirectToAction("Index");
}
}
public class WebPagesController : BaseWebEntityController<WebPage, WebPageModel>
{
public WebPagesController(IRepository<WebPage> repository)
: base(repository)
{
}
}
公共抽象类BaseController:控制器
其中M:new()
{
公共BaseController(IRepository存储库)
{
这个。_repository=repository;
ViewBag.CurrentUser=当前用户;
}
受保护用户当前用户
{
得到
{
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
返回Mapper.Map(System.Web.HttpContext.Current.User.Identity);
}
返回null;
}
}
受保护的虚拟整型页面大小
{
获取{return 5;}
}
受保护的IRepository存储库;
公共虚拟操作结果索引(int?currentPage)
{
var entities=_repository.GetAll();
列表模型=新列表();
foreach(实体中的var currentEntity)
{
Add(Mapper.Map(currentEntity));
}
int pageNumber=(当前第1页);
返回视图(model.ToPagedList(pageNumber,PageSize));
}
[HttpGet]
公共虚拟操作结果添加()
{
返回视图(新的M());
}
[HttpPost]
[ValidateAntiForgeryToken]
[验证输入(错误)]
公共虚拟操作结果添加(M模型)
{
if(ModelState.IsValid)
{
_Add(Mapper.Map(model),CurrentUser);
返回操作(“索引”);
}
返回视图(模型);
}
[HttpGet]
公共虚拟操作结果更新(int modelId)
{
T domainModelEntity=\u repository.GetById(modelId);
M model=Mapper.Map(domainModelEntity);
返回视图(模型);
}
[HttpPost]
[ValidateAntiForgeryToken]
[验证输入(错误)]
公共虚拟操作结果更新(M模型)
{
if(ModelState.IsValid)
{
_repository.Update(Mapper.Map(model),CurrentUser);
返回操作(“索引”);
}
返回视图(模型);
}
公共虚拟操作结果删除(int modelId)
{
_repository.Delete(modelId);
返回操作(“索引”);
}
}
这个特定的控制器
public abstract class BaseController<T, M> : Controller
where M : new()
{
public BaseController(IRepository<T> repository)
{
this._repository = repository;
ViewBag.CurrentUser = CurrentUser;
}
protected User CurrentUser
{
get
{
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
return Mapper.Map<User>(System.Web.HttpContext.Current.User.Identity);
}
return null;
}
}
protected virtual int PageSize
{
get { return 5; }
}
protected IRepository<T> _repository;
public virtual ActionResult Index(int? currentPage)
{
var entities = _repository.GetAll();
List<M> model = new List<M>();
foreach (var currentEntity in entities)
{
model.Add(Mapper.Map<M>(currentEntity));
}
int pageNumber = (currentPage ?? 1);
return View(model.ToPagedList(pageNumber, PageSize));
}
[HttpGet]
public virtual ActionResult Add()
{
return View(new M());
}
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public virtual ActionResult Add(M model)
{
if (ModelState.IsValid)
{
_repository.Add(Mapper.Map<T>(model), CurrentUser);
return RedirectToAction("Index");
}
return View(model);
}
[HttpGet]
public virtual ActionResult Update(int modelId)
{
T domainModelEntity = _repository.GetById(modelId);
M model = Mapper.Map<M>(domainModelEntity);
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public virtual ActionResult Update(M model)
{
if (ModelState.IsValid)
{
_repository.Update(Mapper.Map<T>(model), CurrentUser);
return RedirectToAction("Index");
}
return View(model);
}
public virtual ActionResult Delete(int modelId)
{
_repository.Delete(modelId);
return RedirectToAction("Index");
}
}
public class WebPagesController : BaseWebEntityController<WebPage, WebPageModel>
{
public WebPagesController(IRepository<WebPage> repository)
: base(repository)
{
}
}
公共类WebPagesController:BaseWebEntityController
{
公共网页管理员(IRepository存储库)
:base(存储库)