你什么时候会使用公共服务定位​​器? [英] When would you use the Common Service Locator?

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

问题描述

我一直在看公共服务定位​​器作为的抽象我的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.

通用服务定位器,您可以用上面的应对,而不必强制给国际奥委会的用户。

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

在您的库本身,你可能希望在国际奥委会注册自己的班,现在它得到了很多困难,因为你需要选择一个国际奥委会自己使用,不会在你的来电的方式获得。

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天全站免登陆