我正在开发一个MVC项目。在这我有很多表像Location
,Department
,Skills
,Stream
,CurrentRole
等如何在控制器中编写通用方法来在不同的表上执行CRUD操作?
我想对这些表执行CRUD操作。
我所做的是我写了一些方法来执行Location
表上的操作。有4种方法如下。
添加位置:
public ActionResult AddLocation(tblCurrentLocation location)
{
//Logic to add location to the database.
}
编辑位置:
public ActionResult EditLocation(string id)
{
//Logic to display Edit form...
}
保存编辑后的数据:
[HttpPost]
public ActionResult SaveLocation(tblCurrentLocation tblCurrentLocation)
{
//Logic to update the data in database.
//This is POST method which will get called when user clicks
//save after editing the data.
}
,这是删除数据库中的项
public ActionResult DeleteLocation(int id)
{
//Logic to delete row from database.
}
如果我遵循这种方法并写下所有的方法(约。 16)表将会像我的控制器中的50多种方法那样难以维护。
我在找的是我会写的通用CRUD方法,它将能够接受所有我的表的数据并执行操作。
正在写通用方法解决方案?如果是,那我该如何实现呢?
有没有其他方法可以实现这个目标?
请帮助..
谢谢:)
泛型方法将是我的第一选择,但与一般的仓库结合。这里的后面有很多例子。你可以从那里开始。 –
@AndreiV你能举一些例子吗? 或链接... 谢谢 –