网络pojoclass和数据库实体class,如何避免重复
Network pojo class and database entity class, how to avoid duplication
抱歉问题太笼统,本人经验不足...
我通过改造从 URL 获得了 JSON 数据,我还想将这些数据存储在我的 SQLite 数据库中。要创建此数据库,我将使用新的 Room ORM 映射。
我使用 Android Studio 插件 GsonFormat 提取网络 POJO 类,它生成下面的食谱、成分和步骤 类。并生成一些房间实体 类(更多内容见下文)
如你所见,两组类几乎完全相同,带来了很多重复的代码。但是我也觉得这两组类混用不太好。而且Room禁止实体对象相互引用,所以如果我想利用第一组类,会比较复杂...
我怀疑这些案件一般是如何处理的?
--- Pojo - Gson 可以理解:
public class Recipes {
private int id;
private String name;
private int servings;
private String image;
private List<Ingredients> ingredients;
private List<Steps> steps;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getServings() {
return servings;
}
public void setServings(int servings) {
this.servings = servings;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public List<Ingredients> getIngredients() {
return ingredients;
}
public void setIngredients(List<Ingredients> ingredients) {
this.ingredients = ingredients;
}
public List<Steps> getSteps() {
return steps;
}
public void setSteps(List<Steps> steps) {
this.steps = steps;
}
@Override
public String toString() {
return "Recipes{" +
"id=" + id +
", name='" + name + '\'' +
", servings=" + servings +
", image='" + image + '\'' +
", ingredients=" + ingredients +
", steps=" + steps +
'}';
}
}
class Ingredients {
private double quantity;
private String measure;
private String ingredient;
public double getQuantity() {
return quantity;
}
public void setQuantity(double quantity) {
this.quantity = quantity;
}
public String getMeasure() {
return measure;
}
public void setMeasure(String measure) {
this.measure = measure;
}
public String getIngredient() {
return ingredient;
}
public void setIngredient(String ingredient) {
this.ingredient = ingredient;
}
@Override
public String toString() {
return "Ingredients{" +
"quantity=" + quantity +
", measure='" + measure + '\'' +
", ingredient='" + ingredient + '\'' +
'}';
}
}
class Steps {
private int id;
private String shortDescription;
private String description;
private String videoURL;
private String thumbnailURL;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getVideoURL() {
return videoURL;
}
public void setVideoURL(String videoURL) {
this.videoURL = videoURL;
}
public String getThumbnailURL() {
return thumbnailURL;
}
public void setThumbnailURL(String thumbnailURL) {
this.thumbnailURL = thumbnailURL;
}
@Override
public String toString() {
return "Steps{" +
"id=" + id +
", shortDescription='" + shortDescription + '\'' +
", description='" + description + '\'' +
", videoURL='" + videoURL + '\'' +
", thumbnailURL='" + thumbnailURL + '\'' +
'}';
}
}
---房间实体类
@Entity
public class Recipe {
@PrimaryKey
private int id;
private String name;
private int servings;
private String image;
}
@Entity(foreignKeys = @ForeignKey(entity = Recipe.class,
parentColumns = "id",
childColumns = "recipeId"))
public class Ingredient {
@PrimaryKey
private int id;
private double quantity;
private String measure;
private String ingredient;
private int recipeId;
}
您可以使用一个对象 class 来同时满足 Gson 和 Room 映射。
例如
@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;
....
}
在编译过程中,Gson 和 Room 会寻找注解来映射正确的实体。
一般来说,它们只是带有特定注释的普通 POJO 对象,以帮助其他框架正确使用它。
我能想到 2 个解决方案,您必须根据系统的预期设计目标进行权衡:
1.By 最小化重复,例如使用此线程中列出的方法之一组合 类 中的 2 个,响应对象和实体对象现在紧密耦合。如果您确信响应中的所有字段都适合一直用作 Room 实体,这很好。
2.Instead 减少重复,我们允许它发生是为了有更多的可扩展性。
通常在我的项目中,我个人更喜欢第二种方法,只是因为没有直接依赖,我不必存储不必要的字段(有时我将一些字段存储为元数据而不是使用 Room),而且我不需要每次响应更改时都必须编写迁移代码。
抱歉问题太笼统,本人经验不足...
我通过改造从 URL 获得了 JSON 数据,我还想将这些数据存储在我的 SQLite 数据库中。要创建此数据库,我将使用新的 Room ORM 映射。
我使用 Android Studio 插件 GsonFormat 提取网络 POJO 类,它生成下面的食谱、成分和步骤 类。并生成一些房间实体 类(更多内容见下文)
如你所见,两组类几乎完全相同,带来了很多重复的代码。但是我也觉得这两组类混用不太好。而且Room禁止实体对象相互引用,所以如果我想利用第一组类,会比较复杂...
我怀疑这些案件一般是如何处理的?
--- Pojo - Gson 可以理解:
public class Recipes {
private int id;
private String name;
private int servings;
private String image;
private List<Ingredients> ingredients;
private List<Steps> steps;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getServings() {
return servings;
}
public void setServings(int servings) {
this.servings = servings;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public List<Ingredients> getIngredients() {
return ingredients;
}
public void setIngredients(List<Ingredients> ingredients) {
this.ingredients = ingredients;
}
public List<Steps> getSteps() {
return steps;
}
public void setSteps(List<Steps> steps) {
this.steps = steps;
}
@Override
public String toString() {
return "Recipes{" +
"id=" + id +
", name='" + name + '\'' +
", servings=" + servings +
", image='" + image + '\'' +
", ingredients=" + ingredients +
", steps=" + steps +
'}';
}
}
class Ingredients {
private double quantity;
private String measure;
private String ingredient;
public double getQuantity() {
return quantity;
}
public void setQuantity(double quantity) {
this.quantity = quantity;
}
public String getMeasure() {
return measure;
}
public void setMeasure(String measure) {
this.measure = measure;
}
public String getIngredient() {
return ingredient;
}
public void setIngredient(String ingredient) {
this.ingredient = ingredient;
}
@Override
public String toString() {
return "Ingredients{" +
"quantity=" + quantity +
", measure='" + measure + '\'' +
", ingredient='" + ingredient + '\'' +
'}';
}
}
class Steps {
private int id;
private String shortDescription;
private String description;
private String videoURL;
private String thumbnailURL;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getVideoURL() {
return videoURL;
}
public void setVideoURL(String videoURL) {
this.videoURL = videoURL;
}
public String getThumbnailURL() {
return thumbnailURL;
}
public void setThumbnailURL(String thumbnailURL) {
this.thumbnailURL = thumbnailURL;
}
@Override
public String toString() {
return "Steps{" +
"id=" + id +
", shortDescription='" + shortDescription + '\'' +
", description='" + description + '\'' +
", videoURL='" + videoURL + '\'' +
", thumbnailURL='" + thumbnailURL + '\'' +
'}';
}
}
---房间实体类
@Entity
public class Recipe {
@PrimaryKey
private int id;
private String name;
private int servings;
private String image;
}
@Entity(foreignKeys = @ForeignKey(entity = Recipe.class,
parentColumns = "id",
childColumns = "recipeId"))
public class Ingredient {
@PrimaryKey
private int id;
private double quantity;
private String measure;
private String ingredient;
private int recipeId;
}
您可以使用一个对象 class 来同时满足 Gson 和 Room 映射。
例如
@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;
....
}
在编译过程中,Gson 和 Room 会寻找注解来映射正确的实体。
一般来说,它们只是带有特定注释的普通 POJO 对象,以帮助其他框架正确使用它。
我能想到 2 个解决方案,您必须根据系统的预期设计目标进行权衡:
1.By 最小化重复,例如使用此线程中列出的方法之一组合 类 中的 2 个,响应对象和实体对象现在紧密耦合。如果您确信响应中的所有字段都适合一直用作 Room 实体,这很好。
2.Instead 减少重复,我们允许它发生是为了有更多的可扩展性。
通常在我的项目中,我个人更喜欢第二种方法,只是因为没有直接依赖,我不必存储不必要的字段(有时我将一些字段存储为元数据而不是使用 Room),而且我不需要每次响应更改时都必须编写迁移代码。