存储库模式在android中的应用

时间:2016-03-25 16:56:01

标签: android repository-pattern

我正在使用存储库模式的android应用程序,但我在android中找不到一系列完整的存储库模式实际实现。 任何推荐的教程?

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,发现了一篇很好的文章,比较了Android中存储库的不同方法:Evolution of repository pattern

它讨论了一种众所周知的方法,Clean architecture,建议不要过度设计。

要理解这些帖子,您应该具有使用模式和存储库的一些经验。在我看来,这些帖子可以引导您正确实施。

答案 1 :(得分:1)

实现Repository模式非常简单,您只需要使用CRUD方法创建接口并在域逻辑中使用它。

例如:

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;
}

方法计数和签名在您自己的项目中可能有所不同,但方法是相同的。 之后,您可以创建封装一个或另一个数据源的存储库的具体实现 - ContentProviderRepository,OrmLiteRepository,RealmRepository等。 然后通过使用依赖注入原则,您应该注入正确的实现。

很少有关于存储库模式的好书。 模式独立于平台,因此易于实现和使用每个平台。

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

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