Question

I am completely new to MVC architecture and having some doubts regarding the architecture and they are mainly due to not using entity framework, instead i've been using Data access with datatables and datasets to fetch data to the application. I would like to know the best practices regarding MVC pattern in case if someone can help out with certain links or pdfs(without entity framework). One more thing i would like to know and that is, from where do we call the DAL methods that obtain data from the database? from the Model classes or from the Controller Actions?

Was it helpful?

Solution

This is a brief demo of how one would implement data access code using MVC. Note that data access typically occurs in the controller action, not the model as someone indicated above:

[HttpPost]
public ActionResult Update(MyModel model){
    //You call your data access code here and retrieve your entity
    DataAccessObject dao = new DataAcessObject();
    var entity = dao.Get(model.Id);
    //Now that you have your entity, update its properties from the model that
    //has been posted to this action
    entity.Name = model.Name;
    entity.Phone = model.Phone;
    //Once your entity has been updated, pass it to the Update method of the DAO
    dao.Update(entity);
}

There are plenty of ways to skin this cat, but something like the above should give you the idea. And unless your application is trivially small, you should look at implementing the Repository pattern to decouple your UI and data layers.

OTHER TIPS

MVC Pattern good practice: Views: Should be pure HTML and no logic Controller: This is the HTTP handler and should not contain business logic but only presentation logic (IF conditions for display etc). It is not aware of where data comes from or how data is obtained. It is only aware of the Model objects Model: Represents data and its access. Model should access database and get data and populate object which controller can then use to pass to View.

EntityFramework: not related to MVC and hence when you use EntityFramework within MVC project you may not see the the good practices mentioned followed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top