For a couple of weeks now the Spark View Engine has been available as a package from the NuGet server but last night it had a bit of a makeover.
If you open the package manager and filter on “Spark” as I have, you’ll see two copies of the Spark Core and the Spark.Web.Mvc packages. The highlighted one is the latest build, and the other is the latest release from a while before that – version 1.1 to be exact. At this stage, I would recommend using the latest build due to a number of fixes and additions that have been added recently.
What’s the difference?
Well, the nice thing about getting Spark via the NuGet Package Manager is that for a new project, it will also automatically bootstrap the view engine code into the Application_Start which means you basically go “File –> New MVC Project”, reference Spark via the NuGet Package Manager, and you’re ready to start creating views and controllers.
All the wiring is taken care of. Well how is that done pray tell?
Well, we’re taking advantage of one of the other packages in the NuGet list called the WebActivator package which we take a dependency on. This allows us to shim in bit that we need for bootstrapping. After installing the Spark.Web.Mvc Package in an empty ASP.NET MVC2 application, the folder structure will look something like this:
Notice the AppStart_SparkWebMvc.cs file there? That looks like this inside:
using System.Web.Mvc;
using Spark;
using Spark.Web.Mvc;
[assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication1.AppStart_SparkWebMvc), "Start")]
namespace MvcApplication1 {
public static class AppStart_SparkWebMvc {
public static void Start() {
var settings = new SparkSettings();
settings.SetAutomaticEncoding(true);
// Note: you can change the list of namespace and assembly
// references in Views\Shared\_global.spark
SparkEngineStarter.RegisterViewEngine(settings);
}
}
}
The Spark bootstrapping code is kept separate from your Global.asax and gets dynamically inserted at runtime by the WebActivator. The two advantages here are that it leads to a nice clean uninstall if you don’t need it anymore, and it also lets you keep the complexity of the Global.asax down. You’ll see once you’ve installed multiple packages that all do their own things to the Global.asax, you’ll be glad they’re all neatly parcelled away for easy maintenance.
Conclusion
I won’t go into too much more detail here, suffice it to say that I’ll be keeping the NuGet feed up to date with the latest Spark builds so that you can always take advantage of the latest features, but we’ll leave some older versions up there for those that prefer a more stable, tried and tested release. Let me know if you have any hassles…
Until next time…
Robert The Grey
Surely it would have been nicer to use partial classes and event subscriptions to wire these things in instead of the web activator? Or am I missing something?
ReplyDeleteExcellent! Great article, I already saved it to my favourite,
ReplyDeleteI had heard of it, but never got around to actually playing with it until after the conference. And the verdict is, I really like it.
ReplyDelete--------------------
Designer Sarees