r/SpringBoot • • Jul 07 '25

Question DTO mapping - presentation vs service layer

A pretty basic question - where do you map your entities?
This question emerged once I actually investigated Open Session In View. If I disable it, lazy loaded collections blow up if I try to map in controller (outside the transaction), as I always did. Mapping DTOs in controllers meant keeping presentation and service layers decoupled, services handle business logic and should not be "polluted", which also facilitates multiple frontends without touching service layer.
I am aware that I can use "internal" DTOs for data transfer between layers, but it feels like excessive boilerplate, especially when the mapping is 1:1.

Thanks in advance for sharing your patterns and rationale!

25 Upvotes

51 comments sorted by

View all comments

Show parent comments

0

u/czeslaw_t Jul 08 '25

I don’t , framework build read only entities. In command model I use constructor for create new entity with command as argument. What’s wrong with that? In query entity you can have method: entityToDtoA() and entity.toDtoB()

1

u/Efficient_Stop_2838 Jul 08 '25

I don't get it, so you don't create entities but you use constructor to pass COMMAND (whatever that is) as an argument?

toDtoA, toDtoB... Ever heard of SOLID principles?

0

u/czeslaw_t Jul 08 '25

Very simple example: Add/Get user. //post

record AddUserCommand(UUID roleId, name){}

@Entity Class user{ Private uuid id; Private uuid roleId

User(AddUserCommand command){} } …. new User(AddUserCommand command); commandRepo.save(user);

//Get

record UserDto(UUID id, String name, String roleName){}

@Immutable @Entity @Table(name=user) @NoArgsConstructor class UserQuery { private UUid id; private RoleQuery role;

UserDto toDto(){ return new UserDto(id, name, role.getName()); } }

0

u/Efficient_Stop_2838 Jul 10 '25

OMG, I would really love to know which company allows you to do this, just to put it to my blacklist of partners never to cooperate with. Same entity twice (DRY?) for absolutely no reason. Weird naming. Violation of single responsibility principle. And the list goes on...

0

u/czeslaw_t Jul 11 '25

You're currently fascinated by SOLID and DRY, etc. These are useful concepts, but you still have some learning to do. CQRS might be a good starting point. Good luck!

0

u/Efficient_Stop_2838 Jul 11 '25

I'm not fascinated by them, I am very well aware of the fact they're more suggestions than laws. But you my boy, you're blatantly ignoring them in the situations you definitely should use them. Not only that, you are ignoring tools like lombok and mapstruct which is even more fascinating.

Due to all of that you are writing unreadable, unmaintainable and confusing code, which is the only law that should really be applied. You're writing code for someone else and if I am coming to take over not only I will have hard time understanding why you have the same entities twice, but first thing I am doing is that I will completely rewrite that 💩.

So yeah, I still have some learning to do, which is pretty normal in this industry, but you... you need to learn basics my friend

1

u/czeslaw_t Jul 11 '25

Bro, I use Lombok and MapStruct when it makes sense. Unfortunately for you, you lack a deeper understanding that each tool has its pros and cons. Similarly, you’re unaware of the cost of refactoring and probably don’t even know how to do it. Wanting to start with refactoring shows how ignorant you are.

→ More replies (0)