Skip to content
果糖网 edited this page Jul 21, 2024 · 2 revisions

The event will be entered after the Sql execution, and the event can get the pre-change record and post-change record, execution time and other parameters (can monitor table changes).This function is similar to EF Core ChangeTracker

1.1 supports batch operation

db.Aop.OnDiffLogEvent = it =>
{
// Records before an operation include: Field Description Column Value Table name Table description
var editBeforeData = it.BeforeData; // Before is set to null, because it has not been inserted into the library
// Records after an operation include: Field Description Column Value Table name Table description
var editAfterData = it.AfterData;
var sql = it.Sql;
var parameter = it.Parameters;
var data = it.BusinessData; // This side will display the object you passed in
var time = it.Time;
var  diffType=it.DiffType; //enum insert, update, and delete

//Write logic
};


db.Insertable(new Student() { Name = "beforeName" })
.EnableDiffLogEvent() // Notice The enable log must be added
.ExecuteReturnIdentity();

db.Updateable<Student>(new Student()
{
Id = id,
CreateTime = DateTime.Now,
Name = "afterName",
SchoolId = 2
})
EnableDiffLogEvent(new {title = "update Student", Modular = 1, Operator = "admin"}) // Enables and transmits service object parameters
.ExecuteCommand();

db.Deleteable<Student>(id)
EnableDiffLogEvent(" Delete student ") // Enables the passstring service parameter
.ExecuteCommand();
Only the differences are captured

1.2 Batch Configuration

// Register when the program starts
StaticConfig.CompleteInsertableFunc=
StaticConfig.CompleteUpdateableFunc=
StaticConfig.Com pleteDeleteableFunc = it = > / / it is concrete object Updateable < T > is an object, etc
{

// There may be multiple methods for reflection, so GetMethods().Where is required
var method = it.GetType().GetMethod("EnableDiffLogEvent");
method.Invoke(it,new object[] { null });

// Tips:
// You can define an interface as long as the interface takes this logic
//if(db.GetType().GenericTypeArguments[0].GetInterfaces().Any(it=>it==typeof(IDiff))
// You can write if according to the type
//if(x.GetType().GenericTypeArguments[0] = typeof(Order)) {   }
};

// After registration
//Insertable, Updateable, and Deleteable will automatically call EnableDiffLogEvent().
// No need to write one by one
Clone this wiki locally