将处理器映射到DTO。

huangapple go评论99阅读模式
英文:

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

  1. Map user request(contains only itemName) to MealDTO (other fields except itemName are empty).
  2. Map MealDTO to Meal using FieldsMapper.
  3. 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> }
  4. The portion and cost are mapped to Meal entity fields portion, cost.
  5. 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;
}

huangapple
  • 本文由 发表于 2020年8月11日 13:58:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63352300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定