ASP.NET MVC - where to post database queries

I am trying to learn ASP.NET MVC 4, and I am confused about where I should put my database queries. I have experience working in PHP, in particular CodeIgniter, where I use to place all db requests in the model. I have this query:

db.Orders.Where(order => order.columnName == columnName).SingleOrDefault(); 

Based on this ASP.NET tutorial , I have to put it in a controller. However, I found this question, https://stackoverflow.com/a/166268/2126 , which says I should put it in a model (similar to what I did in PHP). Another answer to the same question is about creating a repository template / class that contains all the requests.

So my question is: what are the advantages and disadvantages of the following parameters in terms of code maintenance (readability, effect of changes, etc.)?

  • Queries in controllers
  • Requests in Models
  • Queries in a separate class / layer
+7
asp.net-mvc asp.net-mvc-4
source share
2 answers

An easy way to handle this is with the repository template. This is not the best way to do this. But you will give you an idea of ​​how you can handle this repository template.

create a repository to execute all db transactions

 public interface IRepository { Order GetOrder(int orderId); } public class Repository : IRepository { YourDBContext db; public Repository() { db = new YourDBContext (); } public User GetOrder(int orderId) { return db.Orders.FirstOrDefault(s=>s.OrderID==orderId); } } 

You can create this in the same project (in the "Data Access Logic" section) or create a separate class library for it (and link to it wherever you use it).

And now in your controller, after importing the necessary namespaces, just create an object of your repository and call the method that interests you

 public OrderController :Controller { protected IRepository repo; public OrderController() { repo=new Repository(); } public OrderController(IRepository repositary) { // This constructor is for your Unit test project, // you can pass a mock repository here // Read dependency injection repo=repository; } public ActionResult details(int id) { var order=repo.GetOrder(id); if(order!=null) { return View(order); } } } 

You might consider using a presentation model if you think your presentation needs it. in this case, you need to read the property values ​​from your domain object and install an instance of your view model and return it to your view.

You can move the code to different classes / layers / projects as your code / functionality grows.

+4
source share

from my point of view, what is called Model in MVC is not a database model, but a class with all the information needed to create a view. This means that the class is completely disconnected from the database and has a different structure with different information.

The controller must check the input criteria (query parameters) to send it to the class that retrieves the information (call its repository, if you want), get the data and move it to the model.

Of course, if you want to add a request to the controller, you can, but it will be more difficult to check the controller. A different approach might be easier (you are mocking the repository class / interface and you are ready).

Byez .u

0
source share

All Articles