I blather on and on about Windsor and how I adore it, much to the annoyance of the other devs that are forced to work with me. My last few uses of the Windsor container used Binsor as the configuration language, and some of my chums that are new to the Windsor/DI landscape have had to go from not using to Windsor to using it with Binsor. There are not very many good Binsor tutorials out there that I could find, so I figured I would take some of the best Windsor tutorials and redo them using Binsor. These tutorials are, of course, the Bitter Coder Tutorials on Windsor and they are wonderful. I figured someone would have done this by now, but I couldn’t find it, so if this is a duplication of someone else’s effort, I duly apologize.
First off, a brief definition of Binsor. Binsor is a Domain Specific Language (DSL) written in Boo with the specific purpose of configuring the Windsor container. The “default” configuration option for Windsor (and most .NET based configuration approaches) is XML, which is fine. However, XML can quickly get unwieldy, has no ability to perform any real logic, and (let’s face it) is just not sexy.
So, off we go, to the first tutorial, found here.
Simple Configuration
So, before we get to the code, in order to get Binsor to work, you’ll need the following references:
From Rhino Tools (http://sourceforge.net/projects/rhino-tools/)
- Boo.Lang
- Boo.Lang.Compiler
- Boo.Lang.Extensions
- Boo.Lang.Parser
- Rhino.Commons.Binsor
- Rhino.Commons.Clr
- Rhino.DSL
From Castle (http://www.castleproject.org/)
- Castle.Core
- Castle.DynamicProxy2
- Castle.MicroKernal
- Castle.Windsor
My first solution has 2 projects, one class library and one console app. The above references all go on the console app only.
Our tax calculator looks just like the Bitter Coder’s
namespace BitterCoder.Tutorials.Binsor.Core
{
public class TaxCalculator
{
private decimal _rate = 0.125m;
public decimal Rate
{
set { _rate = value; }
get { return _rate; }
}
public decimal CalculateTax(decimal gross)
{
return Math.Round(_rate * gross, 2);
}
}
}
Here’s where we take a different path from the XML configuration. Setting up the container looks for the Binsor script named here (“Windsor.boo”) When using Binsor, make sure you set the right build properties for the .boo file. In this case, we want it to be copied to the output directory. If, however, you were working in a web environment, you could just mark the file as content and it’ll be copied to the root of your directory. Now, let’s look at the console app and the setting up of the container:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.Core;
using Castle.Windsor;
using Rhino.Commons.Binsor;
using BitterCoder.Tutorials.Binsor.Core;
namespace BitterCoder.Tutorials.Binsor.ConsoleTester
{
class Program
{
static void Main(string[] args)
{
IWindsorContainer container = new WindsorContainer().Install(BinsorScript.FromFile(“windsor.boo”));
TaxCalculator calculator = container.Resolve<TaxCalculator>();
decimal gross = 100;
decimal tax = calculator.CalculateTax(gross);
Console.WriteLine(“Gross: {0}, Tax: {1}”, gross, tax);
Console.Read();
}
}
}
So, still just one line…groovy. We use the Rhino.Common.Bindsor.BinsorScript class to read windsor.boo and convert it to the parameters windsor needs. Let’s look at the windsor.boo file now:
import System
import System.Reflection
import BitterCoder.Tutorials.Binsor.Core
component “tax.calculator”, TaxCalculator:
Rate=Convert.ToDecimal(0.25)
Five lines! That’s it! And now you see one of the reasons why people like to use Binsor. It’s concise, readable, and can contain logic. As you can see, I have access to any class in .NET that I wish to import, shown by my Convet.ToDecimal call to convert the rate to a decimal.
If you run the console app, you’ll see: Gross: 100, Tax: 25.00 just like in the other tutorial. Go ahead, mess with the rate and run it again. It works.
So, that’s the first one down. Next time we look at configuring arrays….

July 11th, 2008 at 10:54 pm
Yes!!!
This is great stuff!
July 16th, 2008 at 12:11 am
[...] Part 1 [...]
July 28th, 2008 at 6:01 pm
[...] Part 1 [...]
August 4th, 2008 at 7:06 pm
[...] Part 1 [...]
August 11th, 2008 at 10:47 pm
[...] Part 1 [...]
August 18th, 2008 at 8:37 pm
[...] Part 1 [...]
August 21st, 2008 at 6:16 am
[...] Part 1 [...]
September 9th, 2008 at 1:17 am
[...] reading this post I referenced the following assemblies to my project (these can be found in the build directory [...]
October 16th, 2008 at 5:29 am
[...] Part 1 [...]
May 31st, 2010 at 11:00 pm
Wow, this works well, but it gave me a couple of problems.
The first is that I needed to add a binding redirect for Boo.Lang.Compiler into my app.config to get this to run at all.
The second is the boo syntax above has had extended characters added by a word processor and needs re-typing. You land up with all sorts of funny errors, including complaints about line endings (RPAREN in boo compiler speak).
June 1st, 2010 at 5:29 pm
Hey Adam,
Yeah, this project likely could use a porting to the latest Windsor 2.0 code. I haven’t looked at it in awhile. Thanks for finding the issues.
Glenn