Usually you create a general class for abstract operations that you can perform in different types, for example, Entity Framework models that contain an identifier. In this case, you can move all the duplicated code to the base class.
For an MVC, a common base controller might 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 the implementation is this:
public class FooController : GenericController<Foo> { }
Now, when someone asks for /Foo/Details/42 , entitiy is extracted from _repository Set<Foo>() , without having to write anything for this in FooController .
In this way, you can create a basic “CRUD” controller that allows you to easily expand your application using the Create, Read, Update, and Delete operations for new models.
source share