Reply: What is activerecord?

Source: Internet
Author: User
On
Activerecord was mentioned in the previous article. Someone asked in a reply what is activerecord. to be honest, I have been a little confused since I used subsonic last year. What is activerecord, What Is activerecord, and how should I use it. I want to share some of my ideas with you. Welcome to shoot bricks .
Activerecord
What is:
1. Each database table corresponds to the creation of a class. Each object instance of the class corresponds to a row record of the table in the database; Generally, each field of the table has a corresponding field in the class;
2. activerecord is also responsible for making its own persistence. activerecord encapsulates access to the database, that is, crud;
3. activerecord is a domain model that encapsulates some business logic;

ActiverecordNothing:
1.Row data Gateway
In the row data gateway mode, each object also encapsulates the database record state and the access method that persists to the database. These two objects are sometimes difficult to distinguish. the subtle difference is that row data gateway does not encapsulate any business logic;

2. tablegateway
Tablegateway is a data access mode. Each table has a class. The class method encapsulates data operations on a single table, such as crud. The value of the table field accepted by the method is used as a parameter;
For example, if the table person has daoperson, you can use the following methods:
Int create (string name, bool ismale)
Dataset find (INT personid)
Void Delete (INT personid)
Void Update (INT personid, string name, bool ismale)
Many of MicrosoftCodeThis mode is used in the example;

The difference between activerecord is that the activerecord object maintains the record value and is stateful, while tablegateway is stateless, just a set of database access methods;

3. Table Module
Table module is a logic mode in the field. A Class corresponds to a table in the database. Table module works with table gateway. The former is responsible for basic business logic, and the latter is responsible for database access, to achieve isolation between the logic layer and the persistence layer, Microsoft's instance code often uses the two. For example, to table person, two classes are usually defined, personbl and persondb, processes verification and other logic in personbl, and CALLS persondb to access the database, and CALLS dataset or custom data transmission objects to transmit data between layers.

When the business logic is relatively simple and there is a one-to-one relationship with the table, activerecord is relatively simpler, because it includes business logic objects and data access in a class, data transmission objects are not required, which reduces the maintenance workload;
Compared with the table module, activerecord is more tightly coupled with the database;

ActiverecordApplicable:
1. the business logic is relatively simple. When your class is basically one-to-one with the tables in the database, activerecord is very convenient, that is, most of your business logic is for single table operations;

2. When a cross-table operation occurs, the transaction script is often used together to promote the cross-Table transaction to the transaction script;

3. activerecord is simple and intuitive. A class includes data access and business logic. It is more convenient to use with the code generator;

4. These advantages make activerecord particularly suitable for rapid web development. It is precisely the rapid development framework ror that adopts activerecord, and many ror-like frameworks, such as castle, follow suit to bring activerecord to everyone's attention;
I think that's why Martin Fowler has already proposed this model in poeaa, but it was not until the last two or three years that activerecord was hot;

ActiverecordNot Suitable
1. although activerecord has business logic, it is basically based on a single table. the cross-Table logic is usually placed in the transaction script when a cross-table operation occurs. if there are more and more associations between objects, your transaction scripts are getting larger and larger, and there are more and more duplicate code, you need to consider domain model + O/R mapper;

2. activerecord stores data so that it sometimes looks like a data transmission object (DTO ). however, activerecord has database access capabilities and should not be used as DTO. activerecord objects cannot be passed, especially when calls are made across process boundaries;

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.