Matt
Matt

Reputation: 13349

When to allow Services to access the IoC Controller?

What are the scenarios in which it's OK to allow services created via IoC to have access back to the IoC controller to access other services? It seems to me that the short answer is never, but are there situations in which it's OK or even in which it makes sense?

Upvotes: 1

Views: 84

Answers (2)

Christian Horsdal
Christian Horsdal

Reputation: 4932

I think the answer is 'never', since it sounds like what you describe is service locator, which is an anti pattern

Upvotes: 6

Michiel van Oosterhout
Michiel van Oosterhout

Reputation: 23084

There's a rather well written article about this from Martin Fowler:

http://martinfowler.com/articles/injection.html#UsingAServiceLocator

A lot of this depends on the nature of the user of the service. If you are building an application with various classes that use a service, then a dependency from the application classes to the locator isn't a big deal. In my example of giving a Movie Lister to my friends, then using a service locator works quite well. All they need to do is to configure the locator to hook in the right service implementations, either through some configuration code or through a configuration file. In this kind of scenario I don't see the injector's inversion as providing anything compelling.

Upvotes: 0

Related Questions