My OR mapping actual development experience

zhaozj2021-02-12  141

I. Overview

O / R mapping full name Object Relational maping is an object relationship mapping. Direct operation of the table, becomes a direct operation of the properties and methods of the persistence class.

Many projects are based on the development of the database, and the problem is to be increased, deleted, changed, and inquired.

For example, the following C # code, deck the table Customer data from the database CustomerDemo:

String connectionString = "data source = william; persist security info = true; initial catalog = customerdemo; user ID = sa; password = sasa;

SqlConnection thisqlconnection = new SqlConnection (Connectionstring);

String query = "Select Customername from Customer Where Customerid = 1";

Sqldataadapter thesqldataadapter = new sqldataadapter ();

THESQLDATAADAPTER.SELECTCOMMAND = New SQLCOMMAND (Query, THSQLCONNECTION);

DataSet CustomerDataSet = New DataSet ();

THSQLDATADAPTER.FILL (CustomerDataSet);

DataTable CusomerDataTable = New DataTable ();

CusomerDataTable = CustomerDataSet.Tables [0];

/ / Judging whether the data set is empty

IF (CusomerDataTable.Rows.count> 0)

{

DataRow Dr = CusomerDataTable.Rows [0];

// Is it easy to leaving a hidden danger?

IF (! Dr.isnull ("Customername"))

{

TXTCUSTOMERNAME.TEXT = DR ["Customername"]. TOSTRING ();

}

}

THSQLCONNECTION.CLOSE ();

If you encapsulate the table with an O / R mapping to a persistent class, the data of the read table becomes the properties of the access persistence class. E.g:

// The following is just an analog code of a pseudo code, and the different O / R mapping technology packages, and the code writes will be different.

/ / Establish a data connection Connection

Connection conn = DataSource.getConnection ();

// Establish a sustainable plant

SESSIONFACTORY THESessionFACTORY = ConfigurationFactory.BuildSessionFactory (conn);

/ / Instantiate a Customer and take the object of CustomerId = 1

Customer thecustomer = (customer) ThessionFactory.createObject (Typeof (Customer), "

1"

);

// Take the object's properties to assign

TXTCUSTOMERNAME.TEXT = thecustomer.name;

CONN.CLOSE ();

The above code is quite concise, and some commonly used operations on the table are packaged inside the class. O / r mapping has considerable benefits to the development of the entire project:

1, programmer

(1) A general database application project, nothing more than a large number of tables, the field of SELECT, INSERT, DELETE, and EDIT operations. There is not much technical difficulty in these operations, which is time-consuming, and carefully deal with it, and the judgment is used in the IF statement. It is obvious to improve the package of labor, improve development quality and efficiency. (2) O / R mapping The persistence class and corresponding XML mapping files need to be written, can be automatically generated by tools, which greatly reduces the code amount of the program. In actual use, approximately 20% of the code quantity.

(3) The deduplication of programmers is reduced, and the programmer can have more energy to put in other technical aspects.

(4) The code style is unified, the source program is clear and simple, readable. The level of programmers is different, and the coding style has various features, read the source program written by others, and believes that they are a painful thing. After using O / R mapping package, you can regulate your development style from the source, read and modify the source of others.

(5) Let the programmer grow up in a good-oriented environment, cultivate their thinking and coding experiences for object-oriented, so that object-oriented ideas are unimported in the usual project development.

2, system analyst

(1) It is conducive to system analysts to use all object-oriented to be solved in system analysis, design, encoding, and testing. The analysis design document will not appear to write a large number of classes to the coding phase, or the source program sees a large SQL statement full day. With the persistent solution for objects, the system analyst can do not have worries, fully use object-oriented to design analysis. And the UML document written is very clear.

(2) Since the O / R mapping is fully encapsulated in the form of the table, the coupling of the table is greatly reduced. It can significantly improve the extensibility of the project structure and flexibility, making it easier to modify and upgrade.

(3) O / R mapping is not a 1st lattice function library, you can also encapsulate logic commonly used in the project, such as: role assignment, permission control, user login registration, etc., etc., the department of the tree structure, etc. You can support component reuse at the database level. This database-level component is completely isolated from the actual database through o / r mapping, and the reuse is very strong.

(4) The development of the function module that can reduce some attachment, such as chat rooms, forums, shopping systems, SMS channels, etc., through o / r mapping, source programs no longer be derived directly with the database, only need to change Map files can be hung on other existing systems. And reduce the difficulty of project productization.

(5) After the coupling of database and project source program is greatly reduced, the project can be easier to transplant into other databases.

3, project manager

(1) O / R mapping is not complicated, no matter whether there is no experience in development, it is familiar with the object, and it can quickly get started. The programmer does not have to pay attention to the operation section of the database in the program, which can reduce the technical requirements of the programmer (I have organized an item developed with O / R mapping, with several programmers developed, about development 4 For a month, the project was developed, and several programmers didn't know how to use ADO.NET. The project manager can have a larger space to distribute work.

(2) The project is easier to implement the framework, component reuse and accumulation, and improve the quality and efficiency of development. (3) You can automate the source program of some interfaces such as ASP, JSP).

Of course, O / R mapping is just a technology solution, shortcomings and limitations will tell when the final summary of the article.

Second, O / R mapping basic framework

The important part of o / r mapping is the mapping between the table and the persistence class, and now there are two main ways:

One is a simple persistence class mapping: the mapping between the table and the persistence class is written by hardcoding, compiled after compiling. This way is used straight and the programmers can control more and run fast. The disadvantage is that if you change the field, type, etc., you need to change the code in the class directly, and then compile it.

Another type is to achieve mapping with the XML and persistence classes. The persistence class is the mapped entity class, most of the type, length, whether it can be modified, whether it can be inserted, etc., the type, length, whether it is empty, etc., is expressed by XML files. . When the mapping relationship of the table needs to be changed, simply change the XML section, the persistence class does not need to be changed and recompile. This way is now popular, it is flexible, and the coupling is lower. The following is a piece of code for a Grove.net map:

Persistence class:

Public Class Customer PUBLIC CLASS CUSTOMER

{

Int Customerid;

String name;

[Keyfield ("Customerid")]]]

Public Int Customerid

{

Get {return this.customerid;

Set {this.customerid = value;

}

[DataField ("Customername")]]]]

Public String Name

{

Get {return this.name;}

Set {this.name = value;

}

}

The map of the XML file:

Customer

Steps to initialize a persistence class from the table:

1 Read the table of the database -> 2 Find the mapped persistence class in the XML file, and initialize the field name of the table -> 3 one-by-read table, find out the properties of the mapped persistence class in XML - > 4 By the reflection of the class, the step of saving a persistent class to the table with the data of the field of the table to the class.

1 Via the XML file, find the table field mapped in the persistence class -> 2 by reflection, read the table field mapped by one by one -> 3 to assign the value of the attribute to the mapped table field -> 4 save the table Data to the database

The above is just a general processing steps, in the actual O / R mapping, there is different techniques for developing languages.

转载请注明原文地址:https://www.9cbs.com/read-6876.html

New Post(0)