Android储存库模式 [英] Android Repository pattern

查看:84
本文介绍了Android储存库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于存储库模式,我有几个问题:

I have couple of questions about Repository pattern:

  1. 如果我仅使用离线数据库(例如Room with LiveData),是否有任何使用存储库模式的信息?

  1. If I'm using only offline database for example Room with LiveData is there any use of Repository pattern?

如果我的应用程序当前处于离线状态,但是将来会连接到远程数据库,那么我应该实施存储库模式吗?否则以后再处理就不会有问题了吗?

If my app is offline right now, but will be connected to remote db in the future should I implement repository pattern or it's not going to be a problem to do it later?

推荐答案

首先,存储库模式与技术或编程语言无关.

To begin with, Repository pattern have nothing to do with technology or programming language.

存储库模式对于将持久性问题与应用程序的其余部分分开很有用.这也有助于提高测试能力,因为现在,您可以模拟存储库并轻松测试其余代码,而无需连接到持久层.

Repository pattern is useful to separate persistence concerns from rest of the application. This also helps improve testing ability because now, you can mock the Repository and test rest of the code easily without connection to persistence layer.

例如,如果我仅使用离线数据库,例如Room with LiveData,是否有任何使用存储库模式的信息?

If I'm using only offline database for example Room with LiveData is there any use of Repository pattern?

我不知道那些技术.但是如上所述,存储库的目的是对持久性的无知.不管您的数据存储区是什么(在内存数据库,RDBMS,Excel/CSS,Web服务,XML,JSON或任何形式中),存储库模式都可以帮助抽象它.是的,存储库模式在这里很有帮助.

I am not aware about those technologies. But as said above, purpose of repository is ignorance of persistence. No matter what your data store is (in memory database, RDBMS, Excel/CSS, Web service, XML, JSON or whatever), repository pattern helps abstracting it. So yes, repository pattern is helpful here.

如果我的应用当前处于离线状态,但将来会连接到远程数据库,那么我应该实施存储库模式吗?否则以后再做就不会有问题了吗?

If my app is offline right now, but will be connected to remote db in the future should I implement repository pattern or it's not going to be a problem to do it later?

实际上,我强烈建议您在此处实施存储库.由于忽略了持久性,因此可以基于以下假设轻松设计应用程序的其余部分:数据将可用/持久化SOMEHOW(在脱机情况下为本地,在将来为服务器情况下),甚至不知道其处于脱机状态还是在线状态.这样,将来将来从本地存储转移到远程存储时,您的应用程序不会受到任何影响,因为它是针对存储库接口构建的,并且这些接口不会更改.现在,存储库已完全解决了持久性问题.

In fact, I will strongly recommend implementing repository here. As the persistence is ignored, your rest of the application can be easily designed based on assumption that data will be available/persisted SOMEHOW (local in case of offline and server in case of future) without even knowing it is offline or online. That way, when in future you shift from local store to remote store, your application does not affect in any way as it is build against repository interfaces and those interfaces does not change. Persistence concern is fully handled by Repository now.

我的其他答案可能会有所帮助.

My other answer may be helpful.

这篇关于Android储存库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆