Orm is an efficient development auxiliary tool. He has advanced ing ideas (compared with common datatable ing ). But remember that he is only a tool. He has reasons for his existence and shortcomings.
No tool is omnipotent, which can be understood by everyone. If we makeC LanguageWe will encounter problems when sending website cards or using C # For underlying game development,ThisWhat can be explained? Can C or C # be Spam? Obviously, no. The same is true for Orm. He has his own shortcomings, but he cannot evaluate his problems simply.
Here are some of my understandings.ORM (I used wilsonorm for more than two years ).
Background of ORM:
We already know about software development.OO thought's significance for us (Turning real objectsProgramAbstract objects (classes) in it, so that our development can become a simulation of the real society. Through this simulation,CodeNo longer bored. The object makes him beautiful. Various Objects make our software rich and colorful ). But we can't do anything about the database. It's just a two-dimensional mesh, which is his only structure. How can we make this unemotional thing adapt or "pseudo" adapt to our beautiful OO?
Thus,Orm appears. He cleverly solved the problem. He sets table as a class and a piece of data as an instance of this class. In turn, these instances are converted into data rows again to operate on the database.
DatabaseBytes-> Objectspace and namespace
Data TableBytes-> Class
Data rowBytes-> Instance (object)
Data ColumnBytes-> Attributes
Data OperationsBytesÀClass Method
Features of ORM
See what kind of result we get after this ing. Data Tables are familiar to us. We can understand our model in this way.
Objectset users = space. getobjectset <user> ("userid> 100 ");
(Users [2] As user). Name = "xxx ";
Users. persit ();
From this point of view, we no longer care about the data columns, but we can only operate on objects.
Advantages of ORM
1. model the data table to make the logic clearer and easier to understand.
This advantage is the one mentioned at the beginning, which maps two-dimensional data tables into objects. Such development looks clearer, and it is better than all rows and columns.
2: quick development.
Common Database Operations are nothing moreCrud. You can use ORM to easily create, modify, and delete data. Compare the two codes:
String SQL = 'Update T1 set (xx = xxx, XXXX = XXXXX )';
T1.xx = xxx
T1.xxx = xxxx
T1.persit ()
If there are few field tables, there is no difference. If there are dozens of fields, it is very troublesome to use strings and it is easy to write errors. If you useOrm, at least the attributes of the class can be noticed, there are few opportunities for errors, and the code looks much more beautiful, easy to read and maintain.
For dataset fetch operations,Orm cannot help. It can retrieve the data of a table, but it is very weak to connect data tables and obtain specific fields. Not to mention the execution of some complex SQL statements.
3: Other auxiliary tools
Orm provides many other operational functions to remedy their weaknesses. Different ORM provides different features. Wilsonorm provides the following functions:
Executecommand ();
Executedelete
Executescalar
Executeupdate
Getdataset// You can execute complex SQL statements.
Getobjectcount
In addition, different database types are provided.Connector, and database paging technology (this function is very valuable ).
Lack of ORM
1. Data Retrieval flexibility
For example, multi-table join. ThisIt cannot be implemented in the ORM (difficult to implement ). This is very common, especially when making statistics.
For example, the extracted junk data (only a certain column is obtained,).
2: Running Speed
Since ORM requires a lot of operations, such as ing objects, it must consume some system performance. This can also be understood as a result of rapid development, which will inevitably lead to impaired operation efficiency.
How to UseORM
Develop strengths and circumvent weaknesses. Moderate use.
1: simple operations on a large number of data tables Use ORM. General operations, especially insert, modify, delete, and so on, Use ORM for fast and convenient.
2: use other methods for modules that require flexible operations. For example, to use multi-table join or other complex SQL logic, you need to use other tools.
Conclusion:
Rational UseOrm is a process of "Improving strengths and avoiding weaknesses. This is a general attitude towards tools. Only with such rational treatment of ORM can we benefit from it and let tools serve us, rather than being tired of tools.