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)?
From the Add New Item window, select ADO.NET Entity Data Model and set its Name as NorthwindModel and then click Add. Then the Entity Data Model Wizard will open up where you need to select EF Designer database option. Now the wizard will ask you to connect and configure the Connection String to the database.
ASP.NET MVC provides three ways (TempData, ViewData and ViewBag) to manage session, apart from that we can use session variable, hidden fields and HTML controls for the same.
The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.
Let's add one record from the browser by clicking the 'Create New' link. It will display the Create view. Let's add some data in the following field. Click on the Create button and it will update the Index view as well add this new record to the database.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With