Relationship between pojo, Dao, and service layers when using the SSH framework

Source: Internet
Author: User

1. Corresponding to Dao and service

Generally, Hibernate Dao only operates on one pojo object, so a DaO corresponds to one pojo object. The service layer manages transactions (declarative transactions) to process multiple pojo objects (data operations on multiple tables ). The service layer (its interface implementation class) is injected with multiple Dao objects to complete data operations.

2. Whether or not the service exists

My opinion on this is not necessarily correct. My mind now has two models for building the business layer:

Mode 1 is service + Dao, that is, Dao only performs crud and similar simple operations (called function points, excluding business logic ), service is combined into business logic by calling one or more Dao function points. the number of services should be determined by the function module.

In this model, the business logic is placed in the service, and the transaction boundary should also be controlled in the service. Of course, directly controlling the transaction in the service will introduce non-business logicCodeFortunately, spring's AOP can solve this problem, which is also one of the reasons for introducing Spring.

If the disadvantage is that the operations on some objects are simple crud, and the service layer is cumbersome. mode 2 is service + Bo, while BO = Dao + business method adds business methods based on the original Dao to form Bo objects. It should be noted that the business methods in Bo usually target an object. If you need to span Multiple object objects, the methods should be placed in the service.

For example, a simple bank account management system creates a bo object for an account, which can contain business methods such as password modification and money collection (it is not difficult to see that, these methods only operate on a single account object ). Now you need to add a transfer method and put it in the service.

What is the relationship between service and Bo? Another example: Taking the state administration as an example: the grain Bureau is responsible for collecting food and selling seeds, and the Ministry of Construction is responsible for approving land sales and constructing highways. This is part of the administration. Suddenly a flood occurs somewhere, and the grain Bureau needs to open a warehouse for food relief. How can the Ministry of Construction coordinate the two Departments to build a temporary house? It is necessary to set up a special Disaster Relief Committee to allocate resources in two parts. Here, the two parts are Bo, and the relief board is service. I don't know if the expression is correct. When dividing services and Dao in Mode 1, the boundary is clear, but there will be unnecessary code.

The division of Mode 2 is relatively complex, but it can improve the coding efficiency.

Of course, in small-scale applications, if there is no service, Dao or Bo is acceptable.

3. Whether the service and Dao interfaces are available

An interface is a contract that can be implemented in multiple ways. Therefore, whether the interface is available depends on whether the specific implementation needs to be diversified. If there is only one implementation for a DaO or service, it is of little significance to abstract the interface. However, some large applications may require multiple implementations of DAO and service (for example, the account Dao in the preceding example may require a hibernate implementation, a CMP implementation, and a JDO implementation ), an interface is required to hide a specific implementation class at the upper layer.

There are two ways to hide the creation process of a specific implementation class: one is the practical factory method, the cost is a large amount of code (each DAO and service is a factory ). Second, use Spring IoC to implement dependency injection without writing additional code. This is also the second reason for introducing Spring.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.