Dropwizard Dependency Injection (DI) frameworks

Tags: dropwizard
Published: 2020-04-04 13:58:02

# Introduction

Dropwizard does not provide dependency injection (DI) out of the box, which makes the developer free to choose the DI framework he likes. 

Dependency injection is a technique whereby one object supplies the dependencies of another object. A dependency is an object that can be used (a service).

## Benefits
* Helps in Unit testing 
* Boilerplate code is reduced, as initializing of dependencies is done by the injector component
* Extending the application becomes easier
* It helps to enable loose coupling, which is important in application programming

## Disadvantages
* It’s a bit complex to learn, and if overused can lead to management issues and other problems
* Сompile-time errors are pushed to run-time
* Most of Dependency injection frameworks implemented with reflection
* This can hinder the use of IDE automation, such as “find references”, “show call hierarchy” and safe refactoring



## Dependency injection types
* **constructor injection:**  the dependencies are provided through a class constructor. 
* **setter injection:** the client exposes a setter method that the injector uses to inject the dependency
* **interface injection:** the dependency provides an injector method that will inject the dependency into any client passed to it. Clients must implement an interface that exposes a setter method that accepts the dependency


# Options
* Vanilla Java
* Google Guice
* Dagger


## Vanilla Java
The process to manage dependencies manually requires extra work and harms code legibility. 

### Advantages 

* No third-party dependency
* Application start time is not affected
* Type-safe idiomatic configuration
* There is more traceability to the code when not using a DI framework since we know exactly where the instance is coming from.

### Disadvantages

* Building object graphs by hand are labor-intensive, error-prone, and makes testing difficult.
* Hard to maintain 
* Leaky abstractions



## Google Guice
Guice is an open-source software framework for the Java platform released by Google under the Apache License. It provides support for dependency injection using annotations to configure Java objects. 

Google Guice is fairly simple to use and will help with the maintainability of the code. It’s also possible to scan a whole package of classes to inject them.

### Advantages 
* Support JSR-330 annotations
* Mature functionality 
* Auto-scanning functionality 
* Lightweight
* Support multiple scopes
* Type-safe idiomatic configuration
* A lot of guidance and best practices
* Very clean implementation of constructor Injection
* It encourages the use of interfaces and well-defined API’s
* Large community ~9.1K starts on Github, and >10K posts on Stackoverflow  

### Disadvantages
* Use java reflection for dependency lookup, but you can avoid it by providing manually instance 
* Extra third party dependency 

### Dropwizard Guice extension 
* Hubspot: https://github.com/HubSpot/dropwizard-guice
* Intercom: https://github.com/intercom/dropwizard-guice
 

## Google Dagger
Dagger is a fully static, compile-time dependency injection framework for both Java and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.

Dagger requires more work with code due to be a compile-time dependency injection framework but also gains in boot time since there is no need to use reflection. 

### Advantages 
* Support JSR-330 annotations 
* Supposedly faster than Guice, because Dagger works by generating code up-front, whereas Guice uses Reflection at runtime.
* Optional maven compiler plugin can flag various errors at compile-time, which can be extremely useful.
* Detect cyclic dependenciesDetect unused and duplicate bindings. 
* Detect incomplete modules (that don’t provide all bindings required to construct the object graph)
* Simpler API compared to Guice

### Disadvantages
* Limited API compared to Guice
* Overly restrictive
* Dagger will not inject objects that are not annotated with @Inject. May a problem when you dealing with 3rd party libraries
* Young compares to other DI frameworks 
* Extra third party dependency 


Most dependency injectors rely on reflection to create and inject dependencies. Reflection is awesome, but is very time-consuming on low-end devices, and especially on old android versions. Dagger, however, uses a pre-compiler that creates all the classes it needs to work. That way, no reflection is needed. Dagger is less powerful than other dependency injectors, but it’s the most efficient.


# Summary 
Consider all three options I believe that Google Guice is the preferable option as a DI framework for Dropwizard because it will allow the developer to focus on functional requirements. Google Guice supports JSR-330 standardizes annotations that make the replacement of DI Framework easier if we decide to do that in the future. 


# References

* https://github.com/google/guice/wiki
* https://www.journaldev.com/2403/google-guice-dependency-injection-example-tutorial
* https://www.programcreek.com/java-api-examples/?api=com.hubspot.dropwizard.guice.GuiceBundle
* https://www.baeldung.com/guice
* https://dagger.dev/users-guide
* https://dzone.com/articles/dagger-2-tutorial-dependency-injection-made-easy