7

I am trying to learn ASP.NET MVC 4, and I am confused as to where I should put my database queries. I have background in PHP, particularly CodeIgniter, where I am used to putting all db queries in the model. I have this query:

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

Based on this ASP.NET tutorial, I should put it in the controller. However, I found this StackOverflow question, which says that I should put it in the model (similar to what I used to do in PHP). Another answer to that same question mentioned about creating a repository pattern/class that contains all queries.

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

  1. Queries in controllers
  2. Queries in models
  3. Queries in a separate class/layer
1
  • 1
    the setup we use is our data is defined in the model, our queries are defined in a class we call service and the controller handles everything. so on the controller, create a new model, then call the service to populate the model, then pass that populated model to the view Commented Jan 15, 2014 at 16:53

2 Answers 2

4

The simple way to handle this with a repository pattern. This is not the best way to do it. But will give you an idea how you can handle this with the repository pattern.

create a repository to do all your 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 may create this in the same project (under a "Data access logic") or create a separate class library for this (and refer it to 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 you are interested in

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 may consider using a view-model if think your view need it. in that case you need to read the property values from your domain object and set it the the instance of your view-model and return that to your view.

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

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

Comments

0

from my point of view what is called Model in MVC is not the model of the database but the class with all info you need to create the view. It means the class is totally disconnected from the database and has a different structure with different info.

The controller must validate the input criteria (parameters of the request) send it to class that retrieve the info (call it repository if you prefer), get the data and move it into a model.

Of course if you want to add the query into the controller you can, but will be more difficult to test the controller. With the other approach could be easier (you mock the class/interface of the repository and you are ready).

Byez .u

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.