Method for implementing a trigger mechanism based on an orm framework
By introducing a trigger mechanism into the ORM framework, defining data maintenance timing and custom attributes, and utilizing .NET's reflection mechanism to achieve automatic data integrity and consistency maintenance during data operations, the complex SQL statement operations and data integrity and consistency maintenance problems in existing technologies are solved, improving software maintenance efficiency and reducing code maintenance costs.
Patent Information
- Application Number
- CN202311323188.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-13
- Publication Date
- 2026-02-13
- Estimated Expiration
- 2043-10-13
AI Technical Summary
Existing ORM frameworks involve complex SQL statement operations and data integrity and consistency maintenance processes, making it difficult to simplify the code at the upper-level business logic layer.
By introducing a trigger mechanism into the ORM framework, defining data maintenance timing and custom attributes, and utilizing .NET's reflection mechanism, automatic data integrity and consistency maintenance is achieved during data operations, including operations such as adding, modifying, deleting, and querying.
It simplifies the code at the upper-level business logic layer, improves software maintenance efficiency, reduces code maintenance costs, and enables automatic maintenance of data integrity and consistency.
Smart Images

Figure CN117235050B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The application belongs to the technical field of databases, and particularly relates to a trigger mechanism implementation method based on an ORM framework. BACKGROUND
[0002] Object Relational Mapping (ORM) is to automatically persist objects in an object-oriented language program into a relational database by using metadata describing the mapping between objects and databases. Objects and relational data are two forms of business entities, and business entities are represented as objects in memory and as relational data in databases. There are association and inheritance relationships between objects in memory, while relational data cannot directly express many-to-many association and inheritance relationships in databases. In practical applications, a mapping is made between a relational database and business entity objects, so that when we operate business objects, we do not need to deal with complex Structured Query Language (SQL) statements, but can operate them as usual. An ORM framework is a program for implementing the ORM technology.
[0003] The prior art (CN 111966704 A) discloses an ORM framework implementation method, system, computer device and storage medium, specifically discloses determining configuration information based on a preset class definition, the class definition includes query mapping, the query mapping indicates the mapping relationship between the class and the query SQL, in the case of obtaining a database operation instruction, constructing SQL according to the configuration information and the database operation instruction, generating operation SQL; the above prior art automatically generates an object-oriented language to access the database SQL language to operate the database, and automatically converts the obtained database data into objects, so that programmers can not care about the specific design of the database, but focus on the upper business view, but it is relatively complex.
[0004] Therefore, the technical problem to be solved by the application is to simplify the upper business logic layer code and the representation of the internal logical relationship of the data integrity and consistency maintenance process when operating data. SUMMARY
[0005] To solve the technical problem of simplifying the upper business logic layer code and the representation of the internal logical relationship of the data integrity and consistency maintenance process when operating data, the application provides a trigger mechanism implementation method based on an ORM framework.
[0006] The specific scheme is as follows:
[0007] The application discloses a method for implementing a trigger mechanism based on an ORM framework, and specifically comprises the following steps:
[0008] Step S1: defining a data maintenance time through a custom attribute of.NET;
[0009] Step S2: modifying a data maintenance process through the custom attribute of.NET, and binding an association between a data processing time and the data maintenance process;
[0010] Step S3: adding reading of a corresponding data maintenance time in a bottom-layer data driving part, and activating the corresponding data maintenance process by using a reflection mechanism of.NET.
[0011] In step S1, the data maintenance time includes but is not limited to the following: data maintenance before adding, data maintenance after adding, data maintenance before updating, data maintenance after updating, data maintenance before deleting, data maintenance after deleting and data maintenance after querying; wherein, 'before adding' is defined as 'BeforeInsert', 'after adding' is defined as 'AfterInsert', 'before updating' is defined as 'BeforeUpdate', 'after updating' is defined as 'AfterUpdate', 'before deleting' is defined as 'BeforeDelete', 'after deleting' is defined as 'AfterDelete', 'after querying' is defined as 'AfterConstruct', in addition, 'None' is added as an initial value, and 'All' is added as an operation corresponding to all events.
[0012] In step S2, a custom attribute for modifying the data maintenance process is defined, and the custom attribute contains the data maintenance time for triggering the data maintenance process.
[0013] In step S2, an input parameter of the data maintenance process is normalized, and the parameter includes a current execution trigger time 'Timing', a database 'AbstractDao' where data accessed in the trigger process is located, and a boxing operation result 'object[]' of the.NET framework when the reflection mechanism is executed.
[0014] In step S2, a definition manner of the data maintenance process is defined as a processing process function accompanied by data operation, and the function has no return value after execution.
[0015] A corresponding mapping relationship between the data maintenance time and the data maintenance process is defined through a configuration file of data bottom layer or is defined in a code by using a metadata programming mode.
[0016] The data operation driving module can read and process the association between the defined data maintenance time and data maintenance process from the ORM object model definition through the reflection mechanism of.NET itself, and place it in the defined Trigger structure, specifically as follows:
[0017] S21: input type name;
[0018] S22: obtain all method information of the specific type;
[0019] S23: traverse all the read method information;
[0020] S24: read a current untraversed method information from all the method information;
[0021] S25: check whether the method information is modified by the TriggerAttribute custom attribute, if the method information is modified by the TriggerAttribute custom attribute, continue to execute, if the method information is not modified by the TriggerAttribute custom attribute, directly jump to step S28 and continue to execute;
[0022] S26: read the data maintenance mechanism in the custom attribute corresponding to the method together with the above method information, generate a Trigger variable, and add the variable to the 'Trigger collection';
[0023] S27: mark the current obtained method information as read;
[0024] S28: check whether the traversal of all method information is completed, if the traversal of all method information is completed, continue to execute, if the traversal of all method information is not completed, return to step S24 and continue to execute;
[0025] S29: return the generated Trigger collection, and the processing is ended.
[0026] In the data operation driving module, different data operation time combinations are combined with specific TriggerContext scene instances, and the Invoke function in the Method class under.NET is used to execute the triggering function to complete the triggering execution of the "data maintenance process", specifically as follows:
[0027] S301: input the trigger scene (TriggerContext), target object collection (ICollection), and trigger collection (Trigger[]);
[0028] S302: Obtain the current data maintenance opportunity through the input trigger scene definition, and obtain all data maintenance process definitions required to be executed this time according to the obtained data maintenance opportunity and the input trigger set;
[0029] S303: Traverse the target object required to be maintained;
[0030] S304: Obtain a target object not traversed at present, denoted as target;
[0031] S305: Traverse all data maintenance processes required to be executed this time obtained at present;
[0032] S306: Obtain a data maintenance process not traversed at present, denoted as process;
[0033] S307: Execute the data maintenance process by triggering the process;
[0034] S308: Mark the current data maintenance process as a traversed state;
[0035] S309: Check whether the traversal of all data maintenance processes is completed, if the traversal of all data maintenance processes is completed, continue to execute, if the traversal of all data maintenance processes is not completed, return to step S306 and continue to execute;
[0036] S310: Mark the target object read at present as a traversed state;
[0037] S311: Check whether the traversal of all target objects is completed, if the traversal of all target objects is completed, the processing is ended, if the traversal of all target objects is not completed, return to step S304 and continue to execute;
[0038] S312: The processing is ended.
[0039] The application has the following beneficial effects:
[0040] The application extends the ORM framework, so that the ORM framework has a mechanism for automatically maintaining data integrity and consistency when operating the data model (data increase, modification, deletion and query), and the mechanism can effectively improve software maintenance efficiency, reduce code maintenance cost, facilitate subsequent software maintenance and upgrade, and realize the trigger function in the database by using the.net language to add the trigger. BRIEF DESCRIPTION OF DRAWINGS
[0041] Figure 1 is a schematic diagram of a data operation principle.
[0042] Figure 2 is a processing flow schematic diagram of mapping the data maintenance time defined in the extraction object to the data maintenance process.
[0043] Figure 3 is a flow schematic diagram of the triggering scenario in the running process. DETAILED DESCRIPTION
[0044] The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the present application. Obviously, the described embodiments are only part of the embodiments of the present application, but not all the embodiments. Based on the embodiments of the present application, all the other embodiments obtained by those skilled in the art without creative work fall within the scope of protection of the present application.
[0045] As shown in Figure 1 the present application defines eight data maintenance time points, i.e. before query, after query, before adding, after adding, before modifying, after modifying, before deleting and after deleting, based on the trigger mechanism implementation method of the ORM framework. The user divides the implementation time of the data specific integrity and consistency maintenance requirements according to the above maintenance time points, and compiles specific data maintenance process for the data maintenance requirements of each data maintenance time point. When the data is operated (added, modified, queried and deleted), it is checked whether the data maintenance process is defined for the above defined data maintenance time point. If the data maintenance process is found, the data maintenance process is executed at the specific data maintenance time point according to the data maintenance time point corresponding to the process, and finally the integrity and consistency of the data are maintained at the data representation level.
[0046] Specifically, the following steps are included.
[0047] Step S1: defining the data maintenance time point through the custom attribute of.NET;
[0048] Step S2: modifying the data maintenance process through the custom attribute of.NET, and binding the association between the data processing time point and the data maintenance process;
[0049] Step S3: adding the reading of the corresponding data maintenance time point in the underlying data driving part, and activating the corresponding data maintenance process by using the reflection mechanism of.NET.
[0050] Wherein, in step S1, the data maintenance timing, including but not limited to: new data maintenance, new data maintenance, update data maintenance, update data maintenance, delete data maintenance, delete data maintenance and query data maintenance. Wherein, 'new' is defined as 'BeforeInsert', 'new' is defined as 'AfterInsert', 'update' is defined as 'BeforeUpdate', 'update' is defined as 'AfterUpdate', 'delete' is defined as 'BeforeDelete', 'delete' is defined as 'AfterDelete', 'query' is defined as 'AfterConstruct', in addition, 'None' is added as the initialization value, 'All' is added as the corresponding operation of all events.
[0051] In step S2, it also includes defining the custom attribute of the modified data maintenance process, which contains the data maintenance timing of triggering the data maintenance process.
[0052] In step S2, the input parameters of the data maintenance process are reduced, which includes the current execution trigger timing 'Timing', the database where the data accessed in the trigger process is located 'AbstractDao', and the boxing operation result of the.NET framework when executing the reflection mechanism 'object[]'.
[0053] In step S2, the definition of the data maintenance process defines the processing process function accompanied by the data operation, and the input parameter of the function is 'TriggerContext'. After the function is executed, there is no return value.
[0054] The mapping structure of the data maintenance timing and the data maintenance process, that is, the Trigger structure, is as follows:
[0055] / / / <summary>
[0056] / / / represents a trigger
[0057] / / / < / summary>
[0058] public class Trigger
[0059] {
[0060] / / / <summary>
[0061] / / Method executed at trigger
[0062] / / < / summary>
[0063] private MethodInfo method;
[0064] / / / <summary>
[0065] / / / Timing of the trigger
[0066] / / / < / summary>
[0067] private Timing timing;
[0068] / / / <summary>
[0069] / / / Constructor
[0070] / / / < / summary>
[0071] / / / <param name="method"> Trigger association methods
[0072] / / / <param name="timing"> Trigger trigger timing
[0073] internal Trigger(MethodInfo method, Timing timing)
[0074] {
[0075] this.method = method;
[0076] this.timing = timing;
[0077] }
[0078] / / / <summary>
[0079] / / / Get trigger occasion
[0080] / / / < / summary>
[0081] public Timing Timing
[0082] {
[0083] get { return timing;}
[0084] }
[0085] / / / <summary>
[0086] / / / Method for obtaining trigger association
[0087] / / / < / summary>
[0088] public MethodInfo Method
[0089] {
[0090] get { return method;}
[0091] }
[0092] }
[0093] The mapping relationship between data maintenance timing and data maintenance process is defined through the underlying data configuration file or by embedding it into the code through metadata programming.
[0094] In this invention, the Trigger is defined to store the mapping relationship between the timing of data maintenance and the data maintenance process;
[0095] TriggerAttribute, a custom attribute, modifies the relevant data maintenance process, indicating the timing of data maintenance corresponding to the data maintenance process;
[0096] MethodInfo, the metadata representation of the function defined in the type in.NET, is defined as:
[0097] TriggerContext, the trigger context definition, is used to store the database connection corresponding to the current data maintenance opportunity and data maintenance process;
[0098] Icollection, the collection container, is used to store the data model instance that needs to execute the data maintenance process at present;
[0099] Trigger[], the complete mapping relationship between the data maintenance opportunity and the data maintenance process of the current obtained data model of this type.
[0100] As shown in the processing flow diagram of the mapping between the data maintenance opportunity and the data maintenance process defined in the extraction object, the data operation driving module can read and process the defined data maintenance opportunity and data maintenance process association relationship from the ORM object model definition through the reflection mechanism of.NET itself, and place it in the defined Trigger structure, specifically: Figure 2 S21: input the type name;
[0101] S22: obtain all method information of the specific type;
[0102] S23: traverse all the read method information;
[0103] S24: read a current untraversed method information from all the method information;
[0104] S25: check whether the method information is modified by the TriggerAttribute custom attribute, if the method information is modified by the TriggerAttribute custom attribute, continue to execute, if the method information is not modified by the TriggerAttribute custom attribute, directly jump to step S28 and continue to execute;
[0105] S26: read the data maintenance mechanism in the custom attribute corresponding to the method together with the above method information, generate a Trigger variable, and add the variable to the 'Trigger collection';
[0106] S27: mark the current obtained method information as read;
[0107] S28: continue to execute the next step;
[0108] S28: Check if the traversal of all method information has been completed. If the traversal of all method information has been completed, continue execution. If the traversal of all method information has not been completed, return to step S24 and continue execution.
[0109] S29: Returns the generated Trigger set, and the processing ends.
[0110] like Figure 3 The diagram illustrates the trigger scenario flow during operation. In the data operation driven module, different data operation timings are combined with specific TriggerContext scenario instances. The Invoke function in the Method class under .NET is used to execute the triggering function, completing the triggering execution of the "data maintenance process," specifically:
[0111] S301: Input the trigger context (TriggerContext), the target object collection (ICollection), and the trigger collection (Trigger[]);
[0112] S302: Obtain the current data maintenance timing by inputting the trigger scenario definition, and obtain the definition of all data maintenance processes to be executed this time based on the obtained data maintenance timing and the input trigger set;
[0113] S303; Traverse the target objects that require data maintenance;
[0114] S304; Get the target object that has not yet been traversed, denoted as target;
[0115] S305: Traverse all data maintenance processes that need to be executed in this current instance;
[0116] S306: Obtain a currently untraversed data maintenance process, denoted as process;
[0117] S307: The data maintenance process is executed by triggering the process;
[0118] S308: Mark the current data maintenance process as traversed;
[0119] S309: Check whether the traversal of all data maintenance processes has been completed. If the traversal of all data maintenance processes has been completed, continue execution. If the traversal of all data maintenance processes has not been completed, return to step S306 and continue execution.
[0120] S310: Mark the currently read target object as having been traversed;
[0121] S311: checking whether the traversal of all target objects is completed, if the traversal of all target objects is completed, the process ends, if the traversal of all target objects is not completed, returning to step S304 and continuing to execute downwards;
[0122] S312: the process ends.
[0123] The application extends the ORM framework, so that the ORM framework has a mechanism for automatically maintaining data integrity, consistency and the like when operating on a data model (data addition, modification, deletion, query), and by reasonably using the mechanism, software maintenance efficiency can be effectively improved, code maintenance cost can be reduced, and subsequent software maintenance and upgrading are facilitated; a trigger is added by using the.NET language, so that the trigger function which should be implemented in the database is implemented.
[0124] The technical means disclosed in the application scheme is not limited to the technical means disclosed in the above embodiments, and also includes technical solutions composed of any combination of the above technical features. It should be pointed out that, for ordinary skilled persons in the art, without departing from the principles of the application, a number of improvements and refinements can be made, and these improvements and refinements are also considered within the protection scope of the application.
Claims
1. A trigger mechanism implementation method based on an ORM framework, characterized in that: The method comprises the following steps: Step S1: defining a data maintenance time through a custom attribute of.NET; Step S2: modifying a data maintenance process through the custom attribute of.NET, and binding an association between a data processing time and the data maintenance process; Step S3: adding reading of a corresponding data maintenance time in a bottom data driving part, and activating the corresponding data maintenance process by using a reflection mechanism of.NET; The data operation driving module can read and process the defined association between the data maintenance time and the data maintenance process from an ORM object model definition by using the reflection mechanism of.NET, and place the association in a defined Trigger structure, specifically as follows: S21: inputting a type name; S22: obtaining all method information of a specific type; S23: traversing all the read method information; S24: reading a method information that has not been traversed from all the method information; S25: checking whether the method information is modified by a TriggerAttribute custom attribute, if the method information is modified by the TriggerAttribute custom attribute, continuing to execute the following steps, if the method information is not modified by the TriggerAttribute custom attribute, directly jumping to step S28 and continuing to execute the following steps; S26: reading a data maintenance mechanism in the custom attribute corresponding to the method together with the method information, generating a Trigger variable, and adding the variable to a 'Trigger collection'; S27: marking the currently obtained method information as read; S28: checking whether the traversal of all the method information is completed, if the traversal of all the method information is completed, continuing to execute the following steps, if the traversal of all the method information is not completed, returning to step S24 and continuing to execute the following steps; S29: returning the generated Trigger collection, and ending the processing.
2. The method of claim 1, wherein the method is implemented based on an ORM framework. In step S1, the data maintenance time includes but is not limited to: data maintenance before adding, data maintenance after adding, data maintenance before updating, data maintenance after updating, data maintenance before deleting, data maintenance after deleting, and data maintenance after querying; wherein 'before adding' is defined as 'BeforeInsert', 'after adding' is defined as 'AfterInsert', 'before updating' is defined as 'BeforeUpdate', 'after updating' is defined as 'AfterUpdate', 'before deleting' is defined as 'BeforeDelete', 'after deleting' is defined as 'AfterDelete', 'after querying' is defined as 'AfterConstruct', in addition, 'None' is added as an initialization value, and 'All' is added as an operation corresponding to all events.
3. The method of claim 1, wherein the method is implemented based on an ORM framework. In step S2, a custom attribute for modifying the data maintenance process is defined, and the custom attribute contains a data maintenance time triggering the data maintenance process.
4. The method of claim 1, wherein the method is implemented based on an ORM framework. In step S2, the input parameters of the data maintenance process are reduced, including the current execution trigger timing 'Timing', the database 'AbstractDao' where the data to be accessed in the trigger process is located, and the boxing operation result 'object []' of the.NET framework when executing the reflection mechanism.
5. The method of claim 1, wherein the method is implemented based on an ORM framework. In step S2, the data maintenance process is defined, and the processing function accompanied by the data operation is defined. The input parameter of the function is 'TriggerContext', and the function has no return value after execution.
6. The method of claim 1, wherein the method is implemented based on an ORM framework. The corresponding mapping relationship between the data maintenance timing and the data maintenance process is defined through the configuration file of the data bottom layer or built into the code by metadata programming.
7. The method of claim 1, wherein the method is implemented based on an ORM framework. In the data operation driving module, different data operation timing combines specific TriggerContext scene instances, uses the Invoke function in the Method class under.NET to execute the trigger function, completes the trigger execution of the "data maintenance process", and specifically: S301: input the trigger scene TriggerContext, the target object set ICollection, and the trigger set Trigger []; S302: define the current data maintenance timing according to the input trigger scene, and obtain all data maintenance process definitions to be executed this time according to the obtained data maintenance timing and the input trigger set; S303: traverse the target object to be maintained; S304: obtain a target object to be executed this time, denoted as target; S305: traverse all data maintenance processes to be executed this time; S306: obtain a data maintenance process to be executed this time, denoted as process; S307: trigger the process to execute the data maintenance process; S308: mark the current data maintenance process as traversed; S309: check whether the traversal of all data maintenance processes is completed. If the traversal of all data maintenance processes is completed, continue to execute the next step; if the traversal of all data maintenance processes is not completed, return to step S306 and continue to execute the next step; S310: mark the current read target object as traversed; S311: check whether the traversal of all target objects is completed. If the traversal of all target objects is completed, the processing is ended; if the traversal of all target objects is not completed, return to step S304 and continue to execute the next step; S312: the processing is ended.
Citation Information
Patent Citations
ORM framework implementation method and system, computer equipment and storage medium
CN111966704A
User-defined attribute-based database access code automatic generation method and system
CN116450110A