How to organize a controller, service and Dao

I have a REST api in Java, and the application has three levels:

Controller -> public String storeInfo(JsonModel jsonModel)
Service -> public String store(InfoEntity entity)
Dao -> public String store(InfoEntity entity)

Now I realized that the "service" has nothing to do, the part sends back and sends requests. Then I realized that this is because Tao and Service have the same interface.

So I changed:

Controller -> public String storeInfo(JsonModel jsonModel)
Service -> public String store(JsonModel jsonModel)
Dao -> public String store(InfoEntity entity)

Now the "service" has more features. It should map the input to an object that will be stored in the database. He receives help from another facility for this, but it is his responsibility.

However, I now realized that the jsonModel object has more information than just the one that needs to be saved. For example, there is a username and password to access my API. This is the responsibility of the controller. Perhaps I sent a lot of information to the "service", which I thought.

So, I changed the following:

Controller -> public String storeInfo(JsonModel jsonModel)
Service -> public String store(TransferObject transferObject)
Dao -> public String store(InfoEntity entity)

But after that I realized that I now have three different objects, and that means that I have to do two different comparisons. Lots of code, and it looks redesigned.

How have I changed now to make it simple and functional?

+4
source share
1 answer

Your middle option is good, but I will have a specific model Infothat is not polluted by your username and password.

Controller -> public String storeInfo(Info info)
Service -> public String store(Info info)
Dao -> public String store(InfoEntity entity)

beans . - .

bean . .
Info bean, , JSON, , , .. .
, . , , - , .

, , Service : , @Transactional: !

0

All Articles