Monthly Archives: May 2009

Using the Do handler for a method with out parameters

As you can read in the documentation for Rhino Mocks:

There are times when the returning a static value is not good enough for the scenario that you are testing, so for those cases, you can use the Do() handler to add custom behavior when the method is called. In general, the Do() handler simply replaces the method call. Its return value will be returned from the mocked call (as well as any exception thrown). The handler’s signature must match the method signature, since it gets the same parameters as the call.

There are quite a lot of delegates defined in Rhino.Mocks.Delegates but none of them has an output parameter so we got stuck when we tried to mock a method with the following signature:

bool TryGet(int id, out Entity entity);

Luckily enough it didn’t take too long before i realised we could simply define our own delegate:

delegate bool TryGetMethod<keyType, ValueType>(KeyType key, out ValueType value);

And now we can easily setup a result for our method:

myEntityRepository
  .Stub(repository => repository.TryGet(0, out myEntity);)
  .IgnoreArguments()
  .Do((TryGetMethod<int, MyEntity>)delegate(int id, out MyEntity entity)
  {
    entity = new MyEntity();
    return id == 1;
  });

Measuring elapsed time

As the documentation for System.Diagnostics.Stopwatch says:

In a typical Stopwatch scenario, you call the Start method, then eventually call the Stop method, and then you check elapsed time using the Elapsed property.

I find it a shame that they didn’t provide a method that covers this particular scenario so i did it myself:

public static class Stopwatch
{
 public static TimeSpan Measure(this Action action)
 {
  var stopwatch = new System.Diagnostics.Stopwatch();
  stopwatch.Start();
  action.Invoke();
  stopwatch.Stop();
  return stopwatch.Elapsed;
 }
}

Consuming this method is as simple as:

var duration = Stopwatch.Measure(() => FindElement(10000));
Console.WriteLine("It took {0:0} seconds.", duration.TotalSeconds);

Sparkles

In my opinion Pieter Gheysens is one of those persons that has contributed something valuable to the Belgian .Net community: Great Visug sessions and some interesting courses at Compuware so i shamelessly plug his new company Sparkles here. Now go and find out how they can make you a better developer!

Easy deployment through virtualisation

A couple of years ago, i think somewhere in 2003, i realised that hardware virtualisation was the way to go. Whenever i talked with people about distributing and deploying applications with a virtual machine image instead of a traditional installer they laughed at me (or atleast gave me a strange look of disbelief). The availability of multiple machines can be represented in a windowing environment through a metaphor of multiple desktops. Anyway, i’m still convinced that there will be a point in time where users have access to multiple applications that run in their own virtual machine. Time will tell.

Enterprise Service Bus

I believe it was at an Alt.Net meeting that the Enterprise Service Bus book was mentionned and i recently decided to give it a read. This book covers everything there is to know about an ESB. From a developer point of view it can serve as a good introduction to the core components and applied patterns but nothing more.