views:

232

answers:

4

Given this scenario where you have "transfer objects" (POJO's with just getters/setters) which are passed by a client library to your API, what is the best way to name the transfer objects?

package com.x.core; 

public class Car {
        private String make;
        private String model;

        public Car(com.x.clientapi.Car car) {
             this.make = car.getMake();
             this.model = car.getModel();
        }
}

In this example your main class and your transfer object both have the name Car. They are in different packages but I think it's confusing to have the same name. Is there a best practice on how to name the transfer objects?

+6  A: 

I generally add 'DTO' to the end of the Class name as well as place all the DTO's in their own package. In your example I would call it com.x.core.dto.CarDTO.

oneBelizean
It isn't particularly pretty but it does mean that you can import both the dto and the "main" class. Not having the suffix makes copy code really ugly with all the extra package names.
Michael Rutherfurd
+1  A: 

I dont think there is a best practice or convention for a class exhibiting this kind of behavior. I personally dont like the word Object in any of the class names. You could either use some qualification like Poko.Car or use some naming convention like Car (for POJO) CarDa (for data access) CarBiz ( for business domain class)

Or if you dont mind the word object in a class name go for something like CarDto (Car Data Transfer Object)

Perpetualcoder
A: 

Use a convention that is suitable among the other code conventions you are using. I personally use the suffix "TO" (e.g. the data transfer object associated to the Customer domain class is named CustomerTO). Also the package structure should convey the intent of each type of class (so.foo.domain.Customer and so.foo.transport.CustomerTO)

JuanZe
+2  A: 

Adding DTO or DAO or anything else violates DRY. The FQN is perfectly fine, especially if they're really the same thing.

Jim Barrows
I don't disagree.. except I find the code confusing to read with the same name. You can specify the FQN where the client API is used but that is also somewhat cumbersome.
Marcus
I find typing DTO/DAO and any other suffix to be FAR FAR more cumbersome, even with autocomplete.
Jim Barrows