sacredfaith
sacredfaith

Reputation: 870

Confused by example on the Android Developer Website

While working on my android app, I found this example on the android developer site. Specifically, I don't understand the interface UserDataSource ( in /observability) and its corresponding localUserDataSource implementation class (in /observability/persistence). This doesn't look like a repository to me, but it contains an instance to a DAO like a repository would.

  1. Does this approach/pattern have a name?
  2. Are there advantages and disadvantages to doing this over a repository? (It seems like its a lot of redundant code, but I don't know what I'm doing yet)
  3. Should I have a repository in addition to this?

I think I'm being confused by the different ways of doing things, and seeing a different approach to what I was expecting made me wonder what I really needed (or what is correct, more extensible, etc.). Thank you so much for any light you can shine on this for me.

Upvotes: 0

Views: 49

Answers (1)

Gustavo Pagani
Gustavo Pagani

Reputation: 6988

  1. Does this approach/pattern have a name?

The project that you linked is structured using the recommended app architecture described in the official Android docs:

enter image description here

  1. Are there advantages and disadvantages to doing this over a repository? (It seems like its a lot of redundant code, but I don't know what I'm doing yet)
  2. Should I have a repository in addition to this?

In order to clarify, UserDataSource is not a repository, it is a data source. In that project, UserDataSource is used by a repository: UserRepository. As per official docs:

You can consider repositories to be mediators between different data sources.

Therefore the recommended way is to have both a repository and data sources. The advantage is also described in the official docs:

Even though the repository module looks unnecessary, it serves an important purpose: it abstracts the data sources from the rest of the app. Now, our UserProfileViewModel doesn't know how the data is fetched, so we can provide the view model with data obtained from several different data-fetching implementations.

Upvotes: 1

Related Questions