8

I have two sets of classes in my spring application - DTOs and Entities.

After reading Clean Code by Uncle Bob, I have got hooked on to naming things right more than ever before.

I sat down refactoring one of my Spring projects and I am not sure if adding DTO suffix for DTO classes is the right thing to do. If not, then how do you differentiate between DTO and Entity classes. I do use Service and Repository suffixes for my service classes and repository interfaces.

Merely keeping them under different packages with same names is not helpful esp. when they are to be used in same scope.

Note: Not sure if this is a precise question to be asked on Stackoverflow.

comiventor
  • 3,922
  • 5
  • 50
  • 77
  • 1
    voting to close... this is opinion-based... – Eugene Dec 21 '17 at 10:02
  • @Eugene This is the first time I tried to close my question and it just registered it as a vote. – comiventor Dec 26 '17 at 16:36
  • 2
    FWIW - I have also been struggling with how to name these objects. I decided to suffix the DB Entities with `Entity` and leave the DTO's / ViewModels as just the name, e.g. `BillyEntity` and `Billy`. I found that provides the best encapsulation, keeping the 'public' models clean. – Robert Di Paolo Apr 29 '18 at 09:47
  • Please see https://stackoverflow.com/questions/1724774/java-data-transfer-object-naming-convention/35341664 for some interesting discussions related to this topic. – Meursault Aug 02 '21 at 03:42

1 Answers1

6

If you read Core J2EE Patterns, 2nd Edition, it is called Transfer Object with all the sample codes having the TO suffix. You can also have a look at Oracle's Core J2EE Patterns site.

To sum up: You should use either DTO or TO as a suffix to any transfer object you use in your business tier.

Luay Abdulraheem
  • 751
  • 3
  • 11
  • this answer is the worst advice you can follow, read [Java Naming Convention Tautologies](http://www.vertigrated.com/blog/2011/02/interface-and-class-naming-anti-patterns-java-naming-convention-tautologies/) to understand why! –  Apr 26 '18 at 18:43
  • 23
    I have provided a link to Oracle's official Core J2EE patterns page, and you provided a link to your own blog. – Luay Abdulraheem Apr 26 '18 at 20:47