What is the IServiceLocator interface?

孤者浪人 提交于 2019-12-10 13:05:54

问题


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?


回答1:


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:

  • http://martinfowler.com/articles/injection.html#ServiceLocatorVsDependencyInjection
  • http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html


来源:https://stackoverflow.com/questions/2621316/what-is-the-iservicelocator-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!