Persistence mode, part 2nd: Improve code reuse and improve performance

Source: Internet
Author: User

Brief introduction

This series contains two articles, and part 1th discusses several basic best practices for Hibernate and other object-relational mapping (ORM) tools. By using common base domain classes and interfaces, centralized auditing, and generic data Access Objects (generic DAO), applications can build a more compact and maintainable layer of domain models and persistence.

By applying the concepts in part 1th, you can provide new opportunities for code reuse. In this section, we first discuss how to use Hibernate and polymorphism to integrate behavior into the domain model. Next, proceed to the 1th part of the discussion of the generic DAO. After you integrate and use the generic DAO in your application, you may find more common operations. We'll show you how to integrate data paging and querying in a generic DAO, which reduces code. Finally, the strategy of improving the performance of domain model is discussed. Without these policies, the association of misconfigured Associations in the domain model can result in a large number of redundant queries, as well as the acquisition of unneeded records, which can be a waste of resources.

Re-discussion Model: let ORM select Behavior

Because the database tables themselves do not behave, developers often place the behavior of the domain model entity in the service or view layer. However, this approach is not appropriate because it violates the object-oriented basic rule: Object ownership behavior and data. If the behavior is transferred from the object to the service, the object becomes a pure data container. In addition, placing an entity's behavior in a service or view layer causes the core logic of the entity to be dispersed in many parts of the application, leading to maintenance problems. Tools such as Hibernate help to put behavior and data in the model, which helps to construct domain-driven models.

Let's continue with the example of the employee used in part 1th. The object model given in Figure 1 defines the pay-for-payment approach function:

Figure 1. The object model of salary payment method calculation

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.