Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate DTO and value object mapping

Tags:

hibernate

Is it good practice to take hibernate entities till presentation layer? or Do we need to map all properties of entities to a value object and value object will be used for UI?

Please let me know advantages and disadvantages of both the appoaches.

When should we use what?

like image 994
Java P Avatar asked Jul 04 '12 17:07

Java P


People also ask

Is a DTO a value object?

While a DTO just holds some data for you and provides a clear schema for this data, a value object also holds some data, but offers evidence that the data matches the expectations. When the value object's class is used as a parameter, property, or return type, you know that you are dealing with a correct value.

What is DTO in hibernate?

DTO is an abbreviation that stands for Data Transfer Object. Originally, Martin Fowler defined a DTO in his famous book Patterns of Enterprise Application Architecture as: An object that carries data between processes in order to reduce the number of method calls.

What is DTO mapping?

DTO, which stands for Data Transfer Object, is a design pattern conceived to reduce the number of calls when working with remote interfaces. As Martin Fowler defines in his blog, the main reason for using a Data Transfer Object is to batch up what would be multiple remote calls into a single one.

Can DTO have objects?

DTOs are often used in conjunction with data access objects to retrieve data from a database. A value object is not a DTO.


1 Answers

what you call DTO are entities in ORMs. They are normally part of a domain model which contains business logic and contain most of the time more data than is needed to render individual views. My personal rule of thumb

Use entities in Views when there is no transfer layer between the DAL and the view and there is little business logic:

  • Advantages:
    • one model
    • no need to map between models
    • easier use of lazy loading
  • Disadvantages:
    • each change in the model means change of the views
    • many disadvatages with transfer layer see below

Map entities to DTOs when there is a transfer layer and/or the viewdata differs from entities or aggregate many different entities

  • Advantages:
    • DTOs/ views dont have to change when there are changes to the models
    • avoid sending entities over the wire which has loads of problems (lazy loading exceptions, much unneeded data sent, expose sensible information, ...)
    • Model has fewer responsibilities (serialisation) which make them easier to reuse (eg. backend processing)
  • Disadvantages:
    • more classes to write
    • code to translate entities to DTOs
like image 137
Firo Avatar answered Oct 06 '22 14:10

Firo