Tigraine
Daniel Hoelbling talks about programming

Extensibility can equal configurability

February 2nd, 2009 . by Daniel Hölbling

The following code is extensible and configurable:

public class Worker
{
    private IValueCalculator valueCalculator = new DefaultValueCalculator();

    public IValueCalculator ValueCalculator
    {
        get { return valueCalculator; }
        set { valueCalculator = value; }
    }

    public decimal Work(int number)
    {
        return valueCalculator.Calculate(number);
    }
}

What happens here is that I am using the strategy pattern to implement different behaviors to keep my Worker class safe from changes to the calculator code.

Basically I’m doing dependency injection here, but I don’t inject the class through the constructor but through setter injection.

Since I am not bound to the construction phase of the object, I can easily swap IValueCalculator implementations during the worker’s lifetime without having to reconstruct the whole object.

Now, why is this extensible AND configurable?

It’s extensible because it’s easy to implement the IValueCalculator interface and supply it to a worker instance, without changing any of the plumbing around it.

If I want to change the behavior for just one call i can do that very easily:

var worker = new Worker();
var oldCalculator = worker.ValueCalculator;
worker.ValueCalculator = new AlternativeCalculator();
worker.Work(1701);
worker.ValueCalculator = oldCalculator;

But the real beauty of the whole thing is that an inversion of control container like Castle Windsor can also inject setters, so in absence of a configuration file, the default implementation from the code will be used.

But once a Windsor configuration is found you can swap the strategy classes through the configuration even without recompiling like this:

<components>
    <component
        id="Worker"
        type="Blog_Sample.Worker, Blog_Sample" />
    <component
        id="Alternative.Calculator"
        service="Blog_Sample.IValueCalculator, Blog_Sample"
        type="Blog_Sample.AlternativeCalculator, Blog_Sample" />
</components>

If you want the default behavior just delete the Alternative.Calculator component and no setter injection will happen. If a service implementing IValueCalculator is present that one will be injected to the Worker.


  • http://www.tigraine.at/2009/02/27/simplest-thing-to-do/ Simplest thing to do | Tigraine

    [...] Also mind that since everything follows a rather simple Interface I can easily change these implementations later if I need to. But for something simple, this will do just fine. Also I advise to use this through an IoC container like Windsor so you could even go as far as to swap hardcoded translations through the configuration. [...]

  • http://www.geniusshoppers.com/mousepads.html Warcraft Accessories

    Hello!I am following your blog for many weeks now. I have to admit that it is very informative. It is already in my favourite list and i will try to follow it when possible . Thanks for the interesting inputs .