I want to reorganize my basic CRUD operations as they are very repetitive, but I'm not sure if this is the best way. All my controllers inherit a BaseController that looks like this:
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()); } }
I create such new controllers:
public class ForumController : BaseController<Forum> { }
It's nice and easy, and as you can see, my BaseController contains an Index() method, so my controllers have an Index method and will load their respective views and data from the repository - this works fine. I am struggling for editing / adding / deleting methods, my Add method in my repository looks like this:
public T Add(T Entity) { Table.AddObject(Entity); SaveChanges(); return Entity; }
Again, nice and easy, but in my BaseController I obviously can't:
public ActionResult Create(Category Category) { Repository.Add(Category); return RedirectToAction("View", "Category", new { id = Category.Id }); }
as I usually like it: any ideas? My brain, it seems, can not get past this ...; - /
source share