Data Access Layer

Source: Internet
Author: User

DAO mode: Explain the idea and technology of layered development in software development, with the expansion of software scale and complexity of business, a software is divided into several levels to develop, the big small, divide and conquer, is to shorten the software development time, improve the efficiency of software development is an effective method, but also the current software development has been used.

Data persistence: Many programs have the need to save data and read data. When the program is running, the data stored in memory is instantaneous and will be lost after the shutdown. To persist the data, you need to save the data to disk, such as to a database or file. In this way, when the program starts, it can read data from disk, change data or close the program, save data to disk, and make the data persist.

Here comes a term: persistence. Persistence is the mechanism by which data in a program is transformed between the transient state and the persistent state. JDBC is a persistence mechanism that saves programs directly to text files is also an implementation of the persistence mechanism, but what we often use is to save the data to the database.

DAO is the data access object that implements access to persisted data between business logic and persisted data. In the object-oriented design process, there are some "routines" to solve specific problems, called patterns. The DAO pattern provides an interface for accessing the operations required by a relational database system, separating the data access from the business logic and providing an object-oriented data access interface to the upper layer.

The advantage of the DAO pattern is that it achieves two isolation.

? Data access code and business logic code are quarantined. The business logic code calls the DAO method directly and does not completely feel the existence of the database table. The Division of labor is clear, the data access layer code changes do not affect the business logic code, which conforms to the principle of single function, reduce the coupling, improve the reusability.

? Isolation of different database implementations, using interface-oriented programming, if the underlying database changes, such as from Oracle to SQL Server, as long as the new implementation of the DAO interface class can be, the original Oracle implementation without modification, which conforms to the "open-closed" principle, which reduces the coupling of the Code, Improved code extensibility and system portability.

A typical DAO pattern consists mainly of the following parts.

? DAO interface: Defines all operations of the database as an abstract method, which can provide multiple implementations.

? DAO implementation class: The concrete implementation of DAO interface definition method is given for different database.

? Entity class: Used to store and transfer object data.

? Database connection and Shutdown tool class: Avoids the reuse of database connection and shutdown code, and makes it easy to modify.

Benefits of tiered development

With the DAO pattern, the data access code is extracted, the DAO interface and the implementation class to implement the function, the data access layer is formed, the layer code is generally placed under the DAO package, the other layers no longer consider cumbersome data access operations, between layers through the entity class to transfer data.

? Each layer focuses on the realization of its own function, which facilitates the improvement of quality. Different levels of attention are different, the data access layer is mainly database access, the business logic layer is focused on business logic, the front section of the presentation layer focused on the layout and aesthetics of the page. According to different levels need to be the most suitable technical staff to achieve, thereby improving the quality of development.

? Facilitate the division of work and collaboration, thus improving efficiency. Once the interface between the various levels is defined, each developer's task is confirmed, and the developers at different levels can do their job, go hand in hand, and greatly accelerate the development process. facilitates code reuse. Once each module is defined with a unified folio interface, it can be called by each module without duplicating the same functionality. For example, different business logic modules, if you need to operate the same database tables, do not need to develop the corresponding DAO module, reuse can be.

? Facilitates program expansion. For example, the DAO pattern adopts interface-oriented programming, the underlying database changes, can be solved by increasing the new implementation of the interface, implementing lossless substitution, rather than modifying the original code, so as to facilitate the extension of the program.

Principles of layering

? Each floor has its own responsibility.

? The previous layer does not care about the next layer of implementation details, the previous layer with the external interface provided by the next layer to use its function.

? The previous layer calls the next layer of functionality, the next layer cannot invoke the previous layer of functionality. The next tier provides services to the previous tier rather than the services provided on the previous tier.

Similarly, in layered development, hierarchies should also check for similar principles. Encapsulation principle: In simple terms, each level provides an exposed unified interface to the outside, while hiding the internal functionality implementation details, other levels can not and do not need to understand its internal details.

? Sequential access principle: The next layer provides services to the previous tier rather than the services provided on the upper level. The business logic layer can access the capabilities of the data access layer, and the data access layer cannot access the business logic layer functionality. Not access each other, but sequential access. Each layer has its own responsibilities. For the data access layer, its responsibility is to perform data access operations. To improve encapsulation, it should be noted that the interfaces provided to the business logic layer should not contain data access details.

Using entity classes to pass data

In a hierarchical structure, data is transferred between different tiers through entity classes. After encapsulating the relevant information using entity class, it is very convenient to use the entity class as the input parameter or return result of the method in the program. With regard to entity classes, there are mainly the following characteristics.

? The properties of an entity class generally use the private adornment.

? provide a Getter/setter method for the properties of an entity class based on business needs and encapsulation requirements, responsible for reading and assigning properties, and generally using public adornments.

? The method of constructing non-parameter for entity class is provided, and the corresponding constructive method is provided according to business requirement.

? Entity classes are best implemented java.io.Serializable, which supports serialization mechanisms that can be converted to byte sequences and stored on disk or transmitted over a network.

? If the entity class implements java.io.Serializable, the attribute serialversionuid should be defined to resolve the serialization problem between the different versions. For example: private static final long serialversionuid=2070056025956126480l;

Data Access Layer

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.