As far as i know dependency injection is not the same as service location. but the Unity application block contains the Common Service Locator library (which i think is a service locator). how that library should be used and when should it be used, and is it being used by unity itself for dependency injection ?
NOTE: Please do not quote from CommonServiceLocator CodePlex homepage on its usage.

- 19,178
- 4
- 69
- 95
2 Answers
The Common Service Locator interface is intended for library authors who want to use a DI container without forcing the library's choice of container on the apps using the library. The intention is that inside the library where you need to resolve objects, you'd code against the CSL interface instead; then you can use whichever container the app calling you is using.
If you aren't writing a reusable library component, don't use the CSL. It's an additional layer that won't buy you anything.

- 29,165
- 4
- 46
- 63
-
i think you are right, but can you please provide a link about how it is being used that way. – Ibrahim Najjar Nov 18 '12 at 14:05
-
@Sniffer Have a look at the source code of [Enterprise Library](http://entlib.codeplex.com). – Sebastian Weber Nov 19 '12 at 07:35
-
@SebastianWeber i will try and give it a look. – Ibrahim Najjar Nov 19 '12 at 09:47
No, Dependency Injection is not the same thing as Service Location, however they do share a lot of functional similarities. What they do not share is more a philosophical approach.
Both provide access to objects as configured in your application. However, Dependency Injection says that you should never explicitly ask for an object (or interface). You should instead SPECIFY an object or interface, and the framework will deal with hooking everything up for you.
Service Location is where you request an object directly from the Service Locator system.
To put another way, Service Location requires that you find the object you want. Dependency Injection assumes you are created with the objects you need already in place.

- 92,674
- 28
- 195
- 291
-
True words, but the main confusion is why it is being used by Unity which is a DI and MVVM Light Toolkit. Edit: i think @Chris Tavares provided the real reason. – Ibrahim Najjar Nov 18 '12 at 14:03