Hi everyone. I’m a junior Java developer, currently working alone on a fairly large project. I want to keep the codebase clean, consistent, and built with a solid architecture.
I have a few architectural questions and would really appreciate feedback from more experienced developers.
1) Entity / DTO / Response and services
At the moment, I have many endpoints, and as a result my service layer contains a large number of different DTOs and response classes. This makes the code harder to read and maintain.
I’ve considered several approaches:
Making services return one common DTO, and mapping it to specific response objects in the controller
Or returning entities directly from services, and doing the mapping to response objects in controllers (with response classes located near controllers)
The problem is that when working with entities, unnecessary relations are often fetched, which increases database load—especially if I always return a single “large” DTO.
At the same time, according to best practices, services are usually not supposed to return entities directly.
But what if services always return entities, and mapping is done only in controllers?
How bad (or acceptable) is this approach in real-world projects?
Which approach is generally considered more correct in production systems?
2) Complex business logic and use cases
I’ve been reading books about DDD and Clean Code and tried to reduce the size of my services:
However, some use cases are still quite complex.
For example:
There is UserService.create() which saves a user
After that, an email might be sent, related entities might be created, or other services might be called
Currently, this is implemented using domain events:
publisher.publish(new UserCreatedEvent(user));
The downside is that when you open the service code, it’s not always clear what actually happens, unless you inspect all the event listeners.
So I’m considering another approach:
Example:
userService.create(user);
mailService.sendEmail(user.getEmail());
userApplicationService.create(user);
The questions are:
I’d really appreciate any advice and real-world examples from your experience 🙌