RCP: Spark View Engine Bootstrapper

So, tonight I had a RCP session with @JohnnoNolan – we decided that for our topic, we would like to “File > New Project” and see what it is like to replace the default ViewEngine in ASP.NET MVC with the Spark View Engine.

Why Spark View Engine?

I have heard a lot of buzz about it from the developer community. Also, I heard the podcast on Hanselminutes and the notion of it all just sat right with me - there shouldn’t be such an epic battle between HTML/C# in your views. Our sole purpose in the View is to render HTML based on some model data – make it easy.

RCP Session Aims

Neither myself or Johnno had any real experience with Spark before the session. As a starting point, I thought it would be good to cover what I would consider to be the “basics” for views:

  • Using variables from ViewData.
  • Using strongly-typed model data.
  • Using master pages.

I decided to spend 1 hour before the session having a quick run-through of the install process and the basics in the hope of making the session more productive. This definitely helped (and I would always recommend spiking on something anyway).

Session Outcomes

I just want to (briefly) touch on what we got out of the session.

Install is EASY
  • Go to the Spark download page.
  • Get the latest stable release.
  • Extract the files in the zip to wherever you store your third party assemblies.
  • Create a new MVC application (Note there is a “gotcha” here with MVC versions – see below).
  • Add a reference to Spark and Spark.Web.MVC.
  • Add the following to Global.cs MvcApplication::Application_Start:
   1:  protected void Application_Start()
   2:  {
   3:    ViewEngines.Engines.Add(
   4:        new SparkViewFactory());

That’s it – note there are some additional configuration steps that you may need, but I didn’t.

I definitely recommend also installing the VS Integration bits that come with the release package – makes working with Spark a LOT easier (including adding much-needed stuff like code colorisation).

ASP.NET MVC 1 & 2 Gotcha

Even though MS have mentioned in MCP programs, podcasts and MSDN documentation about NOT changing public API’s – guess what they did in MVC2? :) It turns out, there was a change to a constructor for a key class. This means that: If you do not have MVC 1 installed, then you MUST install it to use Spark (at this time).

NOTE: You can use Spark in MVC2 applications, you just need the MVC1 framework available.

I think this essentially boils down to Spark having a reference to System.Web.MVC, but “Specific Version” is set to “false” (so it auto-upgrades reference to 2.0 if 1.0 isn’t available). This is causing it to go BOOM!* With this error:

Method not found: 'Void System.Web.Mvc.ViewContext..ctor(

* If anyone thinks I have misinterpreted the problem here – do comment and I will update!

Lesson to be learned? If you are building code against assemblies produced by a third party – set “Specific Version” to true to ensure that if they update it, your application doesn’t break :)

ViewData is Case Insensitive, “model” is NOT.

These are all the same, since the keys for ViewData are case-insensitive:

   1:  <viewdata Message="string" />
   2:  <viewdata MESSAGE="string" />
   3:  <viewdata MesSaGE="string" />

Obviously, the C# code will need to match the case used in declaration – for example:

   1:  ${Message}
   2:  ${MESSAGE}
   3:  ${MesSaGE}

However, the code required to setup Model data appears to be case sensitive (in kind of a weird way IMO).

   1:  <!-- Note the 'model' Attribute is All Lowercase -->
   2:  <viewdata model="ModelType" />

To access in C#:

   1:  // Note 'Model' has Capital 'M'
   2:  ${Model.Property}

That one caught me out for 5 mins.

Master (Layout) Pages

Unfortunately, we never got around to setting up the master pages because we lost a fair bit of time to the MVC 1 vs 2 issue above.

However, the documentation for Spark is fantastic, and adding layout pages looks just as easy as the rest of Spark. So not really “worried” about doing this.

I <3 Spark

I really, really enjoyed learning about Spark – just reading the documentation, I couldn’t stop grinning. The syntax/markup choices just makes sense (on the whole).

Looking at the markup, I have the distinct feeling that it is all easier for designers etc. to stomach – so if you are outsourcing design, I think Spark may well be a great way to go.

And for one line of code in Global (two if you want to completely remove WebForms)? Why didn’t I do this sooner?

Definitely going to be making sure Spark is the first thing I setup after “File > New Project” in future.

Props to Louis DeJardin (@loudej) on doing such a great job!


Post a Comment

Popular posts from this blog

GTD: Biphasic Sleep Experiment – Day 4

TDD – Getting Started with Test-Driven Development

Visual Studio: Unknown Build Error (HRESULT 0x80131515)