What is ORM? Why use ORM? Analysis on the use and pros and cons of ORM

What is ORM? Why use ORM? Analysis on the use and pros and cons of ORM

What is ORM

ORM (Object-relational mapping), Chinese translation for object-relational mapping, is a technology to solve the mismatch between object-oriented and relational databases. Simply put, ORM automatically persists the objects in the program to the relational database by using metadata describing the mapping between the object and the database.

Why use ORM

In program development, there is no correlation between the tables and fields stored in the database and the entity classes in the program, which is more inconvenient when implementing persistence. So, how to achieve persistence? A simple solution is to use hard coding to provide a separate method for each possible database access operation. This scheme has the following shortcomings:

  • 1. The persistence layer lacks flexibility. Once the business requirements change, the interface of the persistence layer must be modified
  • 2. The persistence layer is bound to the domain model and the relational database model at the same time. Regardless of whether the domain model or the relational database model changes, the poison modifies the relevant program code of the persistence, which increases the difficulty of software maintenance

ORM provides another mode for implementing the persistence layer. It uses mapping metadata to describe the mapping of object relationships, enabling ORM middleware to act as a bridge between the business logic layer and the database layer of any application

ORM’s methodology is based on three core principles:

  • Simple: Model data in the most basic form
  • Communication: The database structure is documented in a language that anyone can understand
  • Accuracy: Create a correctly standardized structure based on the data model

This article uses the C# programming language as an example. In traditional data reading operations, we use Ado.net to perform CRUD operations on the database. Basically, we use SQL hard-coded. For example, the following database query operations:

String sql = "SELECT ... FROM persons WHERE id = 10";
DbCommand cmd = new DbCommand(connection, sql);
Result res = cmd.Execute();
String name = res[0]["FIRST_NAME"];

Pseudo-code implemented in C# using ORM mapping:

Person p = repository.GetPerson(10);
String name = p.getFirstName();

The above sample code indicates that we can obtain an entity object from the data warehouse repository. Of course, the data warehouse may contain other methods. You can also define your own ORM implementation, such as:

Person p = Person.Get(10);

Usually, some filtering or query methods are exposed when processing ORM mapping and data warehouse, allowing the client to perform further filtering and other operations on the data set. For example, the code demonstrates querying users with ID=10 from the database:

Person p = Person.Get(Person.Properties.Id == 10);

Pros/cons

advantage

Compared with traditional database access technology, ORM has the following advantages:

  • Higher development efficiency
  • Data access is more abstract and portable
  • Support object-oriented packaging

Disadvantage

  • Reduce the efficiency of program execution
  • Fixed thinking

From the point of view of system structure, systems using ORM are generally multi-layer systems, and the more layers of the system, the efficiency will be reduced. ORM is a completely object-oriented approach, and object-oriented approach will also have a certain impact on performance.

When we develop systems, there are generally performance issues. Performance problems are mainly caused by incorrect algorithms and incorrect use of databases. The code generated by ORM is generally unlikely to write very efficient algorithms, and it is more likely to be misused in database applications, which is mainly reflected in the extraction of persistent objects and data processing. If ORM is used, The programmer is likely to extract all the data into the memory object, and then filter and process it, which is prone to performance problems.

summary

As a programmer, there will be different concepts in the use of ORM, and the specific choices need to be based on specific projects and scenarios.

This article published synchronized to Figure sharing network , " What is ORM? Why use ORM? Analysis on the use and pros and cons of ORM "

Reference: https://cloud.tencent.com/developer/article/1034514 What is ORM? Why use ORM? Analysis on the use and pros and cons of ORM-Cloud + Community-Tencent Cloud