简体   繁体   English

在C#中克隆DTO

[英]Cloning a DTO in C#

Currently to clone my DTOs I'm implementing IClonable, but this (and the alternatives like a clone ctor) mean that I need to add implementation logic into my (otherwise) data-only object. 目前要克隆我的DTO我正在实现IClonable,但是这(和克隆ctor之类的替代方案)意味着我需要在我的(否则)数据对象中添加实现逻辑。

Is there a better way to clone DTOs, or is including clone logic considered to be OK? 有没有更好的方法来克隆DTO,或者包括克隆逻辑被认为是好的?

Such logic is so basic that I think it's stretching it to call it implementation logic . 这样的逻辑是如此基本,以至于我认为它将其称为实现逻辑 It may simply make much more sense to clone in this way: 以这种方式克隆可能更有意义:

  1. You're able to call MemberwiseClone to make the shallow copy 您可以调用MemberwiseClone来生成浅层副本
  2. The DTO knows everything it needs to clone itself DTO知道克隆自身所需的一切
  3. You'll not have to create separate objects for this or helpers that depend on reflection 您不必为此创建单独的对象或依赖于反射的帮助程序

Not adding cloning logic into DTOs would to me be an obvious example of following architectural patterns too far. 不将克隆逻辑添加到DTO中对我来说是一个明显的遵循架构模式的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM