Symfony2 当听众比服务更可取时? [英] Symfony2 when listeners are preferable than services?
问题描述
我知道,在大多数情况下,服务更可取,因为它们直接在调用站点启动,这种方法使代码更加清晰.
I know, that in most cases services preferable, because they are initiated directly at the call site and this approach making code more clear.
虽然更难推理,但当事情分散到事件中时,系统会做什么.
While harder to reason, what a system doing, when things are spread out over events.
- 何时使用侦听器比使用服务更好?
- 如何使用监听器并保持代码清晰?
请举几个例子.任何帮助,将不胜感激!非常感谢!
Please, give some examples. Any help would be appreciated! Thanks a lot!
推荐答案
当您创建不应由其客户端修改但仍可扩展的可重用包/组件时,侦听器是有意义的.
Listeners make sense when you create a reusable bundle/component that should not be modified by its clients but still be extensible.
侦听器对应用程序代码没有多大意义,因为它们添加了一个间接级别,这使得更难弄清楚发生了什么.
Listeners don't make much sense for application code because they add a level of indirection that makes it harder to figure out what's going on.
这篇关于Symfony2 当听众比服务更可取时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!