加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – POCO应该来自DTO还是更好?

发布时间:2020-12-16 01:33:02 所属栏目:百科 来源:网络整理
导读:在创建n层解决方案时,我不想公开我的业务对象,而是使用DTO而不是这个.另一方面,我不想一直双重定义对象和编写复制代码. 现在我的想法是编写包含所有必要字段和属性的DTO,但没有逻辑(只有状态). 然后我将从这些DTO派生我的业务对象,使用我的业务逻辑扩展它们,
在创建n层解决方案时,我不想公开我的业务对象,而是使用DTO而不是这个.另一方面,我不想一直双重定义对象和编写复制代码.

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

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

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

你认为这种做法是明智的吗?

问候,

塞巴斯蒂安

解决方法

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

“…,
because keeping the DTO unaware of the
domain objects enables you to reuse
the DTO in different contexts.

Likewise,you do not want the domain
objects to know about the DTO because
that may mean that changing the DTO
would require changing code in the
domain logic
,which would lead to a
maintenance 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读