gpt4 book ai didi

c# - 我的 View 模型中应该包含什么?

转载 作者:可可西里 更新时间:2023-11-01 09:07:22 24 4
gpt4 key购买 nike

<分区>

我有名为 ProductCategory 的业务模型,如下所示,我在其中添加了验证:

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 模型依赖于业务模型是不是很糟糕?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com