Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.
:)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top