Daniel Hoelbling-Inzko talks about programming

Extensibility can equal configurability

Posted by Daniel Hölbling on February 2, 2009

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

        type="Blog_Sample.Worker, Blog_Sample" />
        service="Blog_Sample.IValueCalculator, Blog_Sample"
        type="Blog_Sample.AlternativeCalculator, Blog_Sample" />

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.

Filed under castle, programmierung
comments powered by Disqus

My Photography business


dynamic css for .NET