Search code examples
springdependency-injectionguicepicocontainer

Benefits of implementation driven & dependency injection vs cost of maintaining implementations


I am starting an application that I would like to build quickly and will be later developed by 20+ developers.

Knowing what you know about DI in an environment with multiple developers, would you go with DI for a new application you'd like to build relatively fast?

The costs of using DI to me right now, would be lines of code written and generated vs not using an interface for every object. And down the line I am hoping DI doesn't become a performance problem because of reflection.


Solution

  • Simply said, doing Object-oriented development without dependency injection is bad. Very bad. Dependency injection is in my opinion the root of well-understood OO development: separation of concerns, loose coupling between independent components, coding in layers, all of that is done using DI. And also, it makes testing incredibly easier, and allows techniques like TDD, mocking (BDD), etc.

    As Shakyamuni said in his answer, DI is not about the framework you use. Spring did not invent DI, it just proposes a solution to achieve it. So if your original question is "should we use Spring", then I'd say it's a question of personal taste. You can achieve exactly the same result if you do it yourself. I've worked in project with or without containers (Spring, pico, etc.) and they all have their benefits and drawbacks. My personal preference, though, is not to use any of it and manage your DI yourself.

    This is DI:

    // constructor
    public MyClass(MyDependencyInterface injected) {
        this.dependency = injected;
    }
    

    or that:

    // setter injection
    public void setDependency(MyDependencyInterface injected) {
        this.dependency = injected;
    }
    

    And down the line I am hoping DI doesn't become a performance problem because of reflection.

    No idea what you mean by that. DI does not require reflection.