How would one do dependency injection in scala?

前端 未结 9 1623
鱼传尺愫
鱼传尺愫 2021-01-30 12:25

I\'m still at the beginning in learning scala in addition to java and i didn\'t get it how is one supposed to do DI there? can or should i use an existing DI library, should it

9条回答
  •  抹茶落季
    2021-01-30 12:57

    A recent project illustrates a DI based purely on constructor injection: zalando/grafter

    What's wrong with constructor injection again?

    There are many libraries or approaches for doing dependency injection in Scala. Grafter goes back to the fundamentals of dependency injection by just using constructor injection: no reflection, no xml, no annotations, no inheritance or self-types.

    Then, Grafter add to constructor injection just the necessary support to:

    • instantiate a component-based application from a configuration
    • fine-tune the wiring (create singletons)
    • test the application by replacing components
    • start / stop the application

    Grafter is targeting every possible application because it focuses on associating just 3 ideas:

    • case classes and interfaces for components
    • Reader instances and shapeless for the configuration
    • tree rewriting and kiama for everything else!

提交回复
热议问题