I am trying to get an understanding of Inversion of Control and the dos and donts of this. Of all the articles I read, there is one by Mark Seemann (which is widely linked to in SO) which strongly asks folks not to use the service locator pattern.
Then somewhere along the way, I came across this article by Ken where he helps us build our own IoC.
I noticed that is is nothing but an implementation of service locator pattern.
- Is my observation correct that this implementation is the service locator pattern?
- If the answer to 1. is yes, then Do all IoC containers (like Autofac) use the service locator pattern?
- If the answer to 1. is no, then why is this differen?
- Is there any other pattern (other than DI) for inversion of control?
Consolidation of answers:
Answer 1. Kelly: The service locator pattern is indeed used for the initial Resolve call. The service locator pattern has to be used at least once.
Answer 2. Eric: Simply calling Ioc.Resolve(ISomeInterface) doesn't mean that the IoC container is being used as a Service Locator. After all, Resolve() has to be called somewhere, right?
Answer 3. Tungano: No, his code contains a constructor receiving a dependency... No, a container doesn't 'use' any pattern. It's really up to the programmer using the container to use a pattern. It's all in how you wire it together. Basically you can both use a container for DI and Service locator.
Answer 4. Gnat: ...factory and lookup... [can be used for inversion of control.]
I have learnt, thanks to the above answers that IoC itself uses Service Locator Pattern. This is not inherently harmful. Using the IoC can be done in a number of ways (including the Service Locator Pattern). This is the part we need to take care and use other options of injecting the dependency.