The Bitter Coder Tutorials, Binsor Style VI: Switching Lifestyles

Mon, Aug 4, 2008 2-minute read

Previous posts in the series:

In tutorial #6, Alex discusses lifestyles and how to muck about with them.  If you are unfamiliar with what lifestyles are in this context, go read his post first (which you should be doing anyway)

So, we create the same adding service component (minus the spelling errors ;)):

public class AddingService


private int _total = 0;

public void AddAmount(int amount)


_total += amount;



public int Total


get { return _total; }



And we add it to the Windsor configuration, like so:

component “adding.service”, AddingService

Nothing special.  When we run the code we get:

You have counted 75 sheep and herded 75 angry cats

This seems a bit odd, as the container is using the same instance for both AddingService requests.  Let’s make it give us a new one for each request.  This is accomplished by adding the “lifestyle” attribute to the component:

component “adding.service”, AddingService:


Notice the “@” sign, which tells Binsor to put this attribute on the Windsor component, but not the class instance (I can’t figure out a better way to word that.)  Now, when we run it, we get:

You have counted 60 sheep and herded 15 angry cats

Snot on.  Alex goes on to say how you can add an attribute on the class to indicate it’s lifestyle.  That isn’t really relevent to what I’m doing here, but you should check it out all the same.

Coming up….Switching Implementations, which is where a DI container really starts to make sense.