英文:
how to map processor to dto
问题
I am building a MealPlanner application which builds up a menu of meals based on user preferences, and I need some guidance to set 2 fields in DTO (field names: preferenceType, resultState).
Structure of Entity (named Meal):
class Meal {
String itemName; //Such as Pizza, Sandwich
double portion; //such as 1.5
double cost; //such as 15.5, default currency is dollars
}
Structure of DTO:
class MealDTO {
//Identical to Meal
String itemName;
double portion;
double cost;
//Additional fields in DTO
String preferenceType; //FAT FREE, DAIRY FREE, VEGETARIAN
String resultState; //OPTIMAL, FEASIBLE, INFEASIBLE
}
Class to map values between the two classes:
public class FieldsMapper {
// MealDTO -> Meal
public Meal MealDtoToMeal(MealDTO mealDto) {
Meal meal = new Meal();
meal.setItemName(mealDto.getItemName());
return meal;
}
//Meal -> MealDTO
public MealDTO MealToMealDto(Meal meal) {
MealDTO mealDto = new MealDTO();
mealDto.setItemName(meal.getItemName());
mealDto.setPortion(meal.getPortion());
mealDto.setCost(meal.getCost());
return mealDto;
}
}
This way, the values of preferenceType and cost are lost.
How do I map preferenceType, resultState to the DTO?
英文:
I am building a MealPlanner application which builds up a menu of meals based on user preferences, and I need some guidance to set 2 fields in DTO (field names: preferenceType, resultState).
Structure of Entity (named Meal):
class Meal {
String itemName; //Such as Pizza, Sandwich
double portion; //such as 1.5
double cost; //such as 15.5, default currency is dollars
}
Structure of DTO:
class MealDTO {
//Identical to Meal
String itemName;
double portion;
double cost;
//Additional fields in DTO
String preferenceType; //FAT FREE, DAIRY FREE, VEGETARIAN
String resultState; //OPTIMAL, FEASIBLE, INFEASIBLE
}
Class to map values between the two classes:
public class FieldsMapper {
// MealDTO -> Meal
public Meal MealDtoToMeal(MealDTO mealDto) {
Meal meal = new Meal();
meal.setItemName(mealDto.getItemName());
return meal;
}
//Meal -> MealDTO
public MealDTO MealToMealDto(Meal meal) {
MealDTO mealDto = new MealDTO();
mealDto.setItemName(meal.getItemName());
mealDto.setPortion(meal.getPortion());
mealDto.setCost(meal.getCost());
return mealDto;
}
}
Please click here: MealPlanner full flow
This way, the values of preferenceType and cost are lost.
How do I map preferenceType, resultState to the DTO?
(NOTE: Meal Planner flow as steps
- Map user request(contains only itemName) to MealDTO (other fields except itemName are empty).
- Map MealDTO to Meal using FieldsMapper.
- Pass Meal to third party, black box logic. This logic will return a map with the structure {<portion, 2>, <cost, 14>, <preferenceType, VEGETARIAN>, <resultState, OPTIMAL> }
- The portion and cost are mapped to Meal entity fields portion, cost.
- Map Meal to MealDTO and return the DTO object.)
答案1
得分: 0
Firstly, I think you should use enums instead of String for your resultState and preferenceType fields as they have fixed values.
What I think you should do is create another DTO which contains the MealDTO and the resultState and preferenceType fields e.g UserMealDTO. So something like:
class UserMealDTO {
MealDTO meal;
MealPreference preferenceType;
ResultState resultState;
}
// Enums
public enum MealPreference {
FAT FREE, DAIRY FREE, VEGETARIAN;
}
public enum ResultState {
OPTIMAL, FEASIBLE, INFEASIBLE;
}
英文:
Firstly, I think you should use enums instead of String for your resultState and preferenceType fields as they have fixed values.
What I think you should do is create another DTO which contains the MealDTO and the resultState and preferenceType fields e.g UserMealDTO. So something like:
class UserMealDTO {
MealDTO meal;
MealPreference preferenceType;
ResultState resultState;
}
// Enums
public enum MealPreference {
FAT FREE, DAIRY FREE, VEGETARIAN;
}
public enum ResultState {
OPTIMAL, FEASIBLE, INFEASIBLE;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论