Supercharging ASP.NET MVC with MvcContrib
Hot on the heels of the groundbreaking release of ASP.NET MVC CTP 1 in December of 2007, an open source project called MvcContrib came to life. MvcContrib has enjoyed tens of thousands of downloads since it started in December of 2007. In this article, I will explain MvcContrib; it’s major components, how to use it, and how to get involved in its continued development.
MvcContrib from 30,000 Feet
MvcContrib is a .NET 3.5 SP1 library that provides extensions for use with Microsoft’s ASP.NET MVC Framework. MvcContrib does not provide replacements for parts of ASP.NET MVC. Rather it includes features and components that compliment and extend ASP.NET MVC. The MVC Framework provides many extension points, and MvcContrib leverages many of them. From controller factories to grid components to view automation helpers, MvcContrib is the first place you should look for the extras to make your MVC applications great.
MvcContrib is the first and oldest open source project based on ASP.NET MVC. Based on the stats at CodePlex, it may also be the most popular community-based library for the ASP.NET MVC Framework. Figure 1 shows the stats widget from the project site at http://mvccontrib.org.
Figure 1: MvcContrib has many downloads.
In preparation for this article, I also did some quick search engine analysis to see how many pages, blog posts, articles and the like are talking about MvcContrib. I did time-bounded searches and came up with the following chart in Figure 2 that shows the trend of MvcContrib community growth. You can see now that there are around 90,000 web pages on the Internet that mention MvcContrib.
Figure 2: The MvcContrib community has grown many times over.
Before going further, it is important to have some basic information about the MvcContrib project. Table 1 gives overview information.
MvcContrib has kept pace with the latest releases of ASP.NET MVC. At press time, MvcContrib builds against ASP.NET MVC 2 RC, although you can still download previous builds that are built against ASP.NET MVC 1 from http://mvccontrib.org. I covered MvcContrib in my book, ASP.NET MVC in Action, published by Manning Publications Co. You can find out more at http://mvcbook.jeffreypalermo.com/.
The CodePlex Foundation has received an application from MvcContrib requesting acceptance into the ASP.NET gallery. By the time you are reading this article, the result of that application will likely be known.
Throughout the rest of this article, I will walk through the high points of MvcContrib and demonstrate some of the key features that you don’t want to miss. I’ll start by showing you how to obtain and use MvcContrib. Then I’ll walk through the major features contained in the library and show samples. Then I’ll dive into the source and find out how to get involved with the continued development of MvcContrib.
How to Get and Use MvcContrib
The first step to using MvcContrib is to download the latest release. Go to http://mvccontrib.org and click the download button on the CodePlex project site. Look for a distribution file called MVCContrib.release.zip. Download it and extract it locally. Then create a new ASP.NET MVC project and add a project reference to MvcContrib.dll as shown in Figure 3. There is also a zip file called “extras”. This file is much larger and contains the features that are by nature coupled to larger dependencies, like controller factories and view engines.
Figure 3: MvcContrib added as a project reference.
I will run through a very quick example of using the MvcContrib grid as well as ViewDataExtensions. The example display some of the type members in a grid. Figure 4 shows the page I’ll show you how to create.
Figure 4: The MvcContrib grid makes it very easy to format tabular data.
This example needs a listing of MemberInfo objects and a grid with columns “Member Type”, “Name”, and “Declaring Type”. The first bit of code I need to write is a short snippet in the Index action of the HomeController.public ActionResult Index()
= from MemberInfo m
orderby m.Name ascending
= "Welcome to ASP.NET MVC!";
I slipped in some usage of the MvcContrib ViewDataExtensions here by using the single-parameter Add() method. This extension uses the object type as the dictionary key. This is very useful when you only have one object of a certain type in view data, and you are not using polymorphism between the controller and view. Next, in the view, I will add a small snippet to the default Index.aspx page as follows:<%=Html.Grid(
column.For(x => x.MemberType);
column.For(x => x.Name);
column.For(x => x.DeclaringType);
That’s it! Hit CTRL+F5 to run the project without debugging, and you will see a screen similar to Figure 3.
Now that you’ve already seen how to start using MvcContrib, I will move into a more detailed walkthrough of the major MvcContrib features.
By: Jeffrey Palermo
Jeffrey Palermo is a software management consultant and the CTO of Headspring Systems in Austin, TX. His company specializes in Agile coaching and helps companies double the productivity of software teams. Jeffrey is a MCSD.NET , Microsoft MVP, Certified Scrummaster, Austin .Net User Group leader, AgileAustin board member, INETA speaker, INETA Membership Mentor, Christian, husband, father, motorcyclist, Eagle Scout, U.S. Army Veteran, and Texas A&M University graduate.
Headspring Systems is available for consulting. We can coach your software team or deliver custom software quickly to match your needs. We specialize in software architecture and design, technical coaching, software management coaching, software business analysis, business leadership, technical recruiting, C#, .NET, agile, lean, extreme programming, and scrum.