Search code examples
domain-driven-designdtoddd-repositories

DDD - Entity DTO representation on Repositories


There are some properties that makes no sense for the domain but are essential for the repository, one example being the partition key.

Is it ok to have a DTO on my repository that will extend the base implementation of the entity, adding the relevant fields?


Solution

  • That is totally ok. As long as this DTO stays in the repository. In Fowler's PoEAA:

    the Repository pattern is described as: Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.

    As long as the repository is doing his job correctly (storing and fetching domain models) the implementation doesn't matter. Just make sure that whatever implementation details of the repository doesn't get propagated in the rest of the project. If you add a DTO to your entity inside of the repository make sure you return the entity without the DTO or other irrelevant extra fields.