Wednesday, January 11, 2012

Simple Circles–Add an ASP.NET MVC 3 UI (Part 4a)

This post is part of a series on building a simple to use web-based contact and customer relationship management application. The goal is to support various audiences including businesses, teams, clubs, religious organizations, etc.
Last time I left off with a working fake implementation of the IPartyRepository called FakePartyRepository. We know that it is “working” because of the unit tests project that exercised the various methods of the contract. Since I have a working repository I’m going to start building out an ASP.NET MVC 3 website that will use the repository. My experience is that doing so earlier in the development cycle rather than later will flush out any problems or misaligned requirements. Remember, to this point I’ve been operating in something of a vacuum – building interfaces and implementations according to a vague vision of consuming them. Now I will make that vision concrete by constructing a user interface to work with.


Using the New Project wizard, I added an ASP.NET MVC 3 website and chose “Internet Application” for the template with Razor as the view engine. I did not elect to create unit tests as I’m using xUnit and the template support is not worth setting up according to this discussion post. At this point pressing <F5> will bring up the default empty website.
Simple Circles - MVC ScaffoldingI’m a big fan of Steve Sanderson’s Pro ASP.NET MVC Framework books as well as his work on the MVC Scaffolding project which I started using last January as soon as he began publishing it. Therefore, I’m going to draw on his work to move things along quickly. First, I installed the scaffolding project using ‘install-package mvcscaffolding’ which in turn brought in several dependencies as shown on the right.
I’ll also start off simple by using SQL Server Compact 4.0 along with the new ASP.NET Universal Providers 1.01. You can follow Scott Hanselman’s or Scott Guthrie’s posts on setting it up.


MVC Scaffolding works off a model so first I added a Person class to the Models folder…
namespace Circles.Models
  using System;
  using System.ComponentModel.DataAnnotations;

  /// <summary>
  /// Represents a view model of a Person
  /// </summary>
  public class Person
    /// <summary>
    /// Default constructor
    /// </summary>
    /// <remarks>Ensure Gender has a default value.</remarks>
    public Person()
      this.Gender = "Unknown";

    [Display(Name = "Person Id")]
    public Guid PersonId { get; set; }

    [Display(Name = "Full name")]
    public string FullName { get; set; }

    // [Required]
    [Display(Name = "Salutation")]
    public string Salutation { get; set; }

    [Display(Name = "First name")]
    [StringLength(50, MinimumLength = 3)]
    public string FirstName { get; set; }

Simple Circles - Scaffold PersonNow with a single NuGet instruction I scaffold a full CRUD interface for the model class using ‘scaffold controller PeopleController -ModelType Circles.Models.Person’ as shown to the right. This one command generated an MVC controller class complete with all behaviors as well as a set of six Razor pages (Index to list, Details, Edit, Create, Delete, and a shared _CreateorEdit) to support CRUD behavior for a Person instance. As the second image shows, quite a bit of code was generated.Simple Circles - Scaffolded Code

I can’t say enough about how exceedingly cool Steve’s scaffolding work is and how much effort it saves. Unfortunately, the current implementation is built to support Entity Framework as the underlying data technology. Notice the CirclesContext class added to the Models folder and the reference to that context embedded in the PeopleController class. While the hooks are there to plug in other scaffolding templates (e.g. NHibernate) as of yet nobody has stepped forward to provide implementations.

You could also, as Steve suggests, add the Entity Framework 4.1 Code First capability via ‘install-package EFCodeFirst.SqlServerCompact’ then regenerate the controller with the additional flags ‘-Repository –Force’. You’d then have a working repository-based implementation built with all Microsoft products.

In the second part of this installment I’ll take a different approach and wire in Ninject as the dependency injection framework then use it to inject the FakePartyRepository previously built.

The source code for this article can be downloaded from here.

No comments:

Post a Comment