Rob Smyth

Friday 14 September 2007

Virtual Build Box Lava Lamps

Continuous Integration is the blood supply of an XP style team. The visibility of build box failures and time broken is important. It helps the team focus on keeping the build pristine. The team I'm working with uses CruiseControl to fire a warning audio and to change the state of a tray icon. This is great, particularly the audio, but it does not give us indication of how long it has been broken. So come back from lunch and not notice, or we loose the plot for a few hours and forget it.

One solution is to use Lava lamps. As they need to warm up their activity gives a nice feedback of just how green or how read we are. But they need power wiring (You could use the CruiseCotnrol X10 interface) and control and how do you handle multiple builds and multiple boxes?

The other option is to dedicate a screen on a wall. One company I worked with displayed the state of all builds on all boxes as a grid on this screen. It could be seen across the floor and worked well. But it does require a dedicated box and screen etc .

So what about the new digital picture frames now on the market. One from ThinkGeek looks interesting as it can be updated my email so a build box only needs to send an email. This means that a more informative and dynamic display in a team pit without the overhead of computer. I like the idea.

My thoughts of an ideal virtual lava lamp set-up:
  • Audio play on build failure and on build fixed (as provided by CruiseControl tray tool).
  • Screen in team pit displaying status as colour and animating time broken.
  • Tray icon access for build box details (e.g. CruiseControl tray tool).
  • Screen displays a history graph one or two (only) metrics like build time, code coverage, or number of unit tests. Whatever the team feels helps them at that time.
Perhaps I can use it at home to tell me if I have email, the weather forecast etc.

Wednesday 5 September 2007

Dependency Injection - Ninject

To evaluate Ninject I downloaded the binaries and created a VS project to play with. First question was how to use it. I could not see any doco in the downloads (I only downloaded the binaries) and the web site gave a nice intro but no setup info I could immediately see (I'm impatient). So I tried adding a reference to Ninject.Core.dll. This worked well with the simple web guide.

My test was to implement a dog owner (John Smith) with two dogs. John is a singleton and he names his dogs. Each dog has tail and a mouth that can bite. Pull the tail and he bites! This is designed to test a singleton object with multiple instances of another object. Those objects (the dogs) do in turn have other objects with circular references (dog has tail and tail is attached to dog).

This was easy except that Ninject was unable to resolve a circular dependency (using a constructor and method injection combination). I had hoped that the instances would be created and then the method injection done.

Note: Comment from Nate (see comments below) points out that Ninject does resolve circular dependencies. Looks like my test was wrong, I will retry it again in the next couple of days.

Here is the code:

class Program
{
static void Main(string[] args)
{
IKernel kernel = new StandardKernel(new RunTimeIoCModule());

IJohnSmith johnSmith = (IJohnSmith)kernel.Get();
johnSmith.PatDog("Rover");
johnSmith.PatDog("Rex");

johnSmith.ChangeDogsName("Rover", "Spike");

IJohnSmith johnSmithAgain = kernel.Get();
johnSmithAgain.PatDog("Rover");
johnSmithAgain.PatDog("Rex");

johnSmithAgain.PullTail("Rex");

System.Threading.Thread.Sleep(3000); // just time to see the output
}
}

public class RunTimeIoCModule : StandardModule
{
public override void Load()
{
Bind().To();
Bind().To();
Bind().To();
Bind().To();
}
}

[Transient]
public class Dog : IDog
{
private ITail tail;
private IMouth mouth;
private string name;

[Inject]
public Dog(ITail tail, IMouth mouth)
{
this.tail = tail;
this.tail.SetDog(this);

this.mouth = mouth;
}

public string Name
{
get { return name; }
set { name = value; }
}

public void Pat()
{
Console.WriteLine("Dog '{0}': Pat - thank you", name);
tail.Wag();
}

public void Ouch()
{
Console.WriteLine("Dog '{0}': Ouch!", name);
mouth.Bite();
}

public void PullTail()
{
Console.WriteLine("Dog '{0}': Pulling tail - watch out!", name);
tail.Pull();
}
}

[Transient]
public class Mouth : IMouth
{
public void Bite()
{
Console.WriteLine("Bite!");
}
}

[Transient]
public class Tail : ITail
{
private IDog dog;

public void Wag()
{
Console.WriteLine("Tail Wag");
}

public void Pull()
{
Console.WriteLine("Tail Pull");
dog.Ouch();
}

public void SetDog(IDog dog)
{
this.dog = dog;
}
}

[Singleton]
public class JohnSmith : IJohnSmith
{
private IDog dog1;
private IDog dog2;

[Inject]
public JohnSmith(IDog rover, IDog rex)
{
Console.WriteLine("Hi, my name is John Smith.");

this.dog1 = rover;
this.dog1.Name = "Rover";

this.dog2 = rex;
this.dog2.Name = "Rex";
}

public void ChangeDogsName(string oldName, string newName)
{
IDog dog = GetDog(oldName);

if (dog != null)
{
dog.Name = newName;
}
}

public void PatDog(string name)
{
IDog dog = GetDog(name);

if (dog != null)
{
dog.Pat();
}
}

public void PullTail(string name)
{
IDog dog = GetDog(name);

if (dog != null)
{
dog.PullTail();
}
}

private IDog GetDog(string name)
{
IDog dog;

if (dog1.Name == name)
{
dog = dog1;
}
else if (dog2.Name == name)
{
dog = dog2;
}
else
{
dog = null;
Console.WriteLine("I do not have a dog called '{0}'.", name);
}

return dog;
}
}

I've not published the interfaces to save space. I used interfaces to support TDD.

Tuesday 4 September 2007

.Net Dependency Injection Frameworks - Part 2

Searching for a Dependency Injection Framework (IoC) my first pass has narrowed the contenders down to:
For my criteria and a list of other frameworks see the 'Part 1' post.

NinjectStructureMapMicroKernelPuzzle.NetImportance
License
Apache 2 OSS

essential
C#55

4
Doco34X
X4
XML config04

2
Programmatic config54

10
Constructor injection4Y

20
Method injection4?

4
Private field injection4?

3
Setter injection4
4

3
Singleton activation44

10
Transient activation44

10
Other activation45

3
Simple to use4?

20
Multiple configs45

5
Contextual binding45

4
Generic types3
4

2
NMock injection34

4
Maturity
5

4
SCORE
58
57
XX

Note: Above chart updated following Nate's comment (see comments below).

I eliminated MicroKernel as I could not find focused documentation (not related products) and what I could find turned out to be links to Porn site adverts. I gave up on Puzzle.Net as I could not find any documentation web pages.

So I'm down to Ninject & StructureMap and wondering if I ought to take a closer look at Spring.Net as it mentioned often on the web.

I will continue to update this another night. Looks like the next step is to try some sample code.

Monday 3 September 2007

.Net Dependency Injection Frameworks - Part 1

In recent work I have been finding dependency injection (Inversion of Control, IoC) very helpful to enable TDD. While in early development object assembly was relatively simple it is now becoming more complex making me think more about using an IoC framework. This post documents my progress in looking at frameworks and I hope will be updated as I learn more.

The nature of the application is that it will have a long lifetime and will need to be expandable. Overall project complexity is medium.

Requirements:
  • .Net C#
  • License suitable for commercial use
  • Require constructor injection (for readability) and setter injection to support circular dependencies
  • Runtime creation of object sets
  • Plugin support ... I'm not sure
  • Suitable use in a TDD environment with mocking
  • Code readability and simplicity

Spring.Net

The documentation introduction starts by describing Spring as being for 'enterprise applications' and goes on to talk about much more than IoC. So my first impression is that this may be too heavy for my liking.

StructureMap

A dependency injection framework, nothing else. This I like.

I've done a quick look at the documentation. My first impression is good. I noticed a feature suggesting support for mocked object injection. I've not looked further but this is interesting as I do spend time most days creating mock objects for injection.

MicroKernel

First glance is good. Looks simple to use. I want to come back to this one later for a closer look.

ObjectBuilder

The documentation page shows 'under construction ...'. Not a good sign. The site was not first time visitor friendly so I reckon I will revisit this one only if I get desperate.

PicoContainer .Net

Nice documentation, looks cool, but it is really focused on Java. I get the feel that while .Net code is available it isn't really a prime focus. So I will skip this one.

Puzzle.Net

Looks interesting but difficult to find documentation. I noticed another project NPersist on the site that I want to look at later. This one is worth another look later.

Ninject

Looks real good for what I'm looking for. Lightweight. It uses a service binding concept so I need to check if a single interface can be 'bound' to different implementations for different targets.

Links:

Sunday 2 September 2007

A 1930s Barrister-In-Law Recommendation

I was browsing through the October 1930 issue of Homes and Gardens (UK) , as you would on a fine Sunday, and found an important message I must bring to your attending. It is a customer testimonial from 'A Barrister-in-law' in an advertisement (page xiv) proudly titled 'Sewage Purification'.

"... Your sewage installation has given entire satisfaction. I take every opportunity of showing and recommending it to my friends .... it would be a public benefit if your system could be adopted everywhere in lieu of cesspools."

Well done Tuke & Bell, Ltd (London).

I also found advertisements for bird baths, dog beds, stools, rugs, etc just like those around our house and available at K-Mart. Hmmm.