为什么使用MVC4服务定位器反模式? [英] Why is MVC4 using the Service Locator Anti-Pattern?

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

问题描述

依赖注入.NET通过马克·塞曼后,我从的Service地点这是一种反模式。

After reading "Dependency Injection in .NET" by Mark Seemann I stay away from the Service Locator which is an anti-pattern.

在阅读的释放对MVC 4 指出我看到:

通过改进控制DependencyResolver(IOC)的反转:网页API
  现在使用由MVC的依赖实现的服务定位器模式
  解析器以获得许多不同的设备的实例。

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.

于是我离开了好奇和困惑,为什么微软将在2012年使用服务定位器

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

推荐答案

这是一个实现细节,你不应该关心。重要的是,现在的Web API使用DependencyResolver来解决依赖于许多不同的设施,你将能够每当你想插入到这些设施中使用真实的依赖注入。因此,在您code你会使用一个真正的依赖注入。如果微软没有使用 DependencyResolver 那么它会一直你,这样才能保证您的code都用它(作为服务定位器反模式),以当你想实现一些自定义功能解决依赖关系。这本来的对你不好的。现在,它的的坏的微软的,但你不关心他们。

That's an implementation detail that you shouldn't care about. The important thing is that now that the Web API uses the DependencyResolver to resolve dependencies for many different facilities, you will be able to use a real dependency injection whenever you want to plug into those facilities. So in your code you will be using a real dependency injection. If Microsoft didn't use the DependencyResolver then it would have been you that must have used it (as a service locator anti-pattern) in your code in order to resolve dependencies when you want to implement some custom functionality. This would have been bad for you. Now it's bad for Microsoft but you don't care about them.

于是我离开了好奇和困惑,为什么微软会在2012年使用服务定位器。

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

由于设计的框架是不一样的设计使用框架的应用程序。有一些不同的东西设计一个可重用的框架,如ASP.NET MVC,而不仅仅是什么写在书时考虑到。一些例子是设计的框架以这样的方式,使用该框架的人将能够采取的使用这个框架写在书本在他code中的最佳实践优势。

Because designing a framework is not the same as designing an application using a framework. There are some different things to take into consideration when designing a reusable framework such as ASP.NET MVC rather than just what's written in the books. Some example is to design the framework in such a way that a person using this framework will be able to take advantage of the best practices written in the books in his code using this framework.

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

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