<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sergey Volkodav</title>
    <description>The latest articles on DEV Community by Sergey Volkodav (@volkodavs).</description>
    <link>https://dev.to/volkodavs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F360636%2Fa4a646a4-4968-402c-a48c-8d2224538a67.png</url>
      <title>DEV Community: Sergey Volkodav</title>
      <link>https://dev.to/volkodavs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/volkodavs"/>
    <language>en</language>
    <item>
      <title>Dropwizard Dependency Injection (DI) frameworks
</title>
      <dc:creator>Sergey Volkodav</dc:creator>
      <pubDate>Sat, 04 Apr 2020 13:58:02 +0000</pubDate>
      <link>https://dev.to/volkodavs/dropwizard-dependency-injection-di-frameworks-54jf</link>
      <guid>https://dev.to/volkodavs/dropwizard-dependency-injection-di-frameworks-54jf</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

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

&lt;p&gt;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).&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Helps in Unit testing &lt;/li&gt;
&lt;li&gt;Boilerplate code is reduced, as initializing of dependencies is done by the injector component&lt;/li&gt;
&lt;li&gt;Extending the application becomes easier&lt;/li&gt;
&lt;li&gt;It helps to enable loose coupling, which is important in application programming&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Disadvantages
&lt;/h2&gt;

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

&lt;h2&gt;
  
  
  Dependency injection types
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;constructor injection:&lt;/strong&gt;  the dependencies are provided through a class constructor. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;setter injection:&lt;/strong&gt; the client exposes a setter method that the injector uses to inject the dependency&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;interface injection:&lt;/strong&gt; 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&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Options
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Vanilla Java&lt;/li&gt;
&lt;li&gt;Google Guice&lt;/li&gt;
&lt;li&gt;Dagger&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Vanilla Java
&lt;/h2&gt;

&lt;p&gt;The process to manage dependencies manually requires extra work and harms code legibility. &lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages
&lt;/h3&gt;

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

&lt;h3&gt;
  
  
  Disadvantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Building object graphs by hand are labor-intensive, error-prone, and makes testing difficult.&lt;/li&gt;
&lt;li&gt;Hard to maintain &lt;/li&gt;
&lt;li&gt;Leaky abstractions&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Google Guice
&lt;/h2&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages
&lt;/h3&gt;

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

&lt;h3&gt;
  
  
  Disadvantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use java reflection for dependency lookup, but you can avoid it by providing manually instance &lt;/li&gt;
&lt;li&gt;Extra third party dependency &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Dropwizard Guice extension
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Hubspot: &lt;a href="https://github.com/HubSpot/dropwizard-guice"&gt;https://github.com/HubSpot/dropwizard-guice&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Intercom: &lt;a href="https://github.com/intercom/dropwizard-guice"&gt;https://github.com/intercom/dropwizard-guice&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Google Dagger
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages
&lt;/h3&gt;

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

&lt;h3&gt;
  
  
  Disadvantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Limited API compared to Guice&lt;/li&gt;
&lt;li&gt;Overly restrictive&lt;/li&gt;
&lt;li&gt;Dagger will not inject objects that are not annotated with @Inject. May a problem when you dealing with 3rd party libraries&lt;/li&gt;
&lt;li&gt;Young compares to other DI frameworks &lt;/li&gt;
&lt;li&gt;Extra third party dependency &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/google/guice/wiki"&gt;https://github.com/google/guice/wiki&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.journaldev.com/2403/google-guice-dependency-injection-example-tutorial"&gt;https://www.journaldev.com/2403/google-guice-dependency-injection-example-tutorial&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.programcreek.com/java-api-examples/?api=com.hubspot.dropwizard.guice.GuiceBundle"&gt;https://www.programcreek.com/java-api-examples/?api=com.hubspot.dropwizard.guice.GuiceBundle&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.baeldung.com/guice"&gt;https://www.baeldung.com/guice&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dagger.dev/users-guide"&gt;https://dagger.dev/users-guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dzone.com/articles/dagger-2-tutorial-dependency-injection-made-easy"&gt;https://dzone.com/articles/dagger-2-tutorial-dependency-injection-made-easy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>dropwizard</category>
    </item>
  </channel>
</rss>
