Dependency injection seems to be a good thing. In general, should dependencies be injected at the methods that require them, or should they be injected in the contructor of the class?
See the samples below to demonstrate the two ways to inject the same dependency.
//Inject the dependency into the methods that require ImportantClass Class Something { public Something() { //empty } public void A() { //do something without x } public void B(ImportantClass x) { //do something with x } public void C(ImportantClass x) { //do something with x } } //Inject the dependency into the constructor once Class Something { private ImportantClass _x public Something(ImportantClass x) { this._x = x; } public void A() { //do something without x } public void B() { //do something with this._x } public void C() { //do something with this._x } }
The major benefit of constructor injection is that it allows your fields to be marked final. For example:
The following page has a great list of the pro’s and con’s: Guice Best Practices:
Method injection
Constructor injection