The Bitter Coder Tutorials, Binsor Style

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….

Zemanta Pixie
Advertisements

About Ruprict

I am a nerd that is a Nerd Wannabe. I have more kids than should be allowed by law, a lovely wife, and a different sense of humor than most. I work in the field of GIS, where I am still trying to find myself on the map. View all posts by Ruprict

11 responses to “The Bitter Coder Tutorials, Binsor Style

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: