gpt4 book ai didi

c# - POCO 应该从 DTO 派生还是更好?

转载 作者:行者123 更新时间:2023-11-30 14:20:09 28 4
gpt4 key购买 nike

在创建 n 层解决方案时,我不想公开我的业务对象,而是使用 DTO 而不是这个。另一方面,我不想一直重复定义对象和编写复制代码。

现在我的想法是编写包含所有必要字段和属性但没有逻辑(只有状态)的 DTO。

然后我会从这些 DTO 派生我的业务对象,使用我的业务逻辑扩展它们,处理 DTO 基类属性。这些对象也将是在使用的 ORM (NHibernate) 中持久化的对象。

通过这种方法,我可以在服务器端处理业务对象并将它们直接传递给客户端(它们是派生的,因此可向下转换)。我不会被迫以这种方式公开我的业务逻辑并节省大量代码。

您认为这种做法明智吗?

问候,

塞巴斯蒂安

最佳答案

您可能需要考虑以下事项:

"...,because keeping the DTO unaware of thedomain objects enables you to reusethe DTO in different contexts.Likewise, you do not want the domainobjects to know about the DTO becausethat may mean that changing the DTOwould require changing code in thedomain logic, which would lead to amaintenance nightmare.

The best solution is to use the Assembler pattern, which creates DTOs from business objects and vice versa. Assembler is a specialized instance of the Mapper pattern also mentioned in Patterns of Enterprise Application Architecture...."

来自 Pattern and Practice: Data Transfer Object

另外,我自己没有用过,不过你可以看看AutoMapper

关于c# - POCO 应该从 DTO 派生还是更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1668120/

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