One line of code implements the table action log record
Before I introduce a few lessons about my privilege system improvements, this article continues this series of principals, which describes how to implement a single line of code that implements the operational logging of important tables. We know that in many business systems, the data is very sensitive, especially for some of the additions, modifications, deletions and other key operations, if you can support the framework level, with minimal code to achieve important table logging, then it is a very worthy thing to celebrate, It can also provide important log tracking for our customers ' data and even reference for data recovery.
1, the data Access Layer object inheritance relationship
First of all, in order to reduce the duplication of code writing, a reasonable inheritance relationship is necessary, we need to establish a reasonable inheritance relationship on the data access layer, the following is my WinForm development framework inheritance relationship. Each data access object (such as a Itemdetail data access object) inherits an abstract base class Abstractbasedal and a ibasedal base class interface, and it also has its own special business interface, such as Iitemdetail, as shown in the following relationship.
With the above inheritance relationship, we can put regular database important operations (add-deletions) to a higher level to solve this problem without needing to be implemented at each data access layer's business class.
2, operation log record event definition and use
In order to better realize the record of data operation log, we use event mode to trigger the record of the operation log, and the concrete record of the event can be transferred to the external record processing. If the event is externally assigned, then the event record can be triggered at the bottom, and the definition code of the event is defined in the abstract base class, as shown below.
Define event handling for a record action log///</summary>///<param name= "userId" > User id</param>///<param n Ame= "tablename" > Action sheet name </param>///<param name= "Operationtype" > Operation type: Add, modify, delete </param>/// ;p Aram Name= "Note" > Operations detailed record information </param>///<returns></returns> public delegate bool Operationlo
Geventhandler (String userId, String tablename, String operationtype, string note, dbtransaction trans = null); <summary>///class for the data access layer, all database access base classes inherit from this super base class, including Oracle, SQL Server, Sqlite, MYSQL, access, etc.///</summary
> Public abstract class abstractbasedal<t> where t:baseentity, new () {#region constructor protected string dbconfigname = ""; Database configuration name protected string parameterprefix = "@";//database parameterized Access placeholder protected string Safefieldformat = "[{0}]" //prevents field formats with reserved words and keywords with the same name, such as [value] protected string tablename;//The object table name that needs to be initialized protected string Primarykey;//Database primary key field name protected string sortfield;//sort field protected bool isdescending = true;//is descending prot ected string selectedfields = "*";//Selected field, defaults to all (*) public event Operationlogeventhandler Onoperationlo g;//defines the event handling for an action record .... .........