Questions about serialization information mapping

Source: Internet
Author: User

The day before yesterday made a moderate error, because the properties of the upper-level mapping is not deleted, directly modified SQL, resulting in data mapping dislocation, and then data anomalies. I think this kind of mistake, it was thought of, but the mistake happened. Then think about it, the original Java has similar things, ORM mapping. At that time did not have a similar problem, and later looked at the client read the CSV, there is a further thinking. There is a distinction between the mapping of OO language and the points of attention and the point of attention of the C + + mapping method. And the change of direction also embodies the convenience of OO language.

If I were to implement a mapping relationship in code, my first thought was the reflection mechanism, which provided a very convenient deserialization, and the reflection was related to serialization. All say reflection consumption efficiency, then look at me below in C # write a test code, 10W times through reflection to create the entity class and through the normal direct creation of time contrast.

   1:using System;
   2:using System.Collections.Generic;
   3:using system.linq;
   4:using system.text;
   5:using System.Threading.Tasks;
   6:using system.reflection;
   7:
   8:namespace test
   9: {
  Ten:     class Program
One   :     {
  :         Static void Main (string[] args)
  :         {
  :             Console.Write ("Reflection begin:{0}s,{1}ms\n", Datetime.now.second,datetime.now.millisecond);
  :              for (int i = 0; i < 1000*10*10; i++)
  :             {
  :                 Test ect = (test) assembly.getexecutingassembly (). CreateInstance ("test. Test ");
  :             }
  :             DateTime stoptime = DateTime.Now;
  :             Console.Write ("Reflection end:{0}s,{0}ms\n", DateTime.Now.Second, DateTime.Now.Millisecond);
  21st:
  :             Console.Write ("Normal begin:{0}s,{0}ms\n", DateTime.Now.Second, DateTime.Now.Millisecond);
At   :              for (int i = 0; i < i++)
  :             {
  :                 Test ect = (test) assembly.getexecutingassembly (). CreateInstance ("test. Test ");
  :             }
  :             Console.Write ("Normal end:{0}s,{0}ms", DateTime.Now.Second, DateTime.Now.Millisecond);
  :             Console.readkey ();
  :         }
  :     }
  31:
  :      Public class Test
  :     {
  :          Public int test_0 {get; set;}
  :          Public int test_1 {get; set;}
  £ º          Public int test_2 {get; set;}
  Panax Notoginseng:          Public int test_3 {get; set;}
  :          Public int Test_4 {get; set;}
  :          Public int test_5 {get; set;}
Max   :          Public int test_6 {get; set;}
  :          Public int test_7 {get; set;}
  £ º          Public int test_8 {get; set;}
  :          Public int test_9 {get; set;}
  :          Public int test_10 {get; set;}
  :          Public int Test_11 {get; set;}
  :          Public int Test_12 {get; set;}
  :          Public int test_13 {get; set;}
  :          Public int test_14 {get; set;}
  :     }
  : }

The output is as follows:

Reflection in the efficiency of the consumption is still quite large, it looks very scary, but less write a lot of things, and planning table is difficult to 10W this magnitude. What's more, a SQL query comes out more than it is impossible to get so much data at once. The basic difference is still within milliseconds.

If the client uses a reflection mechanism to read the CSV table, then the program will only need to ensure that the variable name and the table fill in the header name consistent, and automatically create instances and methods through reflection, as long as the class name and CSV corresponding. If an error occurs, it is a compile-time error, and the exception control is more accurate. will not cause the runtime will be wrong and convenient only need to modify one place, do not need two names also need to align the operation, when the column more time to plan to change the table frequently, can ensure that the program here the first time to reflect, and abnormal prompt accurate.

However, C + + is not so good, C + + does not reflect. Now take a closer look at the mapping of CSV table parsing and the mapping technique of dbserver processing is consistent. The CSV is OK to use the string to do the one by one match will not appear dislocation. However, the mapping of the DB data does not make a precise correspondence based on the column name, but rather corresponds to the result set in the subscript form of the column array. compile-time is not checked! It is not always possible to know when the runtime is not seen!

C + + is very similar to the C language, C + + is more convenient and memory mapped. This is directly involved in a serialization, deserialization problem. We can only define serialization and deserialization methods to do our own processing. Of course, you can also use Json,xml,protobuf to do serialization and deserialization operations to reduce run-time errors.

Add one point: For exception handling, assertion, error alarm processing, if the efficiency of the premise as far as possible to control at compile time, and then start the run, and then the operation of the run!

Questions about serialization information mapping

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.