问题
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