Java – Dao and services?

I always have a problem. I can't really think of a service object that encapsulates many Dao methods

I mean, for my servlet, sometimes a single Dao method is enough, such as addUser (user params)

Better approach – encapsulate Dao methods with service objects and use only service objects, even if it actually means that a single service method calls a single method or mixes their use together (some methods from the service object and some method contexts from the Dao in the servlet) – does this mean that I have automatically wired Dao and service objects inside the controller?

If I start using DAO and service objects in the same place, will it mix logic?

Solution

I think it depends on the situation If no Dao will result in mixing your business logic and data access logic, it is best to separate one class

However, if your Dao is "virtual" and only one entitymanager method is called, you can use it directly in the service object The idea is to make the course have single responsibilities and easy to expand and test You shouldn't create layers for it

If I want to keep the reusable service layer, I may not use DAO directly from the controller If Dao doesn't make sense, I'd rather use entitymanager (or any persistence strategy) in the service layer

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>