Skip to content
Advertisement

Repository Pattern with Repository Factory

I’m trying to improve my Android persistence layer to be used across multiple applications.

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: https://github.com/grmaciel/android-repository-ormlite

Interface:

JavaScript

Now all my repositories extends my base repository, like this:

JavaScript

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 new Instance()

What i did was to create a Singleton factory that has to be initialized with a container that has the class relations, like this:

JavaScript

The factory:

JavaScript

And then it could be called like this:

JavaScript

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.

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.

Any advices would be appreciated.

Advertisement

Answer

What i did to improve my code was instead of reinventing the wheel i started using a dependency injection library (Dagger 2 – http://google.github.io/dagger/).

You can define modules that return your desidered repository across an application or activities depending on your needs.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement