什么时候使用通用服务定位器? [英] When would you use the Common Service Locator?

本文介绍了什么时候使用通用服务定位器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在查看通用服务定位器,作为抽象IoC容器的一种方法,已经注意到有些人强烈反对这种类型。

I've been looking at the Common Service Locator as a way of abstracting my IoC container but I've been noticing that some people are strongly against this type of this.

人们建议不要使用它吗?一直使用它?或有时使用它?
如果有时候,那么在什么情况下你会使用它,什么情况下你不会使用它。

Do people recommend never using it? Always using it? or sometimes using it? If sometimes, then in what situations would you use it and what situations would you not use it.

推荐答案

想象一下您正在编写第三方开发人员使用的图书馆代码。您的代码需要能够创建这些开发人员提供的服务对象。但是您不知道每个呼叫者将使用哪个IoC容器。

Imagine you are writing library code to be used by 3rd party developers. Your code needs to be able to create service objects that these developers provide. However you don’t know which IoC container each of your callers will be using.

通用服务定位器可让您处理上述情况,而不会强制给用户提供给定的IoC。

The Common Service Locator lets you cope with the above without forcing a given IoC on your users.

内您的图书馆本身您可能希望在IoC中注册自己的课程,现在,您需要为自己的使用选择一个不会妨碍您的来电者的IoC,变得更加困难。

Within your library itself you may wish to register your own classes in the IoC, now it gets a lot harder as you need to choose a IoC for your own use that will not get in the way of your callers.

这篇关于什么时候使用通用服务定位器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆