views:

55

answers:

1

Hello

I'm trying to understand how DI exactly works. I'm currently using Windsor as DI container. I use this to load my services dynamically in code without direct reference. But I have change behaviour and want to know a bit more on the instance mgmt using DI.

I have a web app projct, here is a WCF service using PerCall as instancemode. This means, new instance/call. In this WCF I call a service (loaded via DI) and this service calls another service (again loaded via DI). The WCF is a new instance in the appdomain, but what about the services. They are also new instances? Is this DI container shared among all WCF instances and are the services in this container also single instances?

Can anyone clarify?

+1  A: 

It's hard to answer when your description is so vague. Few points though:

  • Components should not ''share'' container. Components should not ever have reference to the container.

  • Have one root container instance per your entire application. In webapp you would usually instantiate and configure it in ApplicationStart and let it do its job from there.

  • be wary not to take dependency on shorter-living components. For example singleton components should not take dependency on transient components.

Krzysztof Koźmic
It really looks like I need to learn what exactly happens in the container. Currently in the application start, I do create that container based on what is in my config file. (the services). When I need a service, I pass it to the constructor. There the magic happens, I get an instance of that service. But how long does this instance stay alive? For instance if I use a service in the MVC controller. How long does the controller stay a live? Until the request is done?
Sven
It depends on the lifestyle. Why don't you take a look at the documentation and user's guide? http://castleproject.org/container/documentation/v21/index.html
Krzysztof Koźmic