IOC child container / service locator

DISCLAIMER: I know there is a dispute between DI and service localization patterns. I have a question to avoid debate. This question is for fans of the service locator who seem to think that Fowler is "CI ... hard to understand ... in general I prefer to avoid it unless I need it." For the purposes of my question, I have to avoid DI (reasons not intentionally given), so I am not trying to create controversy unrelated to my question.

QUESTION: The only problem I could run into while keeping my IOC container in singleton mode (remember my disclaimer above) is using child containers. Presumably, the baby containers themselves would not be single. At first I thought this poses a real problem. But when I thought about it, I started thinking what exactly this behavior I want (baby containers are not single, and can be Disposed () at will).

Then my thoughts went further into the philosophical sphere. Since I'm a fan of the service locator, I'm wondering how important it is to understand the concept of a child container. In a small set of cases where I saw the utility, it had to satisfy the DI (which I generally avoid), or the problem was resolvable without going to the IOC container. My thoughts were partially inspired by the IServiceLocator interface , which doesn't even bother to enumerate the GetChildContainer method.

So my question is: if you are a fan of the service locator, have you found that baby containers are usually controversial? If not, when were they needed?

Extra credit: If there are other philosophical problems with service locator in singleton mode (besides those posed by DI proponents), what are they?

+2


a source to share


1 answer


IMHO:



  • Child containers have nothing to do with service locators, that is, they are orthogonal. Using a container as a service locator is just the way to use it, and it has nothing to do with a container supporting child containers or not.
  • The use of baby containers depends a lot on the design of the container. For example, while Windsor supports them, they are rarely used. Autofac OTOH uses them for scope / component lifecycle management. And this is a completely optional feature of any container / service locator implementation, so the IServiceLocator doesn't mention it. The IServiceLocator's job is to provide the lowest common denominator in the service locator.
+3


a source







All Articles