我有以下ViewModel,它是继承它的其他ViewModel的基类。
将viewmdoel映射到viewmodel的方法放在自己的位置是不是一个好主意?
像这样:
public class ShowQuestionViewModel
{
public ShowQuestionViewModel()
{
}
public int Question_ID { get; set; }
public Boolean Condition_Fullfilled;
public String requiredmessage = "";
[Required(ErrorMessage = "Dette felt er påkrævet")]
public String Question_Wording { get; set; }
public String Question_Type { get; set; }
[Required(ErrorMessage = "Dette felt er påkrævet")]
//[Digits(ErrorMessage = "Feltet må kun indeholde heltal")]
public String Question_Number { get; set; }
public String Left_scale { get; set; }
public String Right_scale { get; set; }
public int Scale_Length { get; set; }
public String Left_Scale_HelpText { get; set; }
public String Right_Scale_HelpText { get; set; }
public Boolean Visible { get; set; }
public Boolean IsAnswered { get; set; }
public String Question_HelpText { get; set; }
public String Question_HelpText_PDF { get; set; }
public int Category_ID { get; set; }
// Ting der er behov for til Views
public SelectList Categories { get; set; }
public SelectList Visibility { get; set; }
public string RefUrl { get; set; }
public void MapQuestionToViewModel(MvcApplication3.Models.Question question)
{
Question_ID = question.question_id;
Category_ID = Convert.ToInt32(question.category_id);
Question_Number = question.question_number.ToString();
Question_Wording = question.question_wording;
Visible = (bool)question.visible;
String hlptxt = question.help_text;
Question_HelpText = hlptxt;
Question_HelpText_PDF = question.help_text_pdf ?? "";
Left_scale = question.left_scale;
Right_scale = question.right_scale;
Scale_Length = question.scale_length ?? 0;
Left_Scale_HelpText = question.left_scale_hlptxt;
Right_Scale_HelpText = question.right_scale_hlptxt;
Question_Type = question.Question_Type.type_description;
Categories = GetQuestions.GetCategories(question);
Visibility = GetQuestions.getVisibleSelectList();
}
}
还是应该放在其他地方?
欢迎链接到一些设计指南
答案 0 :(得分:0)
我会使用第二个构造函数
public ShowQuestionViewModel(MvcApplication3.Models.Question dbmodel)
MvcApplication3.Models命名空间应该包含视图模型而不包含任何其他内容。我建议您将所有业务逻辑转移到类库中。
您也不必处理映射。您可以使用AutoMapper或ValueInjecter等框架。