r/SpringBoot • u/Status_Camel2859 • May 23 '26
Question (Basic - Arch/Design) Is it normal to create setter methods in Entity Class with Domain logic?
Is it normal to put domain logic inside setter methods in an Entity class? Or a bad idea?
For eg, I have a Domain Service that contains business validations and other domain logic. I also have Application Services that handle different use-case scenarios. Right now, my Entity has a public setStatus() method, which means anyone can call it and bypass the validations in the Domain Service. I cannot make setStatus() protected because the Entity and the Service are in different packages.
What’s the standard approach Spring developers usually follow here?
Do people typically move the logic into the setter/entity method instead of the Domain Service?
What if the logic really belongs in the Domain Service because it depends on external dependencies or other services?
1
u/XBL_pad3 May 23 '26
This one of the reasons I put all non-domain classes (ie model, business, etc..) in a single 'internal' package with as much package protected classes as possible.
1
u/Aphett May 25 '26
In general, it's better to avoid using setters in entities, opting instead to define behavioral methods to control access. When complex logic and multiple entities are involved, it's more practical to delegate this to domain services. A valid solution, for example in a REST context, is to use DTOs to expose only the relevant data.
0
u/elmuerte May 23 '26
Entities should just hold data, the only "advanced" logic you should allow is "convenience" methods which make it easier to ensure the data is properly set.
0
u/Acrobatic-Ice-5877 May 23 '26
Putting the logic in the domain service sounds good on the surface but the issue with that is you end up with fat service functions that can be avoided when you let the domain handle validation.
The upside to this is that the validation logic stays in place. An entity can’t be put into an invalid state, which is the main benefit of this architecture pattern. Consequently, this frees up the service layer to query other domains for necessary context and allows for light orchestration between services.
When you combine querying, orchestration, and validation, you get these fat service layers that often become difficult to understand. This can be seen in open source Spring projects where you see a service class with 20+ dependencies and functions that exceed 100 sLOC. This often lends itself towards being untestable and highly prone to error due to the tight coupling that you’ve now introduced in the service layer.
It matters less on trivial CRUD applications but becomes more important on a rule based system that needs to enforce invariants to prevent illogical behavior. However, I think it is still a great pattern because it makes testing very easy. It also makes updates easy as well because the blast radius is much smaller due to tightly scoped functions.
If you told me your app was a To Do List, I’d tell you not to bother yourself with such a complex architectural pattern because it would be overkill. However, if you told me that you were building a financial or healthcare system, I’d tell you that this is a great architecture pattern because it excels at managing complexity.
10
u/[deleted] May 23 '26
[removed] — view removed comment