How to respect Android Clean Architecture

337 views Asked by At

I've just read the Guide to App Architecture. In this google demonstrates the basic guidelines for building robust android applications using the new Android Architecture Components. As shown in this year's Google I/O an app should have 4 layers:

enter image description here

Where the dependencies flow from top to bottom with higher layers only having knowledge of the components that are directly below them, eg. UI Controller only knows about ViewModel , ViewModel only know about Repository etc.

So I watched the videos, completed the corresponding codelabs, then I studied the guide and after having a coarse understanding I began to code the use-case that was discussed in this guide.

The object of the guide was to implement an app that simply fetches a User object from the backend and shows it to the user, all with respect to clean Architecture.

My question arose as soon as I started coding this use case. The whole application is dependent in this User Pojo. The Repository fetches it from the backend then it persists it in the DataSource using Room and with the help of LiveData the ViewModel and UI are notified. So **in which layer should the definition of my data model -in this use-case the User Pojo- be **. Or each layer should have a User Pojo that will be mapped from the User Pojo that comes from the lower layer?

2

There are 2 answers

0
mbnx On

I would add one "layer" to the picture: the model, or domain if you will. When it comes to room, the model is a simple set of POJO objects. The model layer does not have relations to other layers, but it can be references from all the other layers in your picture.

0
gyosida On

You can have some entity classes (POJOs) that represent the way your data is fetched in a reduced scope, the data source layer. The domain classes, that actually represents your business model, should be based on the entity classes. Ideally they should be exposed by your repository, the entry point to your data, which will need to treat the responses from the many sources you could have and transform them into models the external world understands. In such way you would not depend upon the many changes your data source could suffer along the time and your domain remains consistent.