The Bitter Coder Tutorials, Binsor Style VI: Switching Lifestyles

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:

@lifestyle=”transient”

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.

About these ads

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

2 responses to “The Bitter Coder Tutorials, Binsor Style VI: Switching Lifestyles

  • Michael

    Thanks for this really usefull post.
    I keep getting the error message.
    failed: Could not compile configuration!
    Boo.Lang.Compiler.CompilerError: expecting “RPAREN”, found ‘:’. Boo.Lang.Compiler.CompilerError: Unexpected token: @lifestyle.

    Any ideas? Could you post the code for your example so I could compare it with mine?

    Cheers,

  • ruprict

    Micheal,

    That is my code above. However, the first version of this post did not have the @lifestyle line indented, which could cause issues. If that doesn’t fix yours, you can send me your code at glenn(at)goodrichs(dot)net and I’ll take a gander.

    Glenn

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: