Network pojo class and database entity class, how to avoid duplication
You can use one Object class which satisfies both Gson and Room mapping.
For example.
@Entity
public class Recipe {
@PrimaryKey // Room annotation
@SerializedName("id") // Gson annotation
private int id;
@ColumnInfo(name = "name") // Room annotation
@SerializedName("name") // Gson annotation
private String name;
....
}
In compilation process, Gson and Room will look for annotations to map with correct entities.
In general, they are just normal POJO objects with specific annotations to help other frameworks to use it correctly.
There are 2 solutions I can think of, and you have to consider the tradeoff based on the objectives of your system's intended design :
1.By minimizing duplication, such as using one of the approach listed in this thread in combining 2 of the classes, the response object and entity object are now tightly coupled. This is fine if you're confident all of the fields in the response is appropriate to be consumed as Room entity all the time.
2.Instead of reducing duplication, we allow it to happen for the favor of having more extendability.
Normally in my projects, I personally prefer the second approach, simply because without direct dependency, I don't have to store unnecessary fields (sometimes I store some field as metadata instead of using Room), and plus I don't have to write migrations code everytime the response change.