How to use ASP.NET MVC Generic Controller to populate right model

后端 未结 3 875
醉话见心
醉话见心 2020-12-08 12:12

I asked a question about ASP.NET MVC Generic Controller and this answer shows a controller like this:

public abstract class GenericController<         


        
3条回答
  •  轮回少年
    2020-12-08 12:48

    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.Set() method, which returns the DbSet for the appropriate entity, in this case DbSet which you can query.

    That being said, there are many problems with actually using this code:

    • You don't want to let your controller know about your data access. As you see, a YourEFContext property is used in the controller, tightly coupling it with Entity Framework. You'll want to abstract this away in a repository pattern.
    • You don't want your controller to instantiate its data access, this should be injected.
    • You don't want your controller to return database entities. You're looking for ViewModels and a Mapper.
    • You don't want your controller to do data access. Move the data access in a service layer that also contains your business logic, abstract it again through 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.

提交回复
热议问题