Why using DTO model class for data source and another model class for domain layer?

Viewed 27

what are the advantages of using different model classes rather than just making one model for network requests and for the domain layer I did not get the idea of mapping models why do even we need that when we can use one model?

1 Answers

The reason is that such classes are usually designed for different purposes. A domain model entity is usually designed in a way that it is always valid according to the business rules.

A DTO used to transport information to the UI is usually optimized in a way that it is most convenient for the UI, e.g.: all dates are already pre-formatted.

A DTO used for persistence is usually optimized to for a particular persistence framework, e.g. having certain attributes applied.

Using different classes for those different purposes decouples the different needs. This allows you to change the presentation layer DTO and the persistence layer DTO independently from each other and from the domain model entity when either the UI or the persistence framework/technology changes.

Related