Lately, I’ve been studying the fundamentals of Software Architecture for web applications, and I’m a bit unsure about how the data and presentation layers should interact.
From what I understand, the presentation layer is responsible for operations like validating requests, handling authentication, and so on. Meanwhile, the business logic and query handling should live within services or, in some cases, dedicated queries and commands.
My question is about data serialization and transmission: what’s considered a "clean" approach here?
Should a service or query return a fully prepared DTO containing all the necessary information? Or should it return the raw entity, leaving the presentation layer to handle the mapping?
My main concern is about the potential downsides of each approach:
If the service returns a DTO directly, it could be limiting if another service from a different module needs access to the entity. I assume that services should only interact with other services from different modules (not with repositories or data layers) to avoid tight coupling.
If the service returns the entity, how should the presentation layer handle mapping, especially when the response requires data from other modules? Should the response act as an "aggregate", mapping the necessary fields and fetching additional entities as needed?
Thank you!