July 2008 Entries

In this tip, I discuss Eric Hexter’s TDD Class Generator Add-In for Visual Studio. This Add-In enables you to generate a class, an interface, and a test class by entering a single keyboard shortcut. Eric Hexter -- who has done valuable work on the MvcContrib project -- visited Microsoft last week and showed me one of his current projects. He has started a project for building ASP.NET MVC specific Add-Ins for Visual Studio. In particular, he has created an Add-In for generating testable classes automatically. You can learn more about the project, and download the binaries and source code, from the...

Posted Thursday, July 31, 2008 5:04 PM

In this tip, I show you how to create a custom ASP.NET MVC view engine that supports lightweight, declarative controls. I show you how to create both simple controls and controls that display database data. I also show you how to test the rendered output of the lightweight controls. One of the beautiful things about the ASP.NET MVC framework is that a view can look like anything that you want. Don't like inline scripts? You can build a new view engine. Don't like anything that looks like a tag in your views? Build your own view engine. You always have...

Posted Tuesday, July 29, 2008 12:09 AM

In this tip, I demonstrate how you can unit test ASP.NET MVC views without running a Web server. I show you how to unit test views by creating a custom MVC View Engine and a fake Controller Context. The more of your web application that you can test, the more confident that you can be that changes to your application won’t introduce bugs. ASP.NET MVC makes it easy to create unit tests for your models and controllers. In this tip, I explain how you also can unit test your views. Creating a Custom View Engine Let’s start by creating a custom View Engine....

Posted Saturday, July 26, 2008 4:46 PM

In this tip, I demonstrate how you can retrieve a view from any folder in an ASP.NET MVC application. I show you how to use both specific paths and relative paths. Until today, I thought that a controller action could return a view from only one of two places: · Views\controller name · Views\Shared For example, if you are working with the ProductController, then I believed that you could only return a view from either the Views\Product folder or the Views\Shared folder. When looking through the source code for the ViewLocator class, I discovered that I was wrong. If you supply a “Specific Path”...

Posted Wednesday, July 23, 2008 5:20 PM

In this tip, I demonstrate how you can create LINQ to SQL entities that do not contain any special attributes. I show you how you can use an external XML file to map LINQ to SQL entities to database objects. I’ve talked to several people recently who are deeply bothered by the fact that the LINQ to SQL classes generated by the Visual Studio Object Relational Designer contain attributes. They want to take advantage of the Object Relational Designer to generate their entity classes. However, they don’t like the fact that the generated entities are decorated with a bunch of attributes. For...

Posted Tuesday, July 22, 2008 10:36 PM

In this tip, I demonstrate how you can eliminate controller methods that simply return views. I show you how to use the HandleUnknownAction method to handle every request against a controller automatically. I saw Phil Haack use the following tip in a demo that he presented. I thought that it was such a great idea that I had to share it. There is no good reason to write code unless there is a good reason to write code. I discover that I write a lot of controller actions that do nothing more than return a view. For example, consider the CustomerController in...

Posted Monday, July 21, 2008 11:12 PM

In this tip, I demonstrate how to create an in-memory data context class that you can use when unit testing ASP.NET MVC applications that access a database. In this tip, I explain how you can write unit tests for data access code within an ASP.NET MVC application. I demonstrate how to unit test the LINQ to SQL DataContext without using a Mock Object Framework. First, I show you how to create a generic DataContextRepository that can be used to retrieve and modify database records. Next, I show you how to create a FakeDataContextRepository that you can use within your unit...

Posted Saturday, July 19, 2008 8:56 PM

Fantastic! A new version of the ASP.NET MVC framework was released on CodePlex today. This new version of ASP.NET MVC includes support for error handling, authorization, caching, and Ajax.  You can download this release at: http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=15389 Scott Guthrie has a detailed blog entry on the features of this release. You can read the first part of his two part series here: http://weblogs.asp.net/scottgu/archive/2008/07/14/asp-net-mvc-preview-4-release-part-1.aspx You should also look at Phil Haack’s blog entry on the new release: http://haacked.com/archive/2008/07/16/aspnetmvc-codeplex-preview4.aspx The CodePlex Preview 4 release includes several important new features including: · HandleError Action Filter – Simply by adding the HandleError attribute to any controller action (or controller class), you can...

Posted Wednesday, July 16, 2008 10:27 PM

In this tip, I demonstrate how you can write unit tests for MVC controller actions that access a database. I show you how to create unit tests for your LINQ to SQL controller action code. Most ASP.NET MVC applications that I write contain a substantial amount of data access code. Typically, I use Microsoft LINQ to SQL to perform database operations. How do you unit test this data access code? There are several different approaches that you might take to this problem: (1) Don’t unit test data access code. (2) Create a test database when unit testing data access code (3) Fake the DataContext...

Posted Tuesday, July 15, 2008 8:28 PM

In this tip, I demonstrate how you can use the nVelocity view engine, instead of the normal Web Forms view engine, when displaying views from an ASP.NET MVC application. By default, you build views for an ASP.NET MVC application by creating ASP.NET Web Form pages (.aspx files). You are not required to do this. If you prefer, you can swap out the Web Forms view engine and use an alternative view engine. In this tip, I demonstrate how you can use the nVelocity view engine. Why would you want to use the nVelocity view engine instead of the normal Web Forms view...

Posted Monday, July 14, 2008 4:40 PM

Context is the enemy of testability. In this tip, I demonstrate how you can eliminate, once and for all, the HTTP Context from an ASP.NET MVC application. A controller action that interacts only with the set of parameters passed to it is very easy to test. For example, consider the following simple controller action: VB.NET Version Public Function InsertCustomer(ByVal firstName As String, ByVal lastName As String, ByVal favoriteColor As String) As ActionResult CustomerRepository.CreateCustomer(firstName, lastName, favoriteColor)   Return View() End Function C# Version public ActionResult InsertCustomer(string firstName, string lastName, string favoriteColor) { CustomerRepository.CreateCustomer(firstName, lastName, favoriteColor); ...

Posted Friday, July 11, 2008 5:57 PM

In this tip, I explain the different options for running an ASP.NET MVC application from Visual Studio 2008. I recommend that you run an ASP.NET MVC application directly from the ASP.NET Development Web Server. An ASP.NET MVC application works differently than a normal ASP.NET Web Forms application. When you request a URL, there might not be a corresponding page on your hard drive. For example, imagine that you request the following URL: /Product/Index.aspx There is no reason to assume that an ASP.NET MVC application contains a Product folder or that the Product folder contains a file named Index.aspx. The Routing module used by...

Posted Thursday, July 10, 2008 9:20 PM

In this tip, I show you how you can create a Visual Studio 2008 macro that creates a new MVC controller, view folder, and controller unit test with a single command. Don’t get me wrong. I like the Visual Studio 2008 designer tools. I like dragging-and-dropping items from the toolbox. I’ve memorized many useful Visual Studio keyboard shortcuts. But, at the end of the day, there is nothing faster than firing off a quick command from the Command window. In this tip, I explain how you can take advantage of Visual Studio macros and the Visual Studio Command window to generate...

Posted Thursday, July 10, 2008 1:01 AM

In this tip, I demonstrate how you can pass browser cookies and HTTP server variables to controller action methods in the same way as you can pass form and query string parameters. Imagine that you make the following browser request against an ASP.NET MVC web application: http://localhost/Product/Index When you make this request, by default, the ASP.NET MVC framework will invoke an action named Index() exposed by a class named ProductController. There is a class in the ASP.NET MVC framework, named the ControllerActionInvoker class, which is responsible for invoking a controller action in response to a browser request. The ControllerActionInvoker class has several responsibilities. This...

Posted Tuesday, July 08, 2008 6:31 PM

In this tip, you learn how to create and use templates in the MVC framework that you can use to display database data. I show you how to create a new MVC Helper method named the RenderTemplate() method. While I was back home in California during the 4th of July weekend, I was talking to my smarter, older brother about the differences among building web applications with ASP.NET Web Forms, ASP.NET MVC, and Ruby on Rails. I was bemoaning the fact that I really missed using controls when building an ASP.NET MVC application. In particular, I was complaining that I missed...

Posted Monday, July 07, 2008 6:56 PM

In this tip, I demonstrate how you can create unit tests for the routes in your ASP.NET MVC applications. I show how to test whether a URL is being mapped to the right controller, controller action, and action parameters. If you are being virtuous about test-driven development when building an ASP.NET MVC application, then you should write unit tests for everything. Write the unit test first then write the code that satisfies the test. Repeat, repeat, repeat, ad nauseam. Routes are a very important part of an MVC application. A route determines how a URL is mapped to a particular controller and...

Posted Wednesday, July 02, 2008 8:53 PM

In this tip, I show you how to test ASP.NET intrinsics when building unit tests for an ASP.NET MVC application. I show you how to create a standard set of fake objects that enables you to fake the current user, the current user roles, the request parameters, session state, and cookies. An ASP.NET MVC application is infinitely more testable than an ASP.NET Web Forms application. Every feature of ASP.NET MVC was designed with testability in mind. However, there are still certain aspects of an ASP.NET MVC application that are difficult to test. In particular, you might discover that testing the ASP.NET...

Posted Tuesday, July 01, 2008 12:26 AM