MDaniyal
MDaniyal

Reputation: 1097

Entity and DTO communication between modules

I want to know the best practices. Following things are confusing: 1) Should model return an Entity or directly a DTO by using select new ExampleDto(...)? 2) How we should convert Entity to DTO or vise versa? 3) Can we use the DTO for Entity to DTO conversion that implements serializable class to convert object injson etc?

Kindly provide your expert opinion, in whatever direction you want.

Upvotes: 0

Views: 498

Answers (1)

Md Zahid Raza
Md Zahid Raza

Reputation: 971

Many may argue upon what to return in model.In my opinion returning dto is good practice. The reason being-

  1. We should try to avoid exposing entity to view.
  2. Many times view requires only subset of Entity, So it is not good to return complete bunch of data in that case.

You may consider using Dozer for conversion from DTO to Entity and vice versa. http://dozer.sourceforge.net/

Upvotes: 1

Related Questions