Thursday, March 15, 2012

Creating a FubuMVC Behavior

As part of the day Job, our company decided to use FubuMVC for one of our monitoring and administration tools. This was all very exciting as I enjoy playing with new and shiny things. It's been a few days now and in an effort to put something back to the community I thought I'd provide you all with a few things I've learnt that might help someone out one day. I'll be demoing 3 things

Why would you want to create your own FubuMVC behaviour?

I like to think of the behaviours as a bit like the decorator pattern. They allow us to follow the Open/Closed principle in SOLID. I.e. I can add things such as

  • Logging
  • exception handling
  • unit of work
  • validation
to a code base without actually modifying the existing handler. It's one of the reasons I was so keen to try out FubuMVC. There are a few existing blog posts I've found which explain how to create a FubuMVC behaviour:

Sunday, January 22, 2012

More Readable tests using SpecificationFor<T>

Introduction

In my current role at an agile company we work in pairs and following XP principles, one of which is TDD. I’ve been following TDD for a few years now and one thing I think that helps me when I come back to old tests and helps other developers who are new to a project is when a test has a "context".

Example of a test with context

class When correcting a customers address with no postcode Then ???

Then what? Well that’s the great thing about working in a pair you get to discuss what should happen in the above "context". Perhaps it should log an error, perhaps it should throw an exception, perhaps it should just do nothing? Who knows but at least the context is in the test.

Another example of a test with context

class When correcting a customers address with a valid address Then ???

Again the name of the class should help kick start the discussion between the pair.

If you compare that to a class without a context:

Example of test without context

class CustomerFixtures Then ???

It’s not quite the same and doesn’t really lend it’s self to discussion about behaviour, what do you think?

I don't want to pretend I invented any of these concepts. They've been around for a while now, it's called BDD or Behaviour driven design. For me though it's more about making your tests readable and more importantly understandable from just looking at the class and method names. Do a search for "Context Specification" on your favourite search engine and you'll find lots more examples.

Code

Enough talking get with the code. Here are 2 small helper classes that have been adapted from a “Specification Context” example I found on the interwebs. This works with NUnit. I hope you’ll find it useful:

Tuesday, December 21, 2010

Debugging into NServiceBus so you can solve your own questions

Blog_Image In my previous post I talked about how one of the main problems with NServiceBus is the learning curve to become familiar with all the setup and configuration options. As NServiceBus is an Open Source project, you are able to to download the code. Once you’ve done this in a few simple steps you can start stepping into the source code as if it's your own code. Which makes solving some of the more complex questions with NServiceBus soooo much easier. For example, what does the "Lite" profile do? Well we can find out by stepping through the code. Here is a quick guide on setting up NServiceBus so you can debug into the code.

Downloading the Source code

The NServiceBus downloads page explains how to download the source code. I went for version 2.0 of the code.

You can get the source for the 2.0 release here.

Wednesday, November 24, 2010

Changing Default Logging Behaviour for different profiles with the Generic Host in NServiceBus

nservicebus logo I’ve been doing a lot of work with NServiceBus recently. I have to say it’s an amazing framework, saving my company hours and hours as we didn’t have to write out own Publish and Subscribe framework over the top of MSMQ. One of the tough parts about the framework is the learning curve to become familiar with all the setup and configuration options. Hence this post, I thought I’d share some of the things I’ve learnt over the last 2 weeks, lucky you eh?

Monday, May 10, 2010

Creating jQuery plug-ins for an unordered list

I've been writing a lot more JavaScript recently, sometime's following other peoples code and sometime's writing my own from scratch. A lot of this JavaScript has been done using jQuery. As most of our development team was new to JavaScript the code quickly became unstructured and messy. There had to be a better way.

I was really pleased to discover that it didn't have to be this way and jQuery had created a really easy to use plug-in architecture. As you'll see from this quick guide it's very easy to create a neat and elegant JavaScript solution using a jQuery plug-in. What I really like about the jQuery plug-in solution is that it makes it easy to provide "Progressive Enhancement" to your site. So that if JavaScript has been disabled, the mark-up will remain unchanged. It' like you are rewarding people with JavaScript and providing a richer browsing experience.

The following example is similar to a real world example I was recently asked to create. The client wanted an un-ordered list to only show the first 5 elements and then provide the user with a link to see the other element. They also wanted the list to be sorted. For this I decided to break the problem down into it's 2 parts.

Part 1 would be to create a "listsort" plug-in, Part 2 would be the "hideextra" plug-in to
show/hide the extra elements.

Here's what the final List ended up looking like:

...and these are the steps to create the above:


Monday, February 22, 2010

Creating an ASP.NET MVC 2 Controller Factory with Ninject

Introduction



Recently I've been getting into Test Driven Development and one useful thing that helps me out with this is making use of the Inversion of control principle. This makes it easy to pass a Mock version of all my services for the purposes of testing.

However this presents a challenge with the standard MVC Controller Factory. It only supports the parameterless contructors. However, one of the great things about ASP.NET MVC (2) is that it allows you to override most aspects of the framework. It doesn't restrict you as much as it's ASP.NET webforms predecessor. So to avoid the so call "Anti Pattern" of having both a Parameterless Constructor for your "Real Services" and an overloaded constructor for your testing/Mock Services, you can follow this guide to create a Controller Factory which uses ninject. Then you'll only have 1 constructor and you'll sleep soundly.

Creating Ninject Controller Factory


Sunday, January 31, 2010

Faceting and Multifaceting syntax in Solr 1.4

Introduction


So you've installed Solr 1.4 and you've managed to get some data indexed. Now you're ready to have some fun with faceting. Faceting is basically just filtering the results of a search without effecting the relevance score. Sites such as ebay use faceting to help narrow down the results of a generic search like TV, to give you options such as 32" wide-screen LCD etc.


 

It makes for a very pleasant user experience. It's one of the main reasons to use Solr and Solr makes this process very easy. There are actually 3 types of faceting. In this post I'm only going to talk about "field faceting". Once you've mastered "field faceting", the other 2 types ("query faceting" and "date faceting") are very easy and the basic Solr Wiki will be enough for you to get going.


Field Faceting


Now when you're indexing data in Solr, it's best to use the "text" field type, as this applies lots of filters which will help sort out plurals, remove white space etc. However when it comes to faceting, the values returned will be the values after the filters have been applied. Therefore it's best to create new versions of any fields you wish to facet.