Smalltalk 有依赖注入框架吗?
我正在运行 Pharo,我只是在一个用例中,需要依赖注入
我知道您可以通过显式传递依赖项来步行完成这一切。但这对我来说感觉很尴尬和冗长。
I'm running Pharo and I'm just in a use case that sort of screams for Dependency Injection à la Guice. Is there something similar for Smalltalk?
I understand that you can sort of do it all by foot, by just passing in your dependencies explicitly. But that feels awkward and verbose to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一种 Smalltalk 方言非常强调依赖注入。它扩展了语言,使得不仅方法名称而且类名称都使用动态查找。类名的新颖查找与方法的查找最为相似,只不过是通过一系列嵌套类而不是沿着继承链向上冒泡。因此,您可以通过更改嵌套环境来更改注入的类。
要了解有关该方言的更多信息,请点击此链接。
There is a Smalltalk dialect with strong emphasis on dependency injection. It extends the language such that not only method names but also class names use a dynamic lookup. The novel lookup of class names is most similar to that of methods, except that bubbles up through a series of nested classes rather than along an inheritance chain. Thus you can change the injected classes by changing the nesting environment.
To learn more about the dialect, follow this link.
使用 Guice,看起来就像您定义类以将某些接口作为构造函数参数一样。然后你告诉 Guice“这个接口映射到实现所述接口的类”。
这种事情在 Smalltalk 中是完全没有必要的,因为 Smalltalk 类只关心协议。
如果我们将示例翻译成 Smalltalk,我们可以将任何我们喜欢的对象传递到 RealBillingService 的构造函数中,只要该对象响应 #logChargeResult: 和 #logConnectException:,即只要该对象实现 TransactionLog 所需的协议。
这里有一个链接,指向与上述类似的答案。
With Guice, it looks like you define your classes to take certain interfaces as constructor parameters. Then you tell Guice "this interface maps to that class implementing said interface".
That sort've thing is completely unnecessary in Smalltalk, because Smalltalk classes only care about protocols.
If we translated the example into Smalltalk, we could pass any object we liked into the RealBillingService's constructor, as long as that object responded to #logChargeResult: and #logConnectException:, i.e., as long as that object implemented the protocol required of a TransactionLog.
Here's a link to a similar answer to the above.
我并不是真正的专家,但我在谷歌上找到了这篇文章: http://codebetter.com/blogs/jeremy.miller/archive/2006/05/05/144172.aspx
我希望这会引导您走向正确的方向。
:)
I am not really an expert but I found this article on google: http://codebetter.com/blogs/jeremy.miller/archive/2006/05/05/144172.aspx
I hope this will lead you in the right direction.
:)