My colleague and me have a dispute going on for some weeks now. And I wanted to hear the opinion of the community on this.
We use guice in our application. In the UI part of this application I prefer to instantiate all fields and components via DI and custom annotations (aka as place in for a factory) and wire them up in a buildUI method.
That looks as follows:
My Approach:
@Inject
@PreConfigured(caption="foo", width="100%")
Label field;
private void buildUI() {
this.addComponent(field);
}
I wrote also some annotations for data binding and i18n.
My colleague does it this way:
His Approach:
private Label field;
private void buildUI() {
field = new Label();
field.setCaption("foo");
field.setWidth("100%");
this.addComponent(field);
}
The code here can be very long. Considering data binding and other topics, the second code can become even longer, whereas my approach is just another annotation.
My reasons are:
His reasons are:
But I read also some articles from Fowler, where he states, that setter injection (substitute with field injection) is a bad design decision. But what about views, which don't have any reuse in other projects?
Is Field Injection a bad design? Or is the other way presented above more elegant?
Kind regards Christian
PS: I know the discussion started merely opinion based, but it is a long ongoing discussion (setter vs constructor injection), which is probably worth to let it go.
Disclaimer: since this is kind of a subjective question, I'll also make some subjective claims in this answer, based on personal preference. Here are my two cents:
Both approaches look bad to me.
Yours is bad because:
You have all bindings in a module. Think how large can this module grow, specially if there are lots of screens. Think about the mantainability implications of this: Now a programmer wanting to change a single screen will have to
1- Know the DI framework
2- Find the module where the bindings for this screen are defined.
3- Scroll down to find the binding.
4- Make the change in the module, maybe also in the screen.
5- Commit changes for the module class, maybe also for the screen class.
Using DI means using reflection, which is going to be slower than not using it.
Your colleage's approach is of course flawed, since it contains hard dependencies. This might be acceptable for screens, though. I think DI is ok for big things (like injecting a service with a parser implementation, injecting controllers, daos, loggers), but I won't use it for views, and I'd rather stick to constructor injection. So (in Android) I'd just define the views in xml and code the view-controller part in the Activity class, which is the way you are supposed to do it.
I think that your question is not really suited for StackOverflow (check FAQ) but I can present you my ideas about it.
I support your idea in this problem because it seems that you are already using DI in this project so why would you not use the functionality it provides?
My remarks to your reasons:
@PreConfigured(caption="foo", width="100%")
His points:
4.
private
fields with reflection and it does not involve constructors with 8-10 parameters either. If you want immutable objects you can use the Builder
pattern and if you want singletons you can rest assured that Spring's (I think Guice has the same concept) singleton scope is fine for you. And by the way this is common sense: if it were the only valid way there would be no other way, right?If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With