ビジネス オブジェクトをエンティティにマッピングしますが、エンティティの構造がビジネス オブジェクトと異なる場合があります。
BO はエンティティの内部構造について何も知る必要がないため、文字列としてuserCategories
ビジネス オブジェクトに保存されます。RecipeBo
これらの文字列は のリレーションにマップする必要がありRecipe
、RecipeUserCategoryRel
それに加えて、別のフィールドもこれにマップする必要がありuserId
ます。RecipeBo
RecipeUserCategoryRel
私のアプローチ(これは機能します)は、ラッパーを作成し、手作業でリレーションを手動で作成することですが、これはいじくり回すように見えます:
public class BoMapper
{
private final static ModelMapper modelMapper = new ModelMapper();
static
{
modelMapper.addMappings(new IngredientMap());
}
public static void map(Object from, Object to)
{
modelMapper.map(from, to);
if (from instanceof RecipeBo && to instanceof Recipe)
{
RecipeBo recipeBo = (RecipeBo)from;
List<String> userCategories = recipeBo.getUserCategories();
List<RecipeUserCategoryRel> recipeUserCategoryRels = new ArrayList<>();
for (String userCategory : userCategories)
{
recipeUserCategoryRels.add(new RecipeUserCategoryRel(userCategory, recipeBo.getUserId()));
}
Recipe recipe = (Recipe)to;
recipe.setRecipeUserCategoryRels(recipeUserCategoryRels);
}
}
}
私がBoMapperで行っていること、例えばコンバーターなどを使用することのより良いアプローチはありますか? 難しいのは、リストの各要素をマップし、userId
フィールドも追加することです。