Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a dependency injection framework for Smalltalk?

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.

like image 820
nes1983 Avatar asked Apr 21 '10 15:04

nes1983


People also ask

Which framework is used for dependency injection?

Spring.NET is one of the popular open source frameworks for Dependency Injection. Spring.NET supports . NET 4.0, . NET Client Profile 3.5 and 4.0, Silverlight 4.0 and 5.0, and Windows Phone 7.0 and 7.1.

Which is best dependency injection?

Constructor ControllerConstructor-based dependency injection is certainly considered a best practice. There was a time I personally favored setter-based injection, but I have come around to constructor-based. We can still improve our example.

What is partial dependency injection?

Partial dependency: can be injected using setter injection but it is not possible by constructor. Suppose there are 3 properties in a class, having 3 arg constructor and setters methods. In such case, if you want to pass information for only one property, it is possible by setter method only.


2 Answers

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.

like image 169
akuhn Avatar answered Oct 11 '22 12:10

akuhn


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.

like image 20
Frank Shearar Avatar answered Oct 11 '22 12:10

Frank Shearar