Composite design pattern in C#, the pragmatic way

A simple implementation of the composite design pattern with generics.

You can deal with state and further behavior in infinite different ways. You can use a context parameter and pass that to the ExecuteRule method. Each rule is then able to modify the context. Another way is to have a return value, the return value is an aggregated AND result of all the rules. Or you can define a property in the interface that keeps the state in each IRule and you have a separate construction wich aggregates this from the composites.

These are just a few ways. There are of course infinite more way's to handle this.

Implementing a recording Stub with Jasmine Javascript testing framework.

Jasmine is so lovely. Last week I was playing with a nice little (bdd) unit testing framework for Javascript. I've found this nice framework when I was looking for a good unit testing framework for Javascript. But neither the yahoo library or the Qunit library had the API and the granularity I wanted. So I stumbled across Jasmine which is essentially a BDD testing framework for Javascript. It has a very elegant and simplistic syntax which is perfect to practice test driven or behavior driven development with Javascript. It also has a nice integration with a different build systems like rake and maven.

I was trying to port the exercise that I used for test driven development workshops in C# and Java. In this exercise I rely on mocking and especially on calling a mocked / stubbed method either manually or with a framework. This method has to be called multiple times.. Jasmine has done nice way of mocking it is called a spy. But you cannot call a spy multiple times and let the spy return a value the first time it is called and throw an exception the second time. So this was the perfect opportunity for me to give this a little test drive to implement my own stub object / method. Here's an example how I want to use the stub.

Yes I am a big fan of fluent syntax in frameworks, this is also a reason why I like Jasmine. So the next step was to start writing my tests with Jasmine. I decided to start simple and just use the HTML test runner. I copied the default test runner, added a script tag, opened the Jasmine wiki page on github and started coding. Since I am not a JavaScript expert I looked a lot at how will the spy was implemented in Jasmine.


The result is a fully functional stop that uses fluent syntax to record your calls. This project can be found on github, the file can be found here static/js/frameworks/spec/jasminestubextensionSpecs.js The next steps for me are to use Jasmine in a real world project, to use the features of continuous integration, together with JSHint and rock!

Loops in msbuild and custom meta-data on itemgroups

When you want to iterate over a list of properties or Itemgroups wich do not contain files or filegroups your mind boggles. For example I want execute a task for each item in a Itemgroup, how do I do that?

The short answer:

The long answer:

Itemgroups, are lists where each item contains default meta-data and each item can contain custom defined meta-data. This is an extremely powerful concept, you can define anything on an item. This data can be accessed in a Task. Because you can define input in a Task, and you can define a Itemgroup as input, you can execute a task for each item in a item group.

This is how you define a itemgroup with custom meta-data:

With these basic examples and concepts you can create extremely powerful and modularized build scripts, always remember DRY! (This is how I came to use this)

For more information about msbuild, you can always use msdn. But the real tip is this book: Inside the Microsoft Build Engine: Using MSBuild and Team Foundation Build there is way more information on msbuild than the title indicates, the tfs stuff is minimal.

Have fun!

Download the example

In Msbuid 4.0 you can also use poperty functions to achieve much, a cool recursive loop example is provided. Or use an inline task.