I have been reading Clean Architecture by R. C. Martin.
I'm trying to make sense of it, by developing a small project where I'm trying to apply its concepts. One core concept in the domain layer is to not use frameworks, 3rd party lib, and avoid @Annotations, simply make the classes in the domain pure POJOs.
I would like to know 2 things. Is it conceptually right to do my "entities" validations inside the domain layer and if so, using Bean Validation would be a reasonable option since it is a specification by java itself?
The job of a POJO domain (business) object is to faithfully represent the values of the content, and maintain the integrity of the information it represents. Validating any data input is a key part of that. Protecting against faulty inputs is a main job of the domain POJO.
So, yes, it makes perfect sense to use Jakarta Validation (formerly known as Bean Validation) (Wikipedia) to assist in this effort to faithfully represent the domain data correctly.
The admonition against frameworks and libraries should not be misinterpreted as simply and literally no frameworks/libraries. The goal of that advice is to not intertwine the internals of the domain POJO with the outer world of the application’s complexities. The domain POJO should be unaware of how it is being used. So you should be able to pick up the class of a domain POJO from this app’s codebase and drop it into any other app’s code base with no further programming. The domain object should be agnostic and ignorant of the app within which it is being used.
Avoiding this kind of unnecessary messy intertwining is what is meant by “clean” versus “dirty” architecture. Every part of your app should focus on its own responsibility, to do a job that no other part of the app can do, with all little interference or entanglement from other parts of your app as is practical.
The Jakarta Validation implementation library is used internally by your domain POJO, without concern for the outer app, except for the configuration necessary to load a Jakarta Validation implementation (currently only Hibernate Validator). This scenario is entirely reasonable, and does not violate Martin’s advice.
Similarly, you might use a logging framework, preferably through a façade like SLF4J, with your domain POJOs to track their activity. And you might use Java Management Extensions (JMX) to monitor their current conditions. These scenarios are also entirely reasonable, and do not violate Martin’s advice.
For example, your Customer
, Invoice
, and PurchaseOrder
classes should remain blissfully ignorant of your choice of a reactive/flow architecture, or some event bus coordinating parts of your app, or whether your app is a local desktop app built in JavaFX versus a web app built in Vaadin Flow, or whether you replace your database choice of MySQL with Postgres.