anchor.diff: -= Dependency Injection demistified
  += Dependency Injection demystified
   
   :hp-tags: Java, Guice, Maven
   
  
positive.code: == What does DI mean
  In a https://carlomorelli.github.io/2017/04/23/Easily-designing-the-DB-connection-from-Java.html[previous blog post] I discussed how I was moving the database backend from a H2 instance to a fully fledged Pgsql with connection pooling. However I was going to keep the H2 conf for testing, so I end up with a static Factory class that can provide the desired DataSource implementation: `Factory::getPostgresDataSource` and `Factory::getH2DataSource`.
  
  This DataSource impl is used in the app code. Let's assume for a moment that my app runs with a normal p.s.v. `main()` funcion in a certain `App` class. That is, assume we have something like this:
  ```
  public class App {
      public static void main(String... args) {
  
negative.code:     }
  }
  ```
  This is basically what *Dependency Injection* is all about: _to make code more manageable, we centralize all chosen configuration of interfaces and inject it in cascade through the object constructors._
  
  == DI and Unit Testing
  When working in structured manner (the aforementioned SOLID method), DI is very useful for unit testing. We can test separately pieces of code and mock the needed resources. In my case, I want that `Repository` uses the Postgres d.s. in the real world scenario, but the H2 d.s. in unittests. Unit testing then is relatively easy:
  