【发布时间】:2014-03-14 14:24:33
【问题描述】:
我是使用 DTO 的新手。
我有两个域类:
- 类别
- 产品
如下
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或只是一个ID 到Category?
谁能推荐 DTO 的代码? 这个结构应该如何展平(如果应该)并映射到域类?
提前非常感谢。
【问题讨论】:
-
您创建 DTO 是有目的的。目的定义了 DTO 的内容。因此,只有在我们知道您需要 DTO 的用途时才能回答这个问题。
-
我想通过使用 DTO 作为 ViewModel 将 MVC Web 应用程序的表示层与域类分离。
-
这里的 DTO 将用于将对象传递到业务层,然后从那里传递到数据层,以便对网站管理面板中的类别和产品进行 CRUD 操作。
标签: automapper dto