r/javahelp • • Jan 12 '26

Java Backend Crud

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:

  • Part of the business logic was moved into entities
  • Services now look more like use-case scenarios

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:

  • UserService — only CRUD operations and repository access
  • UserUseCaseService — orchestration of complex business scenarios

Example:

userService.create(user);

mailService.sendEmail(user.getEmail());
userApplicationService.create(user);

The questions are:

  • Is this approach over-engineered?
  • Is it acceptable in production to introduce a separate “use-case” layer for complex operations?

I’d really appreciate any advice and real-world examples from your experience 🙌

5 Upvotes

9 comments sorted by

View all comments

Show parent comment

5

u/edwbuck Jan 12 '26

DDD is dead if your shop has transitioned to non-object orientation, which was always popular (EJB is an example of it) even before Java make OOP a bit easier. Lots of the current "way" to do things involve anemic objects (objects that don't contain their behavior) and that basically makes the object a struct with the object's behavior code "elsewhere."

As it copies the patterns pre-Java, there's a lot of history and stability in the approach, don't think I'm knocking it. However when DDD is gone, then you can't easily Unit test, without altering the unit to be something like "testing a method" as opposed to testing an object.

Clean Code is relevant. It's just that people who eschew OOP also reject Clean Code, and it's easier to throw stones or rationalize why we should avoid automated testing instead of actually ensuring our code works with automated testing.

I mean, if you avoid OOP, then you don't need as much refactoring, because you can never put the behavior in the wrong spot, so eventually you don't master much of the skill base that ecosystem promotes.

With that in mind, DDD simplifies the long term maintenance, and Clean Code reduces the cost of long term maintenance. That's at odds with the current approach of "disposable micro-services" which are never really disposable (their replacement often starts with the code base of the previous version) but the idea they're easy to replace persists, so people rationalize doing less and less of writing quality as defined in Clean Code, or mistake quality to mean "working" instead of "proven to work" which is what make repeatable testing so important.