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?
-
3MVC and the Entity Framework are completely unrelated, you do not need to use EF if you don't want to. For data access, you might be interested in the Repository Pattern, which provides an abstraction over your data access objects which you typically call from your MVC actions.Mister Epic– Mister Epic2013-09-21 13:15:27 +00:00Commented Sep 21, 2013 at 13:15
-
3Datasource access is done from the Model, not the Controllers (often trough a repository pattern).Jeroen Vannevel– Jeroen Vannevel2013-09-21 13:16:21 +00:00Commented Sep 21, 2013 at 13:16
-
@ChrisHardie i have not been using any repository pattern.. what i have got is a class that has certain methods which accesses the datasource and returns datasets etc. I believe i need to call these methods in the relevant actions. i do not have any knowledge about the repository pattern which i believe i need to explore. thanks!Aritra B– Aritra B2013-09-21 13:47:20 +00:00Commented Sep 21, 2013 at 13:47
2 Answers
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.
3 Comments
DbContext, not the model.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.