Application of repository pattern in android [closed]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 12:35:52

问题


i m working on an android application with repository pattern but i I could not find a complete series of practical implementations of repository pattern in android. any recommended tutorial?


回答1:


I run into the same question and found a good article that compares different approaches for Repository in Android: Evolution of repository pattern

It talks about one well-known approach, Clean architecture, suggesting not to overengineer things.

To understand these posts, you should have some experience in using patterns and repository in particular. In my opinion, these posts can lead you to proper implementation.




回答2:


Implement Repository pattern is pretty easy, you just need to create interface with CRUD methods and use it in your domain logic.

For example:

class CreateEntityException;
class ReadEntityException;
class UpdateEntityException;
class DeleteEntityException;

interface Repository<Entity> {
    Entity create(Entity entity) throws CreateEntityException;
    Entity read(long entityId) throws ReadEntityException;
    Entity update(Entity entity) throws UpdateEntityException;
    void delete(long entityId) throws DeleteEntityException;
}

Methods count and signature can be different in your own project but an approach is the same. After it you can create concrete implementation of repository that encapsulate one or another datasource - ContentProviderRepository, OrmLiteRepository, RealmRepository etc. Then by using Dependency Injection principle you should inject correct implementation.

There are few good books that covered Repository patterns. Pattern is independent from platform so it is easy to implement and use every platform.

https://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215

https://www.manning.com/books/functional-and-reactive-domain-modeling



来源:https://stackoverflow.com/questions/36224343/application-of-repository-pattern-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!