title: Changing Guice bindings at runtime
body: <p>I would like to be able to change the Guice injections at runtime to support multiple injections based on user input. This is what I would like to achieve:</p>
  
  <pre><code>public interface IDao {
      public int someMethod();
  }
  
  public class DaoEarth implements IDao {
      @Override
      public int someMethod(){ ... }
  }
  
  public class DaoMars implements IDao {
      @Override
      public int someMethod(){ ... }
  }
  
  public class MyClass {
      @Inject
      private IDao myDao;
  
      public int myMethod(String domain) {
          //If Domain == Earth, myDao should be of the type DaoEarth
  
          //If Domain == DaoMars, myDao should be of the type DaoMars
      }
  }
  </code></pre>
  
  <p>I was thinking of writing my own Provider, but I don't know how to use that provider to change my bindings at runtime. Any input is welcome and appreciated :)!</p>
  
  <p><strong>Update</strong>
  Here's what I currently came up with, it's not as pretty as I'd like, so I'm still looking for feedback</p>
  
  <pre><code>public class DomainProvider {
      @Inject @Earth
      private IDaoProvider earthDaoProvider;
  
      @Inject @Mars
      private IDaoProvider marsDaoProvider;
  
      public IDaoProvider get(Domain domain){
          switch (domain){
              case EARTH:
                  return earthDaoProvider;
              case MARS:
                  return marsDaoProvider;
          }
      }
  
      public IDaoProvider get(String domain){
          Domain parsedDomain = Domain.valueOf(domain.toUpperCase());
          return get(parsedDomain);
      }
  }
  
  //MarsDaoProvider would be equivalent
  public class EarthDaoProvider implements IDaoProvider {
      @Inject @Earth
      private IDao earthDao;
  
      public IDao getDao() {
          return earthDao;
      }
  }
  
  // This means that in "MyClass", I can do:
  public class MyClass {
      @Inject
      private DomainProvider domainProvider;
  
      public int myMethod(String domain) {
          IDaoProvider daoProvider = domainProvider.get(domain);
          IDao dao = daoProvider.getDao();
  
          //Now "dao" will be of the correct type based on the domain
      }
  }
  
  //Of course elsewhere I have the bindings set like
  bind(IDao.class).annotatedWith(Earth.class).to(EarthDao.class);
  </code></pre>