Should a domain object wrap/contain a DTO interface

domain-driven-designdomain-modelnetrepository

Using .NET – I have an interface IPerson. This interface is implemented by classes in multiple, separate repositories, e.g. EF6 (EfPerson), custom SQL (SqlPerson), or even custom assembly connecting to a web service (WebPerson).

Assuming rich domain model, my idea is that my lovely rich domain object 'Person' could have a private member variable _PersonDto of type IPerson, supplied via constructor. The members of Person would be the only way to access data from the _PersonDto.

Q. Is there anything actually inherently wrong with that approach? (Assume I'm not lazy loading, and that I will possibly have a service layer for cross-cutting stuff).

Please note I'm using DTO here to simply mean the anaemic objects I get back from my repositories.

Best Answer

A couple of observations.

First, classes (DTO or otherwise) should be data-persistence agnostic. In other words, they should have no awareness of their underlying data persistence mechanism. It worries me that you have three different versions of a Person that are distinguished only by the way they are stored or retrieved.

Second, DTO classes are merely containers, used primarily for movement of data from one place to another. The only reason you might set them via constructor is if:

  1. You wanted them to be immutable, or
  2. You wanted them to be valid on instantiation.

Validation in business systems is normally accomplished elsewhere (it shouldn't necessarily be tied to the Person object, although you can annotate objects with validation information), and in business systems, there's generally no expectation of immutability (most business systems are a big mass of mutable data and business logic).

So for all these reasons, I say the answer to your question is no.