The analysis of DAL, DAO, ORM and Active record

Source: Internet
Author: User
Tags ruby on rails

Transferred from: http://blog.csdn.net/suiye/article/details/7824943

Model

A model is a concept in MVC that refers to the operation (business logic) of reading and altering data. At first we put the database-related code directly in the model (SQL is written directly in the code), which can lead to later maintenance is quite troublesome. Changes to the business logic require the developer to re-write the SQL, and if the project needs to be separated, the SQL statements need to be extracted and placed on a separate layer. This layer is the DAL (data Access layer).

Persistent layer Persistence

The persistence layer is only a logical concept, the main task is to save the data, generally refers to the database or file, can also be responsible for the completion of related behavior.

Persistence layer refers to the long-term preservation of data, such as the database on the long-term storage of data on the hard disk, XML can also be long-term preservation of data, and if the data stored in the specified file, can also become a persistent layer.

Persistence can be understood as a verb. Hibernate in Java is the persistence of operations, mainly on the database underlying or mapping, so we do not have to care about the annoying relationship mapping, directly manipulate the object can be.

DAL data access layer, data access tier

The DAL is a data access layer in a three-tier architecture (presentation layer, business logic layer, data access layer), is a concept or a scheme, it consists of many DAO, or the concrete implementation of DAO, is to encapsulate the database-related code, so when we execute the Sub-Library, we only use to adjust the DAO code, The model doesn't care whether the data it uses is placed in a or B libraries.

The purpose of designing the data Access layer interface is to allow the business logic layer not to invoke the implementation of the specific data access layer (which does not depend on the specific implementation technology of the data Access layer), the advantage is that the business logic does not have to control what the data access layer is specifically what technology to implement, the interface is unchanged.

The advantages of using the DAL are as follows:

1, the developer can only focus on the entire structure of one of the layers;

2, can easily use the new implementation to replace the original level of implementation;

3. Can reduce the dependence between layer and layer;

4, in favor of standardization;

5, facilitate the reuse of the logic of each layer.

In summary, layered design can achieve the following objectives: Decentralized attention, loose coupling, logical multiplexing, and standard definitions.

The practical applications to reduce coupling are as follows:

The business logic does not have to control what the data access layer is specifically what technology to implement, the interface is constant, the data access layer can be implemented using JDBC, but also can be implemented with hibernate, and the replacement is not very cumbersome, so the coupling reduces

DAO data Access Object

DAO is a guiding principle of software design, which is introduced in the core Java EE pattern: In order to build a robust Java EE application, all access operations to the data source should be abstracted in a common API. In the language of programming, it is the creation of an interface that defines all the transactional methods that will be used in this application. In this application, the interface is used when it is necessary to interact with the data source, and a separate class is written to implement the interface that logically corresponds to this particular data store.

As the name implies is dealing with the database, sandwiched between business logic and database resources, is the specific implementation of the DAL.

Simply said DAO layer is the database of data additions and deletions and other operations encapsulated in the special class, in the business logic layer if you want to access the data, directly call the DAO class (including how to access the database and data additions and deletions and other code), you can return data, Instead of having to write the code in the business logic layer.

ORM object-relational Mapping, Object relational mapping

ORM is also a kind of encapsulation of database access, but ORM is not like DAO is a software design guidelines, emphasizing that the system should be hierarchical, more like a tool, with mature products, such as the Java World is very well-known hibernate, and many PHP framework comes with the ORM Library. Their advantage is that the data objects in your program can be automatically converted to the corresponding tables and columns in the relational database, and the reference between the data objects can be transformed into joins between the tables through this tool. The advantage of using ORM is that your development has virtually no access to SQL statements. Create a table, declare a corresponding class, and then you just use an instance of this class to interact with, as for the data in this object how to store and how to get, all do not care.

Active Record

The Active record is an ORM pattern that fires with the popularity of Ruby on Rails, which integrates the code that is responsible for persistence into the data object, that is, the data object knows how to store itself in the database. This is different from the previous ORM, where the traditional ORM separates the data object from the code that is responsible for persistence, and the data object is simply a struct that contains data, passed in the model layer and the ORM layer. In the active record, the model layer integrates the ORM functionality, which represents both the entity, the business logic, and the data object, and is responsible for storing itself in the database, of course, the part of the stored code is already implemented in the model's parent class, which is part of the framework. The model simply calls the parent class's method to complete the persistence.

The following excerpt from The Thinkphp Handbook

Follow the standard ORM Model: tables are mapped to records, records are mapped to objects, fields are mapped to object properties. Together with the naming and configuration conventions followed, the operation of the model can be implemented very quickly and is simple and understandable.

The main ideas of ActiveRecord are:

1. Each database table corresponds to the creation of a class, each object instance of the class corresponds to a row of records in the database, and usually each field in the table has a corresponding field in the class;

2. ActiveRecord is also responsible for persisting itself, encapsulating the access to the database in the ActiveRecord, i.e. curd;

3. ActiveRecord is a domain model that encapsulates some of the business logic;

ActiveRecord comparison applies To:

1. Business logic is relatively simple, when your class basically corresponds to table one by one in the database, ActiveRecord is very convenient, that is, your business logic is mostly for single-table operations;

2. When a cross-table operation occurs, it is often used in conjunction with the transaction script (Transaction script) to elevate the cross-table transaction to the transaction script;

3. The biggest advantage of ActiveRecord is simple and intuitive. A class includes data access and business logic. It is more convenient to use with the code generator;

These advantages make ActiveRecord particularly suitable for rapid web development.

The analysis of DAL, DAO, ORM and Active record

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.