views:

11

answers:

1

Hello, my app has a lot of support tables that requires admin UI interface to maintain - for ex: Countries, States, Cities etc. I am not sure if it would make sense to add CRUD actions for each of them in one AdminController or create a Controller for each individual support table.

A: 

If your code is highly repetative and you've got the proper data layer abstractions in place (eg, repository pattern, dependency injection) you could create a generic admin controller and subtype it.

public abstract class AdminController<T> : Controller
{
   protected IRepository<T> _Repository;

   protected AdminController(IRepository<T> repo)
   {
       _Repository = repo;
   }

   public ViewResult List()
   {
       var all = _Repository.GetAll();  // todo paging, sorting, etc -- I'd use a service layer to do this
       return View(all);
   }

   public ViewResult Edit(int id)
   {
      var item = _Repository.GetById(id);
      return View(item);
   }
   // and so on...
}

Then the actual controllers:

public class PostController : AdminController<Post>
{
   public PostController(IRepository<Post> repo) : base(repo) {}
}

Of course you'll still need to make all the views.

Ryan
Ryan, I think that's exactly what I am trying to do - although I have to understand generics a bit more to be able to implement it. User bzlm posted a link to an article that talks about fat controllers which is kinda the direction I am looking for as well. The best practice would be to keep the controllers skinny and models fat and since a lot of CRUD actions are similar, use your abstraction solution. If I am not imposing too much, do you happen to have some working code / link to blog where you explained it so I can try it out myself? Still trying to get my hands / head around MVC2...!
uberDesi