Android-cleanarchitecture: Models for list view item and detail view

Created on 13 Jul 2016  路  6Comments  路  Source: android10/Android-CleanArchitecture

I'm fetching some data from 3rd party library to access their API.

For example, i created two use cases(FetchUserList and FetchUserDetail) to fetch user list and user detail.
The user list will be displayed in recycler view but only the user name part. And in user detail view, user name and company name will be displayed.

The model received from the server(API model, as in the library) as follow:

class User {
   String name;
   Int companyId;
   ...
}

class Company {
   Int id;
   String companyName;
   ...
}

What model should i create in my domain layer?

Should i create two models, one for the fetch user list use case and another one for fetch detail use case?

Seems the result model will be dictated by the view, in recycler view i don't need company name(exclude unnecessary data), but in the detail view i need to display more data.

I'm thinking something like below, but in the user list view case i do not need the Company data

class UserModel {
    String name;
    Company company;
}

What would you suggest?

Thank you.

discussion question

Most helpful comment

There's a difference between the Entities in your Domain Layer and the ViewModels in your View Layer. Both don't need to be "in sync" with each other.

So your Domain entity can be

class UserModel {
    int userid;
    String name;
    Company company;
}

And you can have 2 View Models

class UserListViewModel {
    String name;
    int id;
}
class UserDetailsViewModel {
    String name;
   int id;
   String companyName;
   String companyUrl;
   ...
}

Each Use Case can get a UserModel or a List (whatever applies), and that should then be mapped to the correct ViewModel for your usage per use case.

All 6 comments

Hi :)

If you want show only the name on the recyclerview, your model view only should be the name...

You can use a Mapper to pass the domain's layer to view's layer.

There's a difference between the Entities in your Domain Layer and the ViewModels in your View Layer. Both don't need to be "in sync" with each other.

So your Domain entity can be

class UserModel {
    int userid;
    String name;
    Company company;
}

And you can have 2 View Models

class UserListViewModel {
    String name;
    int id;
}
class UserDetailsViewModel {
    String name;
   int id;
   String companyName;
   String companyUrl;
   ...
}

Each Use Case can get a UserModel or a List (whatever applies), and that should then be mapped to the correct ViewModel for your usage per use case.

@Trikke
I see, so in the case of FetchUserList use case, i do not need to get the company data from company repository, this would make the UserModel.company nullable.

To build on this, the idea of mapping and having your view models is that you only put in there the data you need, so you can have different UserDetailsView models for different purposes coming from the same UserDomainModel

hello.
so in the above case if user had edit/submit screen, then we shall create different view models for it and covert those model into domain model?

There is a totally new approach that I have been working on, written in Kotlin:
https://fernandocejas.com/2018/05/07/architecting-android-reloaded/

Was this page helpful?
0 / 5 - 0 ratings

Related issues

misrakli picture misrakli  路  5Comments

fabius-bile picture fabius-bile  路  5Comments

connexion2000 picture connexion2000  路  3Comments

Cook1 picture Cook1  路  4Comments

asyncee picture asyncee  路  6Comments