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; }
}
}
component “adding.service”, AddingService
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”
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.