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)?
- Queries in controllers
- Queries in models
- Queries in a separate class/layer