我想重構我的基本CRUD操作,因爲它們是非常重複的,但我不確定最好的方式去實現它。我所有的控制器繼承BaseController它看起來像這樣:處理CRUD操作的MVC BaseController
public class BaseController<T> : Controller where T : EntityObject
{
protected Repository<T> Repository;
public BaseController()
{
Repository = new Repository<T>(new Models.DatabaseContextContainer());
}
public virtual ActionResult Index()
{
return View(Repository.Get());
}
}
我創造新的控制器,就像這樣:
public class ForumController : BaseController<Forum> { }
尼斯和容易,因爲你可以看到我BaseController
包含Index()
方法,這樣就意味着我的控制器都有一個Index方法,並且會從存儲庫加載它們各自的視圖和數據 - 這完美地工作。我掙扎在編輯/添加/刪除方法,我在我的倉庫Add
方法是這樣的:
public T Add(T Entity)
{
Table.AddObject(Entity);
SaveChanges();
return Entity;
}
同樣,好和容易的,但在我的BaseController
我顯然無法做到:
public ActionResult Create(Category Category)
{
Repository.Add(Category);
return RedirectToAction("View", "Category", new { id = Category.Id });
}
正如我通常會這樣:任何想法?我的大腦似乎無法獲得通過該..; -/
我試過這個,但它似乎沒有工作;我得到編譯器錯誤,如'錯誤類型'Models.Category'不能用作通用類型或方法'Controllers.BaseController'中的類型參數'T'。沒有從'Models.Category'到'System.Data.Objects.DataClasses.EntityObject'的隱式引用轉換。'和'錯誤\t'Models.User'類型不能用作類型參數'T'泛型類型或方法'控制器。BaseController '。沒有從'Models.User'到'Classes.Core.IEntity'的隱式引用轉換。' 我正在使用EF4。 –
eth0
2011-03-18 22:45:33
你需要讓你的實體實現IEntity。也就是說,定義「class Category:IEntity」並在其上實現ID屬性。 – 2011-03-18 22:47:17
如果您使用的是EF,那麼最好的方法是添加一個「public partial class Category:IEntity」部分類並添加ID屬性。一定要在與實體類相同的名稱空間中定義它。 – 2011-03-18 22:49:07