android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.44k stars 8.28k forks source link

Same model for both Retrofit and Room #388

Open kazemihabib opened 6 years ago

kazemihabib commented 6 years ago

Is it a correct pattern to use a same model for both Retrofit and Room library as it's done in GithubBrowserSample sample?

saket commented 6 years ago

Jesse recommends otherwise: https://github.com/square/moshi/issues/453#issuecomment-368563445

lmorda commented 6 years ago

todo-mvp data package seems to imply that the data model for remote and local sources be the same "task" model (which is what brought me to this comment)

https://github.com/googlesamples/android-architecture/tree/todo-mvp-kotlin/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/data

thanks for the info, i'll use separate models too

DoruAdryan commented 5 years ago

Different models looks nice, but having complex models with multiple nesting levels seems to be a pain in the ass. As such, when requesting data from network, it may respond with a json mapping pagination and other stuff inside (which is needed by ViewModel to load more data). Having a Repository with local & remote dataSources looks like is kinda ruined (local responds with List of Objects, remote responds with class that contains the List of Objects). All sample apps that I've seen demonstrates using simple POJOs (which in production code is almost never realistic). Any idea on solving this architecture puzzle?

reda134 commented 5 years ago

Im facing the same dilemma in bringing room and retrofit together. It works fine for simple objects but as my model start to be complex, i was forced to use different models for local and remote. What is really a pain...

mhswtf commented 5 years ago

I have to agree with @DoruAdryan on this. The complex data reference rules of Room really seem to make it infeasible to use Room with Retrofit. I really miss some sort of support for stubbed relationships for complex data models, so the same model can be used for both if it contains object relations.

husnainProgz commented 4 years ago

Im facing the same dilemma in bringing room and retrofit together. It works fine for simple objects but as my model start to be complex, i was forced to use different models for local and remote. What is really a pain...

What is the correct way of using two different models? I need some guidance please? Can you explain a bit?

shahimclt commented 4 years ago

Leaving a tactical dot (.) here. Been scratching my head on this exact same issue for a while now.

danieldcoded commented 3 years ago

I am encountering the same issue, does everyone just have two different data models one for Retrofit and one for Room?

farzeni commented 3 years ago

Hi,

I had the same problem and I found the solution in a Google public course:

https://classroom.udacity.com/courses/ud9012/lessons/c5e4185e-3e76-47fb-962e-ba27c21d36d7/concepts/bcacb6d3-d2d9-478b-ab68-2468e0ac22f7

Actually they recommend to have three kind of models (with extension methods to convert them) to achive entity decoupling:

hope this will help someone