-2

I (think I) just understood the differences between Java Entity, VO, POJO, Javabeans, DAO, DTO, etc, I mean, the theory. Now, I'm trying to understand the implications of the implementations depending on the needs. Who cares if I create a POJO or a JavaBean? At the beginning I will create a POJO if I have no other constraint, until I realise I must make it a Javabean and deal with it's restrictions.

When do you tell yourself: "I need a DTO"? Only for web services or when any client/server call is made? In that case, do you put all the data you need (and the one you think you will need?) in one DTO bunch?

Lastly, in a MVC model, where does each one go? Can I make an entity or a vo in either layer? Where does the DTO go?

Thank you very much

Andy
  • 39
  • 1
  • 4
  • Please be aware of increasing chances to give you correct and precise answer – share you code, give steps to reproduce or/and test cases/data will increase your chances to get an good answer soon – Stepan Novikov Oct 24 '17 at 14:58

1 Answers1

1

You understood the difference between them, now you need to understand their purpose.

There is a neat definition for each one in this link.

Who cares if I create a POJO or a JavaBean? At the beginning I will create a POJO if I have no other constraint, until I realise I must make it a Javabean and deal with it's restrictions.

You need to think what the purpose of the class is. Is it a standalone class with no annotations that provides functionality but can be isolated (not counting libraries used)? Then it is likely a POJO.

Is it a class that serves as a bridge between layers of your project? Is it annotated? Does it implement some business logic for your project? Then it is a JavaBean.

When do you tell yourself: "I need a DTO"? Only for web services or when any client/server call is made? In that case, do you put all the data you need (and the one you think you will need?) in one DTO bunch?

DTOs are generally used to share information between layers. Their main purpose is isolating Entities so that you can change how your information is persisted from how the information flows through the different beans and controllers in your project.

To know when I need a DTO and when not I follow these rules:

  • Does the method more than three parameters? Then use a DTO.
  • Does the method return more than a single parameter? Then use a DTO.
  • When passing parameters to method calls / from method results: Does each element have more than one type (for Maps you would look at the value)? Then use a Collection of DTOs.
  • Otherwise use String, int, Long, etc.

Also never mind reusing DTOs even if most of its fields are not used for a specific method, you won't be wasting much memory if it's properly design. On the other hand, don't worry if you need to create new DTOs that share fields with some others, it might be clearer when reviewing your code. Try to find the right balance between too many DTOs and overpopulated DTOs.

Lastly, in a MVC model, where does each one go? Can I make an entity or a vo in either layer? Where does the DTO go?

It depends on how you structure your project. The term layer is a bit open as it can refer to each of the MVC elements and to the Data/Business/Client architecture (when I used the word layer in this answer, I'm talking about the later classification).

It is a good practice to separate each layer in a different project (specially in large projects). This is a common structure I use for enterprise web applications:

  • ProjectNameDao: Includes database access methods and entities.
  • ProjectNameBo: Includes all the business logic. Shares information with the other layers by using DTOs. It is, along ProjectNameDao, the Model in a MVC model.
  • ProjectNameWeb: Includes views and controllers from the MVC model.
  • ProjectNameDto: Includes all DTOs.
  • ProjectNameUtils: Shared utils, normally POJOs that hold constant values or provide basic functionality like Date formatting.

This structure needs to be adapted to whatever your requirements are, of course.

Bernat
  • 492
  • 5
  • 13
  • Thank you very much for that information! However I have a question regarding the POJOs and JavaBeans. You really don't have to decide wether you need to create one or the other. You only think about the real stuff you need in it, and the purpose as you say, right? Why ask yourself if the class you need is a POJO or a JavaBeans? Thanks a lot – Andy Oct 25 '17 at 07:36
  • That's an engineering effort you make to better organize your code, which will help other developers or your future self. Besides there's certain things you cannot place in a POJO. Most classes will be beans, leaving only utilities as POJOs. – Bernat Oct 25 '17 at 07:49
  • Thank you very much, Bernat! – Andy Oct 25 '17 at 08:22
  • Hi @Andy. If this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Bernat Oct 25 '17 at 09:14