jb's Blog


"behold the turtle, he only makes progress when he sticks his neck out"

ASP.NET MVC and LightSpeed – Building a Website

tag icon Tagged as LightSpeed, MVC, jQuery

There have been a number of recent posts discussing how to get started with ASP.NET MVC and various bits of kit, so I thought I would pop up my 2 cents on getting up and running with ASP.NET MVC and LightSpeed since I think they pair up quite nicely.

In addition we will also look at leveraging:

  • jQuery – for our client side goodness
  • NUnit – for our unit testing
  • Ninject – for managing our dependencies

What are we building?

Last year for Tech Ed I used a sample built around a Film Festival website. I thought I would refresh the sample to use the latest bits following an earlier comment asking if I was going to publish the code (which I was originally intending to put on Codeplex but ended up getting lazy and letting it slip..)

So the domain centres around a schedule of films running in cinemas around the country and our system allows people to locate films and then book some tickets.

What’s the plan?

Generally I find it best to start with the an initial cut of the domain model, then we can set up some unit tests to cover the model and after we are happy with that we can then look at getting the web site itself set up. Finally we can start building out some functionality.

So lets start with the Domain Model

Like most developers I am fairly used to modelling the data first using a tool like SQL Server Management Studio diagramming. I also happen to have the database I already developed for this solution last year, so I think we can continue with that. It looks like this:

Shot002

Note that the KeyTable table is some plumbing that we will use with LightSpeed to support our identity strategy. This is using the KeyTable identity pattern described by Fowler in PoEAA.

Creating our Domain Model using LightSpeed

Now that we have our data definition of the model, lets bring that into a working domain model by using LightSpeed to describe our entities. This is going to be pretty straightforward, since we can use the Visual Studio designer to drag on our tables from our database definitions and then extend them from there.

I am going to start by setting up a fresh solution with a single class library project called Model. This is where we will set up our domain model entities.

Shot003

Next we need to add a new LightSpeed model. If you have not already installed it (and why not?!?) – download and install the trial version of LightSpeed, which will add in the associated templates into Visual Studio.

Shot004

Once we add this, we get the LightSpeed design surface, which will allow us to drag tables from our server explorer over on to the surface and that code gen’s the associated entities into classes for us to use. Also because we are using .NET 3.5 it will also build us a LINQ context which we can use to start querying with LINQ straight away.

This should look somewhat familiar to our earlier ERD :)

Shot005

If you have a look at Model.cs which is a resource associated with the LightSpeed .lsmodel file, you can see what has been code generated – a set of partial classes with the field, property and relationshop definitions which match the model which we have described.

Shot007

So we now have a working domain model which we can start extending with some behaviour. To do this, we just need to create a partial class for the entity we want to extend. Let use the Cinema as an example, you will notice we have an object called GeoLocation which we are going to store as a SqlGeography data type in SQL Server. We may want to add a property to this to return us the co-ordinates of that location as a formatted string.

We would just create a new class file, Cinema.cs and fill it out as follows:

using Microsoft.SqlServer.Types;
using System;
 
namespace Model
{
  public partial class Cinema
  {
    public string Coordinates
    {
      get
      {
        if (GeoLocation == null) return "0,0";
 
        try
        {
          SqlGeography instance = SqlGeography.Parse(GeoLocation.ToString());
 
          return String.Format("{0},{1}", instance.Lat, instance.Long);
        }
        catch (Exception)
        {
          return "0,0";
        }
      }
    }
  }
}

We now have our initial Domain Model

In a very short space of time we have taken our existing data model, represented this as a domain model by using LightSpeed to reflect the data schema and automatically generate a class definition for each entity we have described. We can take the Model assembly and in conjunction with LightSpeed we can fetch and persist these entities against a database.

Playing along at home?

Here is the code and associated database setup script (remember to create a database called FilmFestival and run the script under the context of that database first!) to cover what we have done so far. Also remember to download and install LightSpeed

Download FilmFestival2009_Part1.zip (8KB)

Ok – What’s next?

Next we should look at setting up some tests for the model, and then get our web infrastructure underway using ASP.NET MVC..

3 Responses to “ASP.NET MVC and LightSpeed – Building a Website”

  1. ASP.NET MVC Website – The domain model…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  2. [...] ASP.NET MVC and LightSpeed – Building a Website [...]

  3. [...] to Vote[Del.icio.us] jb’s Blog ” Blog Archive ” ASP.NET MVC and LightSpeed – Building a Website (3/21/200…Saturday, March 21, 2009 from [...]

Leave a Reply

jb's Blog