在 C# 中克隆 DTO

发布于 2024-09-30 05:04:01 字数 121 浏览 1 评论 0原文

目前,为了克隆我的 DTO,我正在实现 IClonable,但这(以及克隆 ctor 等替代方案)意味着我需要将实现逻辑添加到我的(否则)纯数据对象中。

是否有更好的方法来克隆 DTO,或者包含克隆逻辑是否可以?

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.

Is there a better way to clone DTOs, or is including clone logic considered to be OK?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如荒岛 2024-10-07 05:04:02

这种逻辑是如此基本,以至于我认为它被延伸为实现逻辑。以这种方式克隆可能更有意义:

  1. 您可以调用 MemberwiseClone 来制作浅表副本
  2. DTO 知道克隆自身所需的一切
  3. 您不必为此创建单独的对象或依赖于它的帮助程序反思 在

我看来,不将克隆逻辑添加到 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
  2. The DTO knows everything it needs to clone itself
  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文