views:

266

answers:

1

From what I understand IServiceLocator is an interface to abstract the actual IoC container away? I'm asking with relation to Prism where I'm trying to replace Unity with Prism, and I see Prism-classes relying on IServiceLocator. Could someone please clarify the role of the interface and when it is used?

And also; what is the Common Service Locator, and will this be helpful when working with IServiceLocator?

+2  A: 

IServiceLocator is an abstraction of a service locator. IoC containers are kind of supersets of service locators so they can be represented by IServiceLocator. However, the service locator pattern is more intrusive than the pure dependency injection pattern, therefore you should try to favor the latter whenever possible.

The CommonServiceLocator provides the IServiceLocator interface and an abstract implementation of this interface to define common behavior and help IoC containers define their IServiceLocator adapter (every major IoC container provides such an adapter).

See also:

Mauricio Scheffer
Thx! Good answer. Can't find an IServiceLocator adaptor for Ninject though, but maybe it isn't considered major? (ref *(every major IoC container provides such an adapter)*)
stiank81
see http://github.com/enkari/ninject/tree/2.0.0.0/src/CommonServiceLocator.NinjectAdapter/
Mauricio Scheffer
Cool - thx. Already wrote the same thing myself though..
stiank81