使用服务定位器反模式和使用Castle Windsor容器有什么区别? [英] What is the difference between using the Service Locator anti-pattern and using the Castle Windsor container?"

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

问题描述

最近,我一直在尝试了解使用服务定位器反模式"与使用Castle Windsor容器之间的区别.我在Internet上到处都有一些信息,并且在未完成的博文.

Recently, I have been trying to understand what is the difference between using the Service Locator "anti-pattern" and using the Castle Windsor container. I have found some info here and there on the Internet and I have summarized what I have learned so far in an unfinished blog post.

到目前为止,我一直都在考虑依赖注入是保证分离的全部必要条件.但是在我所看到的任何地方,我都能看到诸如温莎城堡之类的集装箱的发展方向.我想清楚地了解原因. 请... 向我解释一下,就像我6岁:)

Until now I have been thinking that Dependency Injection is all one would need to guarantee separation. But everywhere I look I see a push in the direction of containers such as Castle Windsor. I would like to clearly understand the reasons. Please... Explain this to me like I'm a 6 year old :)

推荐答案

有趣的是,您应该要求对它进行解释,就像您六岁一样;这是一个像您五岁时一样的解释:)

Funny you should ask to have it explained like you were six years old; here's an explanation like you were five years old :)

我看到的每个地方都向着诸如温莎城堡之类的集装箱的方向推进

everywhere I look I see a push in the direction of containers such as Castle Windsor

坦率地说,我认为这是因为大多数人实际上不了解什么是依赖注入,这意味着他们不去理解控制反转的概念,而是在寻找替代new关键字的方法.他们已经习惯了.然后他们找到一个DI容器并将其误用作服务定位器.不幸的是,这很容易做到.

Frankly, I think the reason for that is that most people actually don't understand what Dependency Injection is, which means that instead of grasping the concept of Inversion of Control, they go looking for a replacement for the new keyword they're already used to. Then they find a DI Container and (mis)use it as a Service Locator. Unfortunately, that's very easy to do.

这就是为什么我的书,我解释了所有DI概念,而没有将解释与任何单个DI Container耦合在一起.实际上,这是本书的大部分内容.

This is the reason why, in my book, I explain all the DI concepts without coupling the explanation to any single DI Container. That's actually the majority of the book.

服务定位器和依赖注入是实现松散耦合的两种根本不同的尝试.服务定位器有很多缺点,并且没有提供DI所没有的优点.这就是为什么我认为将Service Locator称为反模式是安全的.

Service Locator and Dependency Injection are two fundamentally different attempts at achieving loose coupling. Service Locator has many disadvantages, and offers no advantages not also offered by DI. This is why I think it's safe to call Service Locator an anti-pattern.

您不需要DI容器即可使用DI;实际上,我会说除非您采用相当复杂的方法,否则最好避免一个.

You don't need a DI Container to use DI; in fact, I would say that unless you take a rather sophisticated approach, it's probably better to avoid one.

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

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