Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 292
LeTadas Avatar asked May 28 '18 20:05

LeTadas


People also ask

What is repository pattern in Android?

The repository pattern is a design pattern that isolates the data layer from the rest of the app. The data layer refers to the part of your app, separate from the UI, that handles the app's data and business logic, exposing consistent APIs for the rest of your app to access this data.

What are repository patterns?

The Repository pattern. Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer.

What is Mvvm Android?

MVVM stands for Model, View, ViewModel. Model: This holds the data of the application. It cannot directly talk to the View. Generally, it's recommended to expose the data to the ViewModel through Observables. View: It represents the UI of the application devoid of any Application Logic.

What is the purpose of repository pattern?

The Repository pattern is used to decouple the business logic and the data access layers in your application. The data access layer typically contains storage specific code and methods to operate on the data to and from the data storage.


2 Answers

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.

like image 87
Amit Joshi Avatar answered Oct 11 '22 03:10

Amit Joshi


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.

like image 23
Jeremy Avatar answered Oct 11 '22 02:10

Jeremy