title: Repository Pattern with Repository Factory
body: <p>I'm trying to improve my Android persistence layer to be used across multiple applications.</p>
  <p>What i have done so far is to setup a base repository abstract class and also a base repository interface, the complete code can be check here: <a href="https://github.com/grmaciel/android-repository-ormlite" rel="nofollow noreferrer">https://github.com/grmaciel/android-repository-ormlite</a></p>
  <p>Interface:</p>
  <pre><code>public interface IRepository&lt;T, Id&gt; {
      public void save(T entity) throws SQLException;
      public void saveBatch(List&lt;T&gt; entities) throws Exception;
      public List&lt;T&gt; queryAll() throws SQLException;
      public T findById(Id id) throws SQLException;
      public void delete(T entity) throws SQLException;
  }
  </code></pre>
  <p>Now all my repositories extends my base repository, like this:</p>
  <pre><code>public class DependencyRepository extends BaseRepository&lt;Dependency&gt; 
                                    implements IDependenceyRepository {
      public DependencyRepository(Context context) {
          super(context);
      }
  }
  </code></pre>
  <p>What i'm trying to achieve now is to create a repository factory that would allow people to not have to instantiate their repositories all over the place with the <code>new Instance()</code></p>
  <p>What i did was to create a Singleton factory that has to be initialized with a container that has the class relations, like this:</p>
  <pre><code>public abstract class BaseRepositoryContainer {
      private final Context context;
  
      public BaseRepositoryContainer(Context context) {
          this.context = context;
      }
  
      public abstract &lt;T extends IRepository&gt; Map&lt;Class&lt;T&gt;, Class&lt;T&gt;&gt; getRepositoriesMap();
  
      public Context getContext() {
          return context;
      }
  }
  </code></pre>
  <p>The factory:</p>
  <pre><code>public class RepositoryFactory {
      private Map&lt;Object, Object&gt; repositories = new HashMap&lt;&gt;();
      private final String LOG_TAG = RepositoryFactory.class.getSimpleName();
      private Context context;
      private static RepositoryFactory instance;
      private BaseRepositoryContainer container;
  
      private RepositoryFactory() {}
  
      public void init(BaseRepositoryContainer container) {
          this.container = container;
          this.context = container.getContext();
          this.configureRepositories(container.getRepositoriesMap());
      }
  
      private &lt;T extends IRepository&gt; void configureRepositories(Map&lt;Class&lt;T&gt;, Class&lt;T&gt;&gt; repositoriesMap) {
          for (Entry&lt;Class&lt;T&gt;, Class&lt;T&gt;&gt; entry : repositoriesMap.entrySet()) {
              this.registerRepository(entry.getKey(), entry.getValue());
          }
  
      }
  
      private &lt;T extends IRepository&gt; void registerRepository(Class&lt;T&gt; repInterface, Class&lt;T&gt; realRepository) {
          repositories.put(repInterface, this.createRepository(realRepository));
      }
  
      public &lt;T extends IRepository&gt; T getRepository(Class&lt;T&gt; repInterface) {
          if (container == null) {
              throw new UnsupportedOperationException(&quot;You should call init method providing a container.&quot;);
          }
  
          return (T) repositories.get(repInterface);
      }
  
      private &lt;T extends IRepository&gt; T createRepository(Class&lt;T&gt; repoClass) {
          try {
              T instance = repoClass.getConstructor(Context.class).newInstance(context);
              Log.d(LOG_TAG, &quot;Repository &quot; + repoClass.getSimpleName() + &quot; created&quot;);
              return instance;
          } catch (InstantiationException e) {
              Log.d(LOG_TAG, e.toString());
          } catch (IllegalAccessException e) {
              Log.d(LOG_TAG, e.toString());
          } catch (InvocationTargetException e) {
              Log.d(LOG_TAG, e.toString());
          } catch (NoSuchMethodException e) {
              Log.d(LOG_TAG, e.toString());
          }
  
          return null;
      }
  
      public static RepositoryFactory getInstance() {
          if (instance == null) {
              instance = new RepositoryFactory();
          }
  
          return instance;
      }
  }
  </code></pre>
  <p>And then it could be called like this:</p>
  <pre><code>// when the application is first run
  RepositoryFactory.getInstance().init(new RepositoryContainer(this));
  // retrieving the repository
  IDependenceyRepository repository = RepositoryFactory.getInstance()
                  .getRepository(IDependenceyRepository.class);
  </code></pre>
  <p>So i was wondering if is this a good approach to help to implement towards an abstraction? I don't like much the idea of to have the calling the init method of the factory without obligating people to it, the only way to know is if you dont call will throw an exception which i dont like.</p>
  <p>Does anyone can point me the right direction? A way to improve this design? I dont want to discover later on my project that i have created lots of strong dependencies and have a hard time to change something.</p>
  <p>Any advices would be appreciated.</p>