SDI - Simple Dependency Injection


#1

Hi,

I have just released SDI - Simple Dependency Injection framework.
It is really light and does not enforce anything on a user, does not use annotations, or any kind of XML. It helps also with structuring the code by separating creation logic from business logic.
Full Java implementation is less than 300 LOC of code.

Currently, it is implemented in Java only, but if you find it interesting I will create Scala version also.

Please let me know your comments and suggestions.
And for sure let me know if you want it in Scala :slight_smile:

/Marcin


#2

It might be interesting if in Scala you could prove at compile time that all dependencies are satisfied when you build a service.


#3

In fact, all dependencies must be statically available at compile time. After all, you have to code them up in creators.

But I think that you are referring to static DI like in MacWire? I didn’t think about such a functionality, and right now I don’t know how to implement that and keep at the same time current design principles. Current design encourages to encapsulate creation of objects in separate classes - creators, which is helpful for testing. If I follow the pattern of in-place creation, then I will have to give up this feature.

/Marcin


#4

To take the example from your readme, I guess the following would compile just fine:

Service service = Service.builder()
        .withMainClass(App.class)
        .withCreator(new AppCreator())
        .build();

But then service.get(App.class) would probably fail at runtime, because the service doesn’t know how to create a Config.

I haven’t thought it through in detail but I think in Scala you could check this at compile time if you integrate with something like Shapeless.


#5

I will definitely think about it.
Any suggestions regarding Scala implementation are warmly welcome.
:slight_smile:


#6

The built-in ability of dependency injection is called “cake pattern”, which is simply nested trait.

I applied this cake pattern in many projects. For example, plugin in DeepLearning.scala is one of these use cases.