Frage

I have a data transfer object named SchoolyearEditRequest which has 4 properties holding domain objects.

1) Is it wrong that my DTO wraps domain entities?

2) If yes do I have to create for every domain entity a xxxDTO "wrapper" with only the really needed properties for the client side?

public class SchoolyearEditRequest
{
    // Create a SchoolclassCodeDTO ???
    // Create a SchoolyearDTO ???
    // Create a collection of TimeTableDTO`s ???

    public IEnumerable<SchoolclassCode> SchoolclassCodes { get; set; }
    public IEnumerable<TimeTable> TimeTablesWeekA { get; set; }
    public IEnumerable<TimeTable> TimeTablesWeekB { get; set; }
    public Schoolyear Schoolyear { get; set; }
}
War es hilfreich?

Lösung

Is it wrong that my DTO wraps domain entities?

Yes, it is wrong. DTOs should be empty of any logic - including the logic that may be attached to the objects of which your DTOs are composed. Including domain objects into DTO exposes domain logic, eliminating the main benefit of using DTOs in the first place.

If yes do I have to create for every domain entity a xxxDTO "wrapper" with only the really needed properties for the client side?

Correct, that is the idea. By doing so you will eliminate the coupling between the client and your domain objects.

Note: it is common to use a mapper to simplify the process of mapping between DTOs and the domain objects.

Andere Tipps

If anything, it should be the other way round - your domain entity should wrap your DTO.

The purpose of a DTO is to simply carry data in as generic a way as possible. This means no events, no business logic, etc. Nor should it contain references to domain entities - that is defeating the purpose of having a DTO.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top