A trivial example from the "Dependency Injection with Guice" break the "Law of Demeter." At least as PMD mean it.
public class BillingModule extends AbstractModule {
@Override
protected void configure() {
bind(TransactionLog.class).to(DatabaseTransactionLog.class);
bind(CreditCardProcessor.class).to(PaypalCreditCardProcessor.class);
bind(BillingService.class).to(RealBillingService.class);
}
}
PMD mark the next line the warning Potential violation of Law of Demeter (method chain
calls)
bind(TransactionLog.class).to(DatabaseTransactionLog.class)
Is it fine to suppress this warning with @SuppressWarnings("PMD.LawOfDemeter")
or do I need to go with another approach ?
Clarification
With a few simple methods fluent interface can be converted to the classic one:
fasten(TransactionLog.class, DatabaseTransactionLog.class);
Possible realization of fasten
:
private <M, N extends M> void fasten(final Class<M> dependency, final Class<N> realization){
fastenTo(bind(dependency), realization);
}
private <M, N extends M> void fastenTo(final AnnotatedBindingBuilder<M> binder, final Class<N> realization){
binder.to(realization);
}
This is not just a fantasy, some libraries, that extend Guice partly use it.
For example play.libs.akka.AkkaGuiceSupport
:
https://playframework.com/documentation/2.5.5/api/java/play/libs/akka/AkkaGuiceSupport.html
It have methods
default <T extends akka.actor.Actor> void bindActor(java.lang.Class<T> actorClass,java.lang.String name)
default <T extends akka.actor.Actor> void bindActor(java.lang.Class<T> actorClass,java.lang.String name, java.util.function.Function<akka.actor.Props,akka.actor.Props> props)
It's not bindActor(actorClass).qualified(name).with(props)
but it is bindActor(actorClass, name, props)
I understand that Guice is much more complicated and could have more parameters and combinations, nevertheless, does someone create wrap libraries that replace fluent interface in Guice? Does someone write something about it? Alternatively, is anyone happy with a fluent in Guice?
Moreover, I read the articles and post about the fluent interface and the Law of Demeter. There are only a few articles and clearly not from the mainstream guys. Some of them wrote that fluent is good, and Law of Demeter must be violated here, some of them wrote that fluent interface is bad and some of them wrote that fluent does not violate the Law of Demeter. There is no strict vision by the community.
Still, this question is about Guice, not about "fluent" interface in general.