LeTadas
LeTadas

Reputation: 3556

Android Repository pattern

I have couple of questions about Repository pattern:

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

  2. 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?

Upvotes: 11

Views: 10585

Answers (2)

Amit Joshi
Amit Joshi

Reputation: 16409

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.

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

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?

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.

Upvotes: 13

Jeremy
Jeremy

Reputation: 565

IMO

  1. Yes, its useful to abstract how the data is stored so you have the flexibility to respond to changes both known and unknown.

    • Testing. Mocks
    • Alternative build such as as Android instant that will always fetch the data from network?
    • Other db libraries. For example say you want your feature to use Kotlin Native cross platform and alternative db library is required
  2. It will require refactoring. I think it depends on details of your app and how large it is.

Upvotes: 1

Related Questions