-1

I am newbie in asp.net mvc. I heard the word ASP.NET MVC generic controller, can anyone easily explain what it is? I have worked with the default controller before but now I want to able to visualize the kind of purpose ASP.NET MVC generic controller does. It will be very helpful if some one can explain the situations when a developer has to think about using ASP.NET MVC generic controller. Concepts and code about how to implement it will be greatly appreciated. Thanks

chridam
  • 100,957
  • 23
  • 236
  • 235
Thomas
  • 33,544
  • 126
  • 357
  • 626

1 Answers1

10

You usually create a generic class to abstract away operations you can perform on a range of types, for example Entity Framework models containing an ID. In that case you can move all duplicate code into a base class.

For an MVC controller, a generic base controller may look like this:

public abstract class GenericController<T> 
    where T : class
{
    public virtual ActionResult Details(int id)
    {
        var model = _repository.Set<T>().Find(id);
        return View(model);
    }
}

And an implementation like this:

public class FooController : GenericController<Foo>
{

}

Now when someone requests /Foo/Details/42, the entitiy is pulled from the _repository's Set<Foo>(), without having to write anything for that in the FooController.

This way you can create a basic "CRUD" controller that lets you easily extend your application with Create, Read, Update and Delete operations for new models.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Where does `e.ID` comes from? You should use a class with an `ID` property (`BaseEntity` for example) as type constraint in stead of just `class`. Or do you mean the `Find(object id)` method of Entity Framework? Then it's just `Find(id)`. – Henk Mollema Nov 15 '13 at 12:46
  • @Henk it was more of an example of the principle than ready to copy-paste code, but you're right at both points. Have simplified, thanks. :) – CodeCaster Nov 15 '13 at 12:51
  • I understand. Just saying in case someone actually treats it as copy-paste code and to prevent confusion. ;) – Henk Mollema Nov 15 '13 at 12:53
  • @CodeCaster : thanks for reply. it will be a problem suppose i have product & customer controller which extend GenericController and if request come like /product/Details/42 or /customer/Details/40 then same Details action method will be invoked and a wired result will be return to client. so guide me how to handle this situation with GenericController. – Mou Nov 15 '13 at 18:39
  • Why do you think a "wired" (do you mean weird?) result will be returned? For example Entity Framework's `DbContext.Set()` method will return the set for the entity of type `Entity`. They can do this because each entity can only be used once as a `DbSet` per context, so `ProductController : GenericController` will call `yourContext.Set()`, which will return the same as `yourContext.Products`. – CodeCaster Nov 15 '13 at 18:42
  • suppose if i do not use entity framework rather if i use MS data access application block then how to detect request comes from which controller and accordingly populate model.please guide. thanks – Mou Nov 15 '13 at 19:18
  • @Mou that depends on how you use it, but I'm sure there's a method like `Database.ExecuteSqlStringAccessor(String)`, which you can use in the same way. – CodeCaster Nov 15 '13 at 19:22