Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 272 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding the need for a DI framework

#11
Don't forget one major disadvantage of dependency injection: you loose the ability to easily find out from where something is initialized using Find Usages of your powerful Java IDE. This might be a very serious point if you refactor a lot and want to avoid that the test code is 10 times bigger than the application code.
Reply

#12
Lately there has been very much emphasis on DI *frameworks*, even so that the DI *pattern* is being forgotten. DI's principles as [summarized by J. B. Rainsberger][1]:

> It's simple: make dependencies explicit by requiring collaborators as
> parameters in the constructor. Repeat until you've pushed all decisions
> about which objects to create into the entry point. Of course, this only
> applies to Services (in the DDD sense). Done.

As you've noticed, there is not much difference between configuring the dependencies manually vs. using a framework. With constructor injection, as shown below, your code example would have even less boilerplate and the compiler would force you to provide all required dependencies (and your IDE will probably type them for you).

SawMill sawMill = new SawMill(new HandSaw());
sawMill.run();

A DI framework can reduce the boilerplate of creating factories and wiring the objects together, but at the same time it can also make it harder to find out that where each dependency is coming from - the DI framework's configuration is one more layer of abstraction to dig through, and your IDE might not be able to tell you where a particular constructor is being called from.

An indirect disadvantage of DI frameworks is that they can make wiring the dependencies *too easy*. When you can't anymore [feel pain][2] from having lots of dependencies, you may just keep on adding more dependencies instead of rethinking the design of the application to reduce the coupling between the classes. Wiring the dependencies manually - especially in test code - makes it easier to notice when you have too many dependencies as the test setup becomes longer and it becomes harder to write unit tests.

Some advantages of DI frameworks come from their support of advanced features such as AOP (for example Spring's @Transactional), scoping (though many times [scoping with plain code][3] will be enough) and pluggability (if a plugin framework is really needed).

Recently I made an experiment of manual DI vs. framework-based DI. The progress and results are shown as screencasts in [Let's Code Dimdwarf episodes 42 through 47][4]. The project in question had a [Guice][5]-based plugin system for creating [actors][6], which I then rewrote using manual DI, without Guice. The result was a much simpler and clearer implementation, and only a little bit more boilerplate.

**Synopsis:** First try using just manual DI (preferably constructor injection). If there becomes lots of boilerplate, try to rethink the design to reduce the dependencies. Use a DI framework if some of its features provide value for you.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

[5]:

[To see links please register here]

[6]:

[To see links please register here]

Reply

#13
Dependency injection is a degenerate form of [implicit parameter passing][1], and the purpose is essentially the same, to solve what's called [The Configurations Problem][2]:

> The configurations problem is to
> propagate run-time preferences
> throughout a program, allowing
> multiple concurrent configuration sets
> to coexist safely under statically
> guaranteed separation.

Dependency Injection frameworks compensate for the lack of [implicit parameters][1], [Curried functions][3], and convenient facilities for [monads][4] in the language.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:
[4]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through