Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persistence entities as data transfer objects

I have some persistence capable Java objects with all the @annotations in my web application. All these objects reside in a data layer. Is it a best practice to use these persistence objects as data transfer objects?

For example, if I want to pass back the data fetched from data store, should I directly return those persistence objects or should I manually copy data to a intermediary DTO and pass it back to other layers? Which approach would you suggest?

like image 972
Veera Avatar asked Nov 05 '22 08:11

Veera


1 Answers

I would say that it is OK to do so (in fact the major advantage for these ORMs were to use these domain objects in different layers w/o having unnecessary DTOs) if you follow the following guidelines:

  1. You don't extend the session boundaries i.e., any changes related to database should always be done using the Data Access Layer you have defined and not through these passed objects in other layers.
  2. Any data that you need in other layer (layers above the Data Access Layer like Business Logic Layer and Presentation Layer) is pre-populated in these objects otherwise you will get exceptions according to the ORM behavior.
  3. Don't extend the session boundaries for resolving the issue mentioned in No. 2
like image 118
Faisal Feroz Avatar answered Nov 09 '22 02:11

Faisal Feroz