<分区>
我有名为 Product
和 Category
的业务模型,如下所示,我在其中添加了验证:
public class Product
{
public int ProductId {get; set;}
[Required]
[StringLength(25)]
public string Name {get; set;}
public string Description {get; set;}
public int CategoryId {get; set;}
}
public class Category
{
public int CategoryId {get; set;}
public string Name {get; set;}
}
对于 View 模型,我创建了这样的东西:
public class ProductViewModel
{
public Product Product {get; set;}
public IList<Category> Categories {get; set;}
}
我的一个 friend 建议将所有验证保留在 View 模型中,并将业务模型的所有属性映射到 View 模型中,如下所示:
public class ProductViewModel
{
public int ProductId {get; set;}
[Required]
[StringLength(25)]
public string Name {get; set;}
public string Description {get; set;}
public int CategoryId {get; set;}
public IList<SelectListItem> CategoryDropdownValues {get; set;}
}
我问他这种方法相对于上面的方法有什么好处,他不是很确定。但他坚持认为,您不应直接在 View 中使用业务模型,而应仅验证 View 模型。
我的问题:
- 我应该将验证逻辑保留在 View 模型还是业务模型中?
- 让 View 模型依赖于业务模型是不是很糟糕?