DTOs

Discussions

EJB design: DTOs

  1. DTOs (3 messages)

    If you have a DTO/value object named CustomerDTO that includes the customer's address for example, would you consider the address a DTO as well (e.g. AddressDTO)? In other words, would the CustomerDTO class would have a method of "getAddress" that returns an instance of AddressDTO?

       Nathan

    Threaded Messages (3)

  2. DTOs[ Go to top ]

    Helo,

    from a object model points of view, it may be good to have that included in the customer object.

    however.. still.. it depends on if it is needed to have an instance of the address object embed when return the customer instance ...

  3. DTOs[ Go to top ]

    Good point. "Address" was just an example and may not actually be needed in the DTO depending on the application.

    What if it was the US state where there is a lookup table of US states consisting of a name and an abbreviation for each state. Or, a type like CustomerType, InvoiceType or ContentType depending on the application. A DTO is often likely to need to reference other objects to hold some of it's info right? I guess from your reply you are saying that you would refer to those as DTOs as well.

       Nathan
  4. DTOs[ Go to top ]

    It depends on your design for your DTO classes and your database (assuming you have one).

    If you are creating so-called "Domain DTOs" (i.e. DTOs based directly on your database schema), then you should do just that. If there is an Address (or State, or whatever) table, that should be a DTO, and anyone with a reference to that should have the equivalent DTOs referenced (e.g. customerDTO.getState().getName()).

    Note that your Domain DTOs can be based on your Domain objects you no-doubt came up with during design if you don't want to base them off of your database schema (or you have no database schema).

    You then can make specialized custom DTOs if you are queries or business processes that only care about certain parts of certain DTOs. Like if you want to display a list of customers' last names and their states, you might not want your EJBs to send back entire customer and state objects, so you could make a CustomerLastNameStateDTO (excuse the bad name, but you get the idea), that has two methods:

    String getLastName();
    String getStateName();

    Some people prefer using hashtables instead of Custom DTOs, but keep in mind that you have to be disciplined and you have to maintain the list of keynames somewhere so that both client and server have them and that they both have the same list.