architecture-components-samples icon indicating copy to clipboard operation
architecture-components-samples copied to clipboard

Same model for both Retrofit and Room

Open kazemihabib opened this issue 6 years ago • 9 comments

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

kazemihabib avatar Jun 05 '18 20:06 kazemihabib

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

saket avatar Jun 06 '18 06:06 saket

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

lmorda avatar Aug 31 '18 02:08 lmorda

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?

DoruAdryan avatar Nov 14 '18 09:11 DoruAdryan

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...

reda134 avatar Nov 14 '18 14:11 reda134

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.

mhswtf avatar Feb 15 '19 15:02 mhswtf

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?

husnainProgz avatar Dec 10 '19 06:12 husnainProgz

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

shahimclt avatar Jul 23 '20 11:07 shahimclt

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

danieldcoded avatar Mar 03 '21 22:03 danieldcoded

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:

  • Network Models: mapping your network JSONs to Kotlin objects
  • Database Models mapping your database data to Kotlin objects:
  • Domain Models: the ones you will use in your application logic

hope this will help someone

farzeni avatar Mar 04 '21 09:03 farzeni