3

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?

3
  • 3
    MVC 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. Commented Sep 21, 2013 at 13:15
  • 3
    Datasource access is done from the Model, not the Controllers (often trough a repository pattern). Commented 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! Commented Sep 21, 2013 at 13:47

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Actually, data access is supposed to occur in the Model and not the Controller, that's the whole point of the Model
The purpose of the model is to represent something from your business domain (a Car, a Person, an Order). Persistence is outside the model's single responsibility. If you let Visual Studio generate your controller using an Entity Framework entity as its model, you will see that the data access functions reside in DbContext, not the model.
Or use Dapper, which is a very lightweight bare-bones ORM.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.