Tuesday, 9 February 2010

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(
  System.Web.Mvc.ControllerContext,
  System.Web.Mvc.IView,
  System.Web.Mvc.ViewDataDictionary,
  System.Web.Mvc.TempDataDictionary)'.

* 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!

Sunday, 7 February 2010

TomatoTimer Now on GitHub

Not sure if you guys remember me talking about TomatoTimer in the past, but after some thought I have decided to whack it on GitHub as a public repository.

Why GitHub?

Cos it’s awesome. Even when it fails it’s awesome because I think Octocat is awesome :)

Why a Public Repo?

Put simply, there are people that were interested in using the application, and there a couple of bugs in it that I can’t guarantee a fix time on. I am not (and likely to remain not) invested in WPF applications at all at this time. I am instead focusing my efforts on ASP.NET MVC. So, rather than keep people waiting, I thought I would throw the code online so they can at least use it.

If it just so happens that someone sees the problem and they send me a pull request, even better.

Where Do I Get the Code?

You can get (read-only) access to the repository here: http://github.com/robcthegeek/TomatoTimer. The code is C# 3.5, with a WPF front-end. xUnit is used for testing, and Rhino Mocks for mocking (although likely to remove as I have been working on a “mocks ban” recently and actually found it quite nice :).

Everything you need should be in there, if you come across any problems, please raise an issue and I will get it sorted ASAP for you.

Anything Else I Should Know?

The code was written a long time ago, also there was a fair bit of “let’s try this” – so the quality is not 100%. But if/when I get the chance I will do my best to clean up :) (more motivated to do so now it is in the public eye!) ;)

Hope you find it useful!

Tuesday, 2 February 2010

“Getting Started with Ruby” Kata

I have been having talks/ideas for some time about starting a bit of a affair with Ruby. As a .NET geek, you may be wondering “why?”.

I don’t want to “go on” too much here, if you’re curious about these, please feel free to ask:

    • It’s Designed for Programmers
    • It’s Dynamic and Not JavaScript
    • Rails Looks Interesting
    • Massive Community Movement/Involvement
    • Strong Open Source Community

I got thinking. I want to get started, but have no idea where to begin. I make no claims that I am anything other than a complete n00b.

So, I started feeling my way through the darkness and decided to try and formalise it into a kata that can be repeated.

NOTE: This is totally a “bootstrapping” kata – if you have done anything beyond the basics with Ruby, this is of no good to you!

The Kata

Problem

Create a simple class that returns a number formatted as a percentage. For whole numbers, “zero” decimal places should be removed, for fractions, they should be formatted to two decimal places.

For example:

  • “10” => “10%”
  • “10.2” => “10.20%”
  • “10.268” => “10.27%”
  • “10.00” => “10%”

When the input is less than zero or greater than 100, then the result should be capped at zero or 100.

For example:

  • “200” => “100%”
  • “-54” => “0%”
Steps

These are the rough steps I followed:

  • Install your IDE of choice (I opted for Netbeans because it is free and looked reasonable – please comment if you know better IDEs!).
  • Create your first (failing) test (I did “0” => “0%”).
  • Create a class to pass the test.
  • Add tests for more whole numbers.
  • Add tests for the range cap.
  • Add tests for two decimal place formatting.
  • Add tests for input (e.g. non-numerical strings).

I decided to stop there.

My Thoughts/Points to Note
  • The default unit test framework baked in to Ruby requires that test method names start with “test”.
  • I love the fact that unit testing is baked into the framework, and IDE’s (cleanly) support it out-of-the-box.
  • I spent AGES (like an hour) wrestling with the formatting to two decimal places. I couldn’t find any decent rounding function in the framework. My implementation is probably lame, feel free to send a pull request :P
  • I am still really getting used to the “type-less” nature of parameters etc. so not sure if I should be doing more (or even less) work with validating inputs etc. (i.e. put all our faith in duck typing, or program defensively).

You can check out my code here.
NOTE: I think this link may go down when I merge in the changes – if so, just ping me and I will update.

So, a real learning journey. Felt weird (and admittedly scary) to be sat there looking at code I didn’t really understand again. Definitely a good thing for my geek-head :)

Yes, this kata took me way longer than I had hoped/expected (*cough* ~2.5 hours).

What are your thoughts on this kata? Too little? Too much?

Sunday, 31 January 2010

DDD8 - A Big Thanks!

Yesterday, I attended the DDD8 (#DDD8 on Twitter) conference at MS Reading. I went to the DDD gig last year (also at Reading) and had a great time, so did turn up with high expectations!

I just wanted to put a post up with my thoughts on the day/sessions in the hope that:

  1. It gets more people involved (be it talking, attending or sponsoring).
  2. People know I am grateful for the free event.

General Organisation

I found the day to work out nicely – thanks to SqlBits, this year we had coaches pick us up from Reading train station and take us straight to the MS campus. This must have saved the geek collective a good amount of cash.

Plenty (IMO) of food was laid on, welcomed to MS with a nice sausage and bacon roll and a brew. Breakfast of champs :)

As with all events like this, there are always a clash with sessions you want to go to. I have no idea how you can fix this and please everyone – so I am not going to moan. But it is a plus that there were so many good session that I had conflicts in my head ;)

I think we can all agree that the only real downside is how quickly the places went (~12 minutes I think!). Again, no idea how to easily solve this other than a bigger venue/more speakers?

Quick Session Review

These are obviously only the sessions I attended.

Real World MVC Architectures

Great session by Ian Cooper (@ICooper) on how to avoid “Fat Controllers”, introduce service layers, creation of ViewModels, Command/Query separation and more. Also shared some nice anecdotes of how things have evolved at his own workplace due to pain caused by lack of the above.

Hello Document Databases

Apparently, this was Neil Robbins’ (@NeilRobbins) first hour-long talk. I almost called BS on it because he did such a great job. Found this to be a nice, easy-going introduction to CouchDB as well as the concept of document databases. Also had a nice bit of history, and outlined the reasons for the NoSQL movement.

C# 4

A bit of a (well deserved) celeb in the geek community, Jon Skeet (@JonSkeet) gave a nice introduction to new features in C#4. Was great to see Jon speaking because not only is he a freaking C# ninja, he is a great communicator. Sad Tony couldn’t make it though :(

Not Everything is an Object

A rather tanned-looking Gary Short (@GaryShort) gives a talk on how functional languages (Clojure was used on the demo) can help solve some of the issues we have with OOP languages. The main one being, concurrency. Really got me thinking about how adding a functional language to my geek toolbox could really help with those odd bits of code should “just work”.

Testing C# and ASP.NET Applications with Ruby

Ben Hall (@Ben_Hall) came along to show us how a language that can support clean syntax (or essentially DSL’s), namely Ruby, can REALLY help with the automated testing process. I am thinking of starting a bit of a love affair with Ruby, so was keen to see “what else can be done with it” outside of the MVC Web App scenario. Was great to not only see how it can be used, but how much easier it makes the process as a whole.

The Social Side

Unfortunately, I couldn’t stick around after the event for a few drinks. This kinda sucked because there are always SOOOOOO many people that you need/want to say “hi” to. My apologies to those I never got the chance to have a good chat with.

I have decided next year, I will book myself into a hotel or something so I don’t have to shoot off and can celebrate the good day as you should – at the bar :)

To those that I did meet/talk with – was a real pleasure, had fun talking to you all. Look forward to keeping the conversation going and meeting you again in the future!

In Summation…

My heartfelt thanks to all of the guys that make this happen (be it speakers, helpers, sponsors, Microsoft, whoever) – these days are so beneficial for the community as a whole. And the fact they don’t cost us a penny obviously helps with us getting along to them!

A great day! Be there next year! :)

Update

I have just submitted my feedback for the event (make sure you do yours!) and following discussions with some other attendees, I made a suggestion:

Next year, we do Saturday “business as usual” but then stick around for an “Open Spaces” style day on Sunday. I think this offers the following benefits:

  • It encourages people to stay, so they can enjoy the after-event talks beers.
  • All of the ideas swimming in their head after day 1 can be put into some code in day 2.
  • It gives us more of a chance to meet the (many) names you want to turn into a face.
  • We can get more free food from Microsoft.

Obviously, this may be more difficult for some people to accommodate (e.g. family and whatnot) but I think even if only half of the people could make it, it could still be a really awesome day?

What are your thoughts?

Saturday, 30 January 2010

New Year, Intentionally Late New Start

So, I have been reading wave after wave of similar posts. I guess it is finally time to do mine..

Looking Back

Last year was a great year for me, I learned a lot about myself, GTD, sleep and TONS of stuff to help me continue improving my craft.

That said, all is not rosy. I can’t help but feel I did loads but got nothing done. Towards the end of the year, I really started “getting” my GTD process (mainly the addition of Personal Kanban). I crashed out with burnout, and then spent the holiday season thinking and reflecting on where the hell I went wrong.

Looking Forward

Personal Kanban (with “Lean” in the mix) introduced reinforced the concept of “only adding value”. I say reinforced, because us geeks should already be familiar with the concept. “Why is that?” you ask? – Well most of us geeks have heard the term YAGNI right? Agile peeps use it all the time. Put simply, “it it ain’t worth it – don’t do it”.

I need more YAGNI in my personal life. Writing badass code means shit all when you are either unhappy or running around in circles – or worse, both.

Timely Advice from Entrepreneurs

I have recently been reading some books that have been collecting dust on my “To Read” shelf. One of these is Sir Richard Bransons “Screw It! Let’s Do It”. Coupled with that, I also listened to a podcast (2 parts) on Freelance Advisor with Barbara Winter (author of “Making a Living Without a Job”). Both had a message that really struck a chord with me (obviously this is not their only message!).

In essence, what they said came down to this:

  • Corporate drones check their bank balance before doing something. If balance < required budget. It doesn’t happen.
  • Entrepreneurs think “that sounds like a great idea – how do I make it happen?

I think this is a really powerful message. Are you a drone or a driver? To be quite blunt, I don’t want my life to be dictated to me by a bank balance.

KISS – My 2010 Mantra

So, following the KISS principle - this year, my mantra is simply:

PICK YOUR SHOTS

That’s it. I have it written in big letters on my desktop so I see it every day.

I want to look at something, if I think it is a good idea, then I want to make it happen. I want to survey the options before me, pick what will be best for me and then nail it.

Sample of Things Currently on My RADAR

  • Get an Open Source project up and running.
  • Add the features on the backlog for my website, and then get a designer in to make it look pretty (finally admitted design is not my core competency and would be best left to the pros).
  • Get more blog content up, especially technical bits (I know I have been heavily GTD-focused).
  • Review my current employment position.
  • Get more involved with the (geek) community.
  • Have an awesome trip to Vegas.
  • Have an awesome trip somewhere with snow and learn how to (snow) board.

So, are you a drone or a driver? Are you picking your shots or “spraying and praying”?