I asked a question about ASP.NET MVC Generic Controller and this answer shows a controller like this:
public abstract class GenericController<
Given my disclaimers in the other question and my comments here explaining why this isn't an ultimate solution, I'll try to give a more concrete implementation:
public abstract class GenericController : Controller
where T : class
{
protected YourEFContext _dataSource;
public GenericController()
{
_dataSource = new YourEFContext();
}
public virtual ActionResult Details(int id)
{
var model = _dataSource.Set().Find(id);
return View(model);
}
}
public class CustomerController : GenericController
{
}
This is all code that is needed to let /Customers/Details/42
load customer with ID 42
be loaded from the Entity Framework context. The "generic" part is solved by Entity Framework's DbContext.SetDbSet
for the appropriate entity, in this case DbSet
which you can query.
That being said, there are many problems with actually using this code:
YourEFContext
property is used in the controller, tightly coupling it with Entity Framework. You'll want to abstract this away in a repository pattern.Now your question actually is "Does the Enterprise Library Data Block have a method like GetDataSet
", so you don't have to refer to customer
and product
in your generic controller, but unfortunately I can't find that as I haven't used EntLib for a few years. It would help if you show the code you currently use to access your database.
The ultimate goal you're looking for:
[ MVC ] <=> [ Service ] <=> [ Repository ]
View ViewModel Controller BusinessModel BusinessLogic DataModel Database
Your controller only talks to your service to Create/Read/Update/Delete BusinessModels, and performs the mapping between ViewModels and BusinessModels (at <=>
). The service contains the business logic and business model (DataContacts when using WCF) and in turn maps (<=>
) to and from DataModels and talks to your repository to persist your models.
I understand this can be a bit much to grasp at once, and that's probably why most ASP.NET MVC tutorials start with all three tiers in one application. Take a look at ProDinner for a more proper approach.