加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

依赖注入 – 为什么MVC4使用服务定位器反模式?

发布时间:2020-12-14 01:18:01 所属栏目:百科 来源:网络整理
导读:阅读 “Dependency Injection in .NET”之后Mark Seemann我离开 Service Locator这是一个反模式。 阅读the release notes on MVC 4我看到: Improved Inversion of Control (IoC) via DependencyResolver: Web API now uses the service locator pattern imp
阅读 “Dependency Injection in .NET”之后Mark Seemann我离开 Service Locator这是一个反模式。

阅读the release notes on MVC 4我看到:

Improved Inversion of Control (IoC) via DependencyResolver: Web API
now uses the service locator pattern implemented by MVC’s dependency
resolver to obtain instances for many different facilities.

因此,我对于为什么Microsoft将在2012年使用服务定位器感到好奇和困惑。

这是一个你不应该关心的实现细节。重要的是,现在Web API使用DependencyResolver来解析许多不同设施的依赖关系,只要你想插入这些设施,就可以使用真正的依赖注入。所以在你的代码中,你将使用一个真正的依赖注入。如果微软没有使用DependencyResolver,那么你必须在你的代码中使用它(作为服务定位器反模式),以解决依赖关系,当你想实现一些自定义功能。这对你来说是不好的。现在对于微软是不好的,但你不在乎他们。

Thus I’m left curious and confused why Microsoft would use a service locator in 2012.

因为设计框架与使用框架设计应用程序不同。在设计可重用的框架(如ASP.NET MVC)时,需要考虑一些不同的事情,而不仅仅是书中的内容。一些例子是以这样的方式设计框架,使得使用该框架的人将能够利用在他的代码中使用该框架在书中编写的最佳实践的优势。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读