views:

179

answers:

1

DISCLAIMER: I know there is debate between DI and service locator patterns. I have a question that is intended to avoid the debate. This question is for the service locator fans, who happen to think like Fowler "DI...is hard to understand...on the whole I prefer to avoid it unless I need it." For the purposes of my question, I must avoid DI (reasons intentionally not given), so I'm not trying to spark a debate unrelated to my question.

QUESTION: The only issue I might see with keeping my IOC container in a singleton (remember my disclaimer above), is with the use of child containers. Presumably the child containers would not themselves be singletons. At first I thought that poses a real problem. But as I thought about it, I began to think that is precisely the behavior I want (the child containers are not singletons, and can be Disposed() at will).

Then my thoughts went further into a philosophical realm. Because I'm a service locator fan, I'm wondering just how necessary the notion of a child container is in the first place. In a small set of cases where I've seen the usefulness, it has either been to satisfy DI (which I'm mostly avoiding anyway), or the issue was solvable without recourse to the IOC container. My thoughts were partly inspired by the IServiceLocator interface which doesn't even bother to list a "GetChildContainer" method.

So my question is just that: if you are a service locator fan, have you found that child containers are usually moot? Otherwise, when have they been essential?

extra credit: If there are other philosophical issues with service locator in a singleton (aside from those posed by DI advocates), what are they?

+1  A: 

IMHO:

  • Child containers have nothing to with service locators, i.e. they're orthogonal. Using a container as a service locator is just that, a way to use it, and it has nothing to do with the container supporting child containers or not.
  • Child containers usage depend largely on container design. For example, while Windsor supports them, they're seldomly used. Autofac OTOH uses them heavily to manage scope / component lifecycles. And it's an entirely optional feature of any container / service locator implementation, that's why IServiceLocator doesn't mention it. IServiceLocator's job is to provide the lowest common denominator in a service locator.
Mauricio Scheffer
Your answer is exactly what I was looking for, and even better! You took the time to point out how frequently child-containers are used in various frameworks (a *very* useful tidbit)!!!Perhaps I could trouble you one more time: where does Unity fit in the "child container frequence" spectrum? Are child container uses in Unity more like Windsor (infrequent) or more like Autofac (frequently)?
Brent Arias
sorry, I have no significant experience with Unity.
Mauricio Scheffer