java - Repository Pattern with Repository Factory -
i'm trying improve android persistence layer used across multiple aplications.
what have done far setup base repository abstract class , base repository interface, complete code can check here: https://github.com/grmaciel/android-repository-ormlite
interface:
public interface irepository<t, id> { public void save(t entity) throws sqlexception; public void savebatch(list<t> entities) throws exception; public list<t> queryall() throws sqlexception; public t findbyid(id id) throws sqlexception; public void delete(t entity) throws sqlexception; }
now repositories extends base repository, this:
public class dependencyrepository extends baserepository<dependency> implements idependenceyrepository { public dependencyrepository(context context) { super(context); } }
what i'm trying achieve create repository factory allow people not have instantiate repositories on place new instance()
what did create singleton factory has initialized container has class relations, this:
public abstract class baserepositorycontainer { private final context context; public baserepositorycontainer(context context) { this.context = context; } public abstract <t extends irepository> map<class<t>, class<t>> getrepositoriesmap(); public context getcontext() { return context; } }
the factory:
public class repositoryfactory { private map<object, object> repositories = new hashmap<>(); 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 <t extends irepository> void configurerepositories(map<class<t>, class<t>> repositoriesmap) { (entry<class<t>, class<t>> entry : repositoriesmap.entryset()) { this.registerrepository(entry.getkey(), entry.getvalue()); } } private <t extends irepository> void registerrepository(class<t> repinterface, class<t> realrepository) { repositories.put(repinterface, this.createrepository(realrepository)); } public <t extends irepository> t getrepository(class<t> repinterface) { if (container == null) { throw new unsupportedoperationexception("you should call init method providing container."); } return (t) repositories.get(repinterface); } private <t extends irepository> t createrepository(class<t> repoclass) { try { t instance = repoclass.getconstructor(context.class).newinstance(context); log.d(log_tag, "repository " + repoclass.getsimplename() + " created"); 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; } }
and called this:
// when application first run repositoryfactory.getinstance().init(new repositorycontainer(this)); // retreaving repository idependenceyrepository repository = repositoryfactory.getinstance() .getrepository(idependenceyrepository.class);
so wondering if approach implement towards abstraction? don't idea of have calling init method of factory without obligating people it, way know if dont call throw exception dont like.
does can point me right direction? way improve design? dont want discover later on project have created lots of strong dependencies , have hard time change something.
any advices appreciated.
what did improve code instead of reinventing wheel started using dependency injection library (dagger 2 - http://google.github.io/dagger/).
you can define modules return desidered repository across application or activities depending on needs.
Comments
Post a Comment