Friday, October 14, 2011
ASP.NET MVC Example Application over Northwind with the Entity Framework
For this example, you will need:
- VS2008 (Free trial )
- ASP.NET 3.5 Extensions (Update: use lastest bits from http://asp.net/mvc )
- ADO.NET Entity Framework Tools Dec 07 Preview (Update: No longer needed, in VS2008 SP 1)
- Northwind sample database (just northwnd.mdf)
If you are the type that likes to eat your desert first, you can skip to the end and just download the full working solution (Update: download this version of the sample that works with latest bits).
Getting Started
File/New Project - Select ASP.NET MVC Web Application and Test
This creates a single solution with both a web application project as well as a project we can use for unit testing. Both are pre-populated with the basic stuff you need to get going.
One of the powerful new features that ASP.NET MVC brings to the table is the ability to customize the URLs that access your application. The URL routing feature explicitly breaks the connection between physical files on disk and the URL that is used to access a given bit of functionality. This is important for Search Engine Optimization as well as general usability of the website. For example, rather than accesshttp://localhost/Products/ItemDetails.aspx?item=42 you can now very give a pretty URL such ashttp://localhost/Products/CodFishOil
This is done by creating a route table in the global.asax file in the MVC Application. Luckily for us, the defaults included in the template work perfectly this application.
RouteTable.Routes.Add(new RouteThis code spells out the format of the URLs we want to use for our site. In particular, a URL of the format
{
Url = "[controller]/[action]/[id]",
Defaults = new { action = "Index", id = (string)null },
RouteHandler = typeof(MvcRouteHandler)
});
http://localhost/Products/Details/CodFishOil
would translate to the ProductsController class (notice we add the "Controller" suffix to the class name to make these classes stand out in the object model at design time). Then the Action is a method on that class called Details and finally the argument to that details method is CodFishOil.
It is of course possible to have other formats, simply by changing he regular expression in the URL pattern string.
The model represents the data you are going to use in the application. In our case, this is a pretty good place to start the core of the application.
In the App_Data direction of the MVCApplication copy the Northwind.mdf file. Northwind is likely the most common example database we have for SqlServer... You can download it from the official location, or for just the raw file, feel free to grab it from here.
Next, we need to create a LINQ model on top of northwind to make it easier to work with... You can do this withNHibernate, LinqToSql, Entity Framework , or any other .NET OR-Mapping technology. As long as it results in .NET Object, the ASP.NET MVC framework can work with it. In this case I am going to use the Entity Framework.
Right click on the Models directory and select add new item
In the dialog select ADO.NET Entity Data Model.
In the wizard, select "Generate from Database" and then go with the default "Northwnd" connection string.
For our demo, we are only going to use the Categories, Products and Suppliers tables, but you could of course extend this demo to include a richer feature set. But for now, unselect the Views and Stored Procedures and all the tables except those three..
When you click finished, VS will create a set of .NET classes that are custom built for accessing this database and we get a nice designer for visualizing the data relationships.
Notice, that the default names given to these classes still use the plural names from the database, but in our OR-Mapping they represent single instances, so to make the code easier to read, go in and change all the table names to their appropriate singular form: Category, Product and Supplier. Then Navigation properties on Product needs to be singular as there is only one Category and Suppler for each product.
Next we need to cleanup the namespace so things look pretty in our code... Right click on the design surface then set the properties such that namespace is "NorthwindModels" and the Entity Container name to "NorthWindEntities"
Creating the Controller
Right click on the Controller directory and select "Add new Item". In the dialog find MVC Controller and be sure to give it a name that ends in the Controller suffix. In our case we are writing the ProductsController.
OK, we are ready to start working in ProductsController.cs
The goal of the controller is to prepare the model for the view. We want to bring as much logical as possible out of the view because it is so hard to test in the view. So in the controller, we are going to be accessing the Model and getting it all set up so all the view has to do is spit out some data.
The first thing we need, is access to our database.
1. Add the right namespaces... Linq and a reference to our OR mapping.
using System.Linq;2. Next, we create an instance of the NorthwindEntities container class. Nearly all of our actions will access this class.
using NorthwindModel;
public class ProductsController : Controller
{
NorthwindEntities Northwind = new NorthwindEntities();
The next thing we do is access the Categories table from the model. I snap the results to a generic collection class (you may have to add a reference to System.Collections.Generic) then we pass the results to the view named "Categories". This is a very simple example, later we will see more complex logic here.
//URL: http://localhost/Products/Categories[ControllerAction]
public void Categories()
{
List<Category> categories = Northwind.Categories.ToList();
RenderView("Categories", categories);
}
Next step, we need to create the "Categories" view..
Creating the View
Right click on the Views folder, add new Directory "Products". This so we can organize all our views cleanly. The Right click on Views/Products folder and add new item MVC View Content Page. We are going to leverage the Master Page that comes with the default project to make this look a little more pretty.
Call it Categories.aspx... It is important that the view name matches the first argument to the RenderView method above.
The default project puts the master page in Views/Shared/Site.Master
In order to get the strongly typed access to the ViewData we are passing in from the controller, we need to tell the View Page, the type to expect. This is done by opening up the codebehind (Categories.aspx.cs) and changing the derived type from:
public partial class Categories : ViewPage
{
}
to:
Then you simply write clean, simple, designable HTML. Notice here I am looping over all the items returned in ViewData and pushing them out as a link. I use the MVC helper method Html.ActionLink to create the URL for the List action with the appropriate product ID.public partial class Categories : ViewPage< List<Category> >
{
}
<h2>Browse Products</h2>OK, we are finally ready to run it!
<ul class="categorylisting">
<% foreach (var category in ViewData) { %>
<li>
<%= Html.ActionLink(category.CategoryName, new { action="List", id=category.CategoryName }) %>
</li>
<% } %>
</ul>
Hit F5, and navigate to the controller action we just wrote on http://localhost:64701/products/Categories
Clicking on any of the links give you an error, because we haven't written the List action yet.. we will do that next.
As an aside, if like me, you are in the habit of using "View in Browser" on aspx pages you develop, you are likely to see this error. To reproduce, right click on Categories.aspx and select View in browser.
You get an error. Why? Well, remember with the MVC model, all execution goes through the controller, the views themselves are not runnable. Future tooling will make this better, but in the mean time, use F5 or you can "run in browser" with default.aspx.. just be sure you have built the solution first!
OK, let's now go back and add the List action that we are missing. What we need to do here is find all the products with a give Category. First I need to get all the products out of the model, then I need to ensure that the Category references are loaded. The entity framework offers an explicit loading model by default. As such you have to explicitly load any tables you need. And finally we render the view.
//example URL:http://localhost:64701/products/List/ConfectionsNotice, i am calling a custom method on the NorthwindDataContext class... Personally I like the idea of keeping all the data access logic encapsulated in that class. So to define this method, Right click on the Model, add new Item, select CodeFile and name it NorthwindDataContext.cs and give it the implementation.
[ControllerAction]
public void List(string id)
{
List<Product> products = Northwind.GetProductsByCategory(id);
//prepare the view by explicitly loading the categories
products.FindAll(p => p.Category == null).ForEach(p => p.CategoryReference.Load());
RenderView("ListingByCategory", products);
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace NorthwindModel
{
public partial class NorthwindEntities
{
}
}
public List<Product> GetProductsByCategory(string category)Next, we need to add the ListingByCategory view... We follow the same steps from above to add a ListingByCategory.aspx page in the Views/Products/ directory.
{
return Products.Where(p => p.Category.CategoryName == category).ToList();
}
This time, we should make the ViewData be of type List<Products>
public partial class ListingByCategory : ViewPage< List<Product> >Next, to implement the view, we simply loop over the view data and spit it out in the right format.
{
}
<%--Print out the catagory name--%>Once you add the /Content/Images directory from the sample project you get this:
<% foreach (var product in ViewData) { %>
<% if (product.Category.CategoryName != null) { %>
<h2> <%=product.Category.CategoryName %></h2>
<% break; %>
<%} //end if %>
<%}//end foreach %>
<ul class="productlist">
<% foreach (var product in ViewData) { %>
<li>
<img src="/Content/Images/<%=product.ProductID%>.jpg" alt="<%=product.ProductName %>" />
<br/>
<a href="/Products/Detail/<%=product.ProductID %>"> <%=product.ProductName %> </a>
<br />
Price: <%=String.Format("{0:C2}", product.UnitPrice)%>
<span class="editlink"">
(<%= Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID })%>)
</span>
</li>
<% } %>
</ul>
One of the primary reasons for using the MVC model is to enable Test Driven Development and more generically Unit Testing. Unit testing UIs is notoriously difficult. Automation is fragile and hiring an army of testers to click on buttons is not always cost effective. The model MVC is going for is to separate out as much code (and therefore potential bugs) as possible into the Model and the Controller where they are testable.
So, we are going to write Unit Tests for the two control methods we just created and the corresponding logic in the model.
First, we need to setup our testing environment. The default template already created a Test project we can start to customize.
1. In the MvcApplicationTest project, create a new class called ProductControllerTest.cs
2. Add a method for testing the Categories action we defined above.
namespace MvcApplicationTest.Controllers3. We want to test the controller we wrote, but we don't want to load IIS and all of ASP.NET... we want to have the leanest, most simple system we can to avoid test breaks, etc. To that end, I created a test specific subclass of the ProductsController we defined above. In this subclass I override the RenderView method. Instead of calling out to ASP.NET, my test specific implement simply captures the arguments so they can be queried later in the test.
{
[TestClass]
public class ProductsControllerTest
{
[TestMethod]
public void Categories()
{
}
}
class TestProductsController : ProductsController
{
public string ViewName { get; set; }
public string MasterName { get; set; }
public new object ViewData { get; set; }
protected override void RenderView(string viewName, string masterName, object viewData)
{
ViewData = viewData;
MasterName = masterName;
ViewName = viewName;
}
}
[TestMethod]5. Set the connection string in the test project so that it access the metadata from EF model, but does not open the database. In MVCApplicationTest add an app.config file to the root of the project with the this connection string.
public void Categories()
{
// Create Products Controller
TestProductsController controller = new TestProductsController();
List<Category> l = new List<Category>();
l.Add(new Category() { CategoryName = "Drink" });
l.Add(new Category() { CategoryName = "Food" });
l.Add(new Category() { CategoryName = "Cheese" });
controller.Northwind.TheCategories = l.AsQueryable();
// Invoke Controller Action
controller.Categories();
// Verify Detail Action Behavior
Assert.AreEqual(typeof(List<Category>),
controller.ViewData.GetType(),
"Product object Passed to View");
Assert.AreEqual(3,
((List<Category>)controller.ViewData).Count,
"Correct Number of Catagories returned");
Assert.AreEqual("Food",
((List<Category>)controller.ViewData)[1].CategoryName,
"Correct Product Object Passed to View");
Assert.AreEqual("Categories",
controller.ViewName,
"Correct View Rendered");
}
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="NorthwindEntities"
connectionString="metadata=res://*/;provider=System.Data.SqlClient;"
providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
MVCApplication/Model/NorthwindDataContext.cs add the following encapsulation of the Categories property.
private IQueryable<Product> savedProducts = null;Then, make sure that all access go through this abstraction. For example:
public IQueryable<Product> TheProducts
{
get
{
if (savedProducts == null) savedProducts = this.Products;
return savedProducts;
}
set
{
savedProducts = value;
}
}
public List<Product> GetProductsByCategory(string category)Finally, we need to make sure the NorthwindEntities is accessible from the test classes, so in ProductsController.cs add the "public" modifier.
{
return TheProducts.Where(p => p.Category.CategoryName == category).ToList();
}
public NorthwindEntities Northwind = new NorthwindEntities();
You can download the full working example here (Updated to work with latest bits).