gpt4 book ai didi

automapper - 如何为嵌套域对象设计 DTO 类?

转载 作者:行者123 更新时间:2023-12-04 16:49:54 26 4
gpt4 key购买 nike

我刚开始使用 DTO。

我有两个域类:

  1. 类别
  2. 产品

如下

public class Category
{
// Properties
public int Id { get; set; }
public string Name { get; set; }

// Navigation properties
public virtual Category ParentCategory { get; set; }

// Foreign key
public int? ParentCategoryId { get; set; }

// Collections
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<Category> Subcategories { get; set; }
}

public class Product
{
// Properties
public int Id { get; set; }
public string Name { get; set; }

// Navigation property
public virtual Category Category { get; set; }

// Foreign key
public int CategoryId { get; set; }
}

我想使用 Automapper。

我不确定如何为上述聚合(图表)设计 DTO。

  • CategoryDTO 应该有一个 ProductDTO 类型的集合还是 Product 类型的集合?
  • ProductDTO 应该有一个 CategoryDTO 作为导航属性还是一个 Category 或只是一个 Category 的 Id?

谁能推荐 DTO 的代码?这种结构应该如何展平(如果应该的话)并映射到领域类?

非常感谢。

最佳答案

我将我的 DTO 设计为仅用于 MVC 的特定 Controller 操作的数据。通常这意味着如果我有一个 CategoryController,那么我就有一个 CategoryIndexModel、CategoryDe​​tailsModel、CategoryEditModel 等等。只在该屏幕上包含您想要的信息。我尽可能地扁平化,除非我有部分或集合,否则我不会创建子 DTO。

关于automapper - 如何为嵌套域对象设计 DTO 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22407741/

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