Saturday, May 29, 2010

Test driving the TekPub ASP.Net MVC2 Starter Site

I like starter sites… we use one I put together at work (I called it BlankSolution).  They make it really fast to get up and running on a new project, AND to carry over improvements and best practices from project to project.  They are also great for learning.

Rob Conery released an MVC 2 starter site recently which looks really interesting.  Go check it out to see all the goodies it includes… then come back here to read about my experience using it to build up a real project (the NCharlie Task Board of course!)

Here we go, step by step (I’m following Rob’s blog post announcing the 0.5 release):

1. Download the bits from CodePlex, extract the zip and open the solution.  Screeeech… error opening in VS 2008.  Something about a bad pointer in the Web.csproj file to MSBuild “v10.0” WebApplicationTargets.  A quick edit to the csproj file (changed v10.0 to v9.0) fixed that (I won’t be forking the starter project for fixes if I find them… just want to see how quickly I can get up and running tonight.)

2. Set up the app to use SubSonic (the ORM tool Rob wrote).  Using Subsonic is a “for now” choice, to quickly build up the database from POCO domain classes.

- Global.asax.cs needs one change, on line 135.  Change “SiteEFSession” to “SubSonicSimple”.

- SubSonicSimple.cs needs one change, on line 18, add the name of the connection string the app will use, “SiteData”.

- The Web.config file doesn’t need to change, as long as your local database instance name is “SQLEXPRESS”.

3. Add a new Class Library project to the solution to hold the domain classes.  I called it “Core”.  Make sure to add the “Core” project to the “Web” project’s references.

(Side note: If you read through the comments on Rob’s getting started post, he ripped some guy a new one for suggesting he put his domain entities in a separate project, and add separate “Model” classes that map between the Core and the UI… so proceed at your own risk!)

- Add the domain classes:

Domain-objects

4. Modify HomeController.cs:

- Add ISession as a private, injected member.

- Grab all the Board records from the database, ordered by Board name, and paged

public class HomeController : Controller {
private ISession _session;

public HomeController(ISession session)
{
_session = session;
}

public ActionResult Index(int? page) {
var boards = _session.All<Board>().OrderBy(x => x.Name);
var list = new List<Board>(boards);

return View(list);
}
 

5. Control-F5 to build and run the site… it builds, but:



Error-screen


6. I had some problems.



- First, my SQL Server instance is version 2005… Rob’s Site.mdf file in the App_Data folder is v655 (2008 I think), and it would not open.  I created a new “SiteData” blank database on my local instance, modify the connection string in Web.config, and tried again.  Same beautiful error screen I got before.


- Second (I looked in the Event Viewer for this exception), I found this in the exception detail:


Exception information:
    Exception type: InvalidOperationException
    Exception message: Can't decide which property to consider the Key - you can create one called 'ID' or mark one with SubSonicPrimaryKey attribute

…so, I added an “ID” property to by Board class, and tried again.  Error screen.

- This time the “Index.aspx” view is expecting the model to be “Web.Model.User”, and I’m passing it a list of Board entities to choke on… that makes sense.  So I changed the “Inherits” directive at the top of Index.aspx to expect IList<Core.Domain.Board>, and removed the one “EditorFor” statement on line 35, and…

Starter-home 



It’s alive!  It took me almost 2 hours to follow Rob’s getting started post to this point… I didn’t get to the part about switching SubSonic out for EF or LinqToSql.  I’ll leave that, and making this into an actual Task Board, for next time.

No comments:

Post a Comment