Search code examples
c#clonedto

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.

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


Solution

  • 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.