0

I'm building an android project, I have a database and I create many entity classes (which has all sorts of annotations like @id, @Nullable). Now I need to show the data in my view.

I'm wondering if it's ok to use directly entity classes in the view (e.g. adapter), or it's better to convert them first in VO object? How to organise things in a clearer way ? Do I need to create a converter for each entity ?

Thanks.

hawarden_
  • 1,904
  • 5
  • 28
  • 48
  • *I'm wondering if it's ok to use directly entity classes in the view (e.g. adapter)* you can, but you should indeed separate them. *it's better to convert them first in VO object?* yes *How to organise things in a clearer way ? Do I need to create a converter for each entity ?* yes – Tim May 29 '19 at 12:30
  • @Tim Castelijns Hi, I want to know the best practices in converting entities to model classes because I don't want boilerplate classes everywhere. In "Create a view using Room" tutorial Google directely uses entities in view but I think it's bad – hawarden_ May 29 '19 at 12:33

1 Answers1

0

Maybe the MVVM pattern is what you are looking for. Your entity classes (Models) are "wrapped" by ViewModels. The ViewModels expose the data to your UI and accept user input.

There's no need for a 1:1 mapping between Models and ViewModels. Your ViewModel (e.g. for a whole screen) can hold several different Models and interact with them.

Tidder
  • 1,126
  • 1
  • 7
  • 15