Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in JHipster DTOs are generated in the service layer and not in the REST layer?

Tags:

jhipster

We've started a JHipster project and used DTOs. At the beginning I wasn't fan, but then, this allowed us to really customize our DTOs for our REST layer: perfect.

But now we are adding JMS to the project and we realize that our message listeners need to access our service layer... but the services returns DTOs that are suited for the REST layer, not the messaging layer.

Why does JHipster generate the DTOs in the service layer ? Why not in the REST layer ? This way, both REST and JMS layer (and something else later) could access the service layer that only deals with entities. Then, both REST and JMS will have their own DTOs suited for their own needs.

Any idea why this was done on the first place ? Thanks

like image 938
agoncal Avatar asked Oct 21 '25 23:10

agoncal


1 Answers

In JHipster, you have in fact DTOs and VMs:

  • DTOs (Data Transfer Objects) are in the service layer: they are here to get data out of the transactional layer. The idea is that inside the service layer you have JPA entities, which have relationships (lazy loaded with Hibernate), and that the service layer returns DTOs which are not managed by Hibernate (no lazy loading anymore). There might not be a 1-to-1 relationship between entities and DTOs: maybe DTOs aggregate several entities, maybe they have more fields (or less fields!), that all depends on your business model.
  • VMs (View Models) are in view layer. This wording comes from Angular, where we often have "vm" objects. The idea is to have objects which are specific to your view layer, so it's easier, more secured and more performant to code the client application. There could be several VMs for one DTOs, as the DTO's data might be used in several different screens. Typically VMs are what is being transferred in JSON from the Java back-end to the JavaScript front-end.

None of those layers are mandatory with JHipster. You will typically have:

  • Just entities for simple CRUDs
  • Entities and DTOs for business code
  • Entities and VMs when there is a specific, complex view
  • Entities, DTOS and VMs when everything is complex
like image 118
Julien Dubois Avatar answered Oct 23 '25 14:10

Julien Dubois