EFCore 5 新特性 SaveChangesInterceptor

Intro

之前 EF Core 5 还没正式发布的时候有发布过一篇关于 SaveChangesEvents 的文章,有需要看可以移步到 efcore 新特性 SaveChanges Events,在后面的版本中又加入了 Interceptor 的支持,可以更方便的实现 SaveChanges 事件的复用, 今天主要介绍一下通过 SaveChangesInterceptor 来实现日志审计

SaveChangesInterceptor

源码实现:

public interface ISaveChangesInterceptor : IInterceptor
{
/// <summary>
/// Called at the start of <see cref="M:DbContext.SaveChanges" />.
/// </summary>
/// <param name="eventData"> Contextual information about the <see cref="DbContext" /> being used. </param>
/// <param name="result">
/// Represents the current result if one exists.
/// This value will have <see cref="InterceptionResult{Int32}.HasResult" /> set to <see langword="true" /> if some previous
/// interceptor suppressed execution by calling <see cref="InterceptionResult{Int32}.SuppressWithResult" />.
/// This value is typically used as the return value for the implementation of this method.
/// </param>
/// <returns>
/// If <see cref="InterceptionResult{Int32}.HasResult" /> is false, the EF will continue as normal.
/// If <see cref="InterceptionResult{Int32}.HasResult" /> is true, then EF will suppress the operation it
/// was about to perform and use <see cref="InterceptionResult{Int32}.Result" /> instead.
/// A normal implementation of this method for any interceptor that is not attempting to change the result
/// is to return the <paramref name="result" /> value passed in.
/// </returns>
InterceptionResult<int> SavingChanges(
[NotNull] DbContextEventData eventData,
InterceptionResult<int> result); /// <summary>
/// <para>
/// Called at the end of <see cref="M:DbContext.SaveChanges" />.
/// </para>
/// <para>
/// This method is still called if an interceptor suppressed creation of a command in <see cref="SavingChanges" />.
/// In this case, <paramref name="result" /> is the result returned by <see cref="SavingChanges" />.
/// </para>
/// </summary>
/// <param name="eventData"> Contextual information about the <see cref="DbContext" /> being used. </param>
/// <param name="result">
/// The result of the call to <see cref="M:DbContext.SaveChanges" />.
/// This value is typically used as the return value for the implementation of this method.
/// </param>
/// <returns>
/// The result that EF will use.
/// A normal implementation of this method for any interceptor that is not attempting to change the result
/// is to return the <paramref name="result" /> value passed in.
/// </returns>
int SavedChanges(
[NotNull] SaveChangesCompletedEventData eventData,
int result); /// <summary>
/// Called when an exception has been thrown in <see cref="M:DbContext.SaveChanges" />.
/// </summary>
/// <param name="eventData"> Contextual information about the failure. </param>
void SaveChangesFailed(
[NotNull] DbContextErrorEventData eventData); /// <summary>
/// Called at the start of <see cref="M:DbContext.SaveChangesAsync" />.
/// </summary>
/// <param name="eventData"> Contextual information about the <see cref="DbContext" /> being used. </param>
/// <param name="result">
/// Represents the current result if one exists.
/// This value will have <see cref="InterceptionResult{Int32}.HasResult" /> set to <see langword="true" /> if some previous
/// interceptor suppressed execution by calling <see cref="InterceptionResult{Int32}.SuppressWithResult" />.
/// This value is typically used as the return value for the implementation of this method.
/// </param>
/// <param name="cancellationToken"> The cancellation token. </param>
/// <returns>
/// If <see cref="InterceptionResult{Int32}.HasResult" /> is false, the EF will continue as normal.
/// If <see cref="InterceptionResult{Int32}.HasResult" /> is true, then EF will suppress the operation it
/// was about to perform and use <see cref="InterceptionResult{Int32}.Result" /> instead.
/// A normal implementation of this method for any interceptor that is not attempting to change the result
/// is to return the <paramref name="result" /> value passed in.
/// </returns>
ValueTask<InterceptionResult<int>> SavingChangesAsync(
[NotNull] DbContextEventData eventData,
InterceptionResult<int> result,
CancellationToken cancellationToken = default); /// <summary>
/// <para>
/// Called at the end of <see cref="M:DbContext.SaveChangesAsync" />.
/// </para>
/// <para>
/// This method is still called if an interceptor suppressed creation of a command in <see cref="SavingChangesAsync" />.
/// In this case, <paramref name="result" /> is the result returned by <see cref="SavingChangesAsync" />.
/// </para>
/// </summary>
/// <param name="eventData"> Contextual information about the <see cref="DbContext" /> being used. </param>
/// <param name="result">
/// The result of the call to <see cref="M:DbContext.SaveChangesAsync" />.
/// This value is typically used as the return value for the implementation of this method.
/// </param>
/// <param name="cancellationToken"> The cancellation token. </param>
/// <returns>
/// The result that EF will use.
/// A normal implementation of this method for any interceptor that is not attempting to change the result
/// is to return the <paramref name="result" /> value passed in.
/// </returns>
ValueTask<int> SavedChangesAsync(
[NotNull] SaveChangesCompletedEventData eventData,
int result,
CancellationToken cancellationToken = default); /// <summary>
/// Called when an exception has been thrown in <see cref="M:DbContext.SaveChangesAsync" />.
/// </summary>
/// <param name="eventData"> Contextual information about the failure. </param>
/// <param name="cancellationToken"> The cancellation token. </param>
/// <returns> A <see cref="Task" /> representing the asynchronous operation. </returns>
Task SaveChangesFailedAsync(
[NotNull] DbContextErrorEventData eventData,
CancellationToken cancellationToken = default);
}

为了比较方便的实现自己需要的 Interceptor,微软还提供了一个 SaveChangesInterceptor 抽象类,这样只需要继承于这个类,重写自己需要的方法即可,实现比较简单,就是实现了 ISaveChangesInterceptor 接口,然后接口的实现都是空的虚方法

源码链接:https://github.com/dotnet/efcore/blob/v5.0.0/src/EFCore/Diagnostics/SaveChangesInterceptor.cs

使用 SaveChangesInterceptor 实现自动审计

简单写了一个测试的审计拦截器

public class AuditInterceptor : SaveChangesInterceptor
{
public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)
{
var changesList = new List<CompareModel>(); foreach (var entry in
eventData.Context.ChangeTracker.Entries<Post>())
{
if (entry.State == EntityState.Added)
{
changesList.Add(new CompareModel()
{
OriginalValue = null,
NewValue = entry.CurrentValues.ToObject(),
});
}
else if (entry.State == EntityState.Deleted)
{
changesList.Add(new CompareModel()
{
OriginalValue = entry.OriginalValues.ToObject(),
NewValue = null,
});
}
else if (entry.State == EntityState.Modified)
{
changesList.Add(new CompareModel()
{
OriginalValue = entry.OriginalValues.ToObject(),
NewValue = entry.CurrentValues.ToObject(),
});
}
Console.WriteLine($"change list:{changesList.ToJson()}");
}
return base.SavingChanges(eventData, result);
} public override int SavedChanges(SaveChangesCompletedEventData eventData, int result)
{
Console.WriteLine($"changes:{eventData.EntitiesSavedCount}");
return base.SavedChanges(eventData, result);
} private class CompareModel
{
public object OriginalValue { get; set; } public object NewValue { get; set; }
}
}

实际应用的话还需要根据自己的场景做一些修改和测试

测试 DbContext 示例,这里使用了一个简单的 InMemory 做了一个测试:

public class TestDbContext : DbContext
{
public TestDbContext(DbContextOptions<TestDbContext> dbContextOptions) : base(dbContextOptions)
{
} public DbSet<Post> Posts { get; set; }
} public class Post
{
[Key]
public int Id { get; set; } public string Author { get; set; } public string Title { get; set; } public DateTime PostedAt { get; set; }
}

测试代码:

var services = new ServiceCollection();
services.AddDbContext<TestDbContext>(options =>
{
options.UseInMemoryDatabase("Tests")
//.LogTo(Console.WriteLine) // EF Core 5 中新的更简洁的日志记录方式
.AddInterceptors(new AuditInterceptor())
;
});
using var provider = services.BuildServiceProvider();
using (var scope = provider.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<TestDbContext>();
dbContext.Posts.Add(new Post() { Id = 1, Author = "test", Title = "test", PostedAt = DateTime.UtcNow });
dbContext.SaveChanges(); var post = dbContext.Posts.Find(1);
post.Author = "test2";
dbContext.SaveChanges(); dbContext.Posts.Remove(post);
dbContext.SaveChanges();
}

输出结果(输出结果的如果数据为 null 就会被忽略掉,所以对于新增的数据实际是没有原始值的,对于删除的数据没有新的值):

More

EF Core 5 还有很多新的特性,有需要的小伙伴可以看一下官方文档的介绍~

上述源码可以在 Github 上获取 https://github.com/WeihanLi/SamplesInPractice/blob/master/EF5Samples/SaveChangesInterceptorTest.cs

Reference

EFCore 5 新特性 `SaveChangesInterceptor`的更多相关文章

  1. EFCore 5 新特性 —— Savepoints

    EFCore 5 中的 Savepoints Intro EFCore 5中引入了一个新特性,叫做 Savepoints,主要是事务中使用,个人感觉有点类似于 Windows 上的系统还原点,如果事务 ...

  2. efcore 新特性 SaveChanges Events

    efcore 新特性 SaveChanges Events Intro 昨天早上看到之前关注的一个 efcore 的 issue 被 closed ,于是看了一眼, ef core 新合并了一个 PR ...

  3. SQL Server 2014 新特性——内存数据库

    SQL Server 2014 新特性——内存数据库 目录 SQL Server 2014 新特性——内存数据库 简介: 设计目的和原因: 专业名词 In-Memory OLTP不同之处 内存优化表 ...

  4. ElasticSearch 5学习(10)——结构化查询(包括新特性)

    之前我们所有的查询都属于命令行查询,但是不利于复杂的查询,而且一般在项目开发中不使用命令行查询方式,只有在调试测试时使用简单命令行查询,但是,如果想要善用搜索,我们必须使用请求体查询(request ...

  5. [干货来袭]C#6.0新特性

    微软昨天发布了新的VS 2015 ..随之而来的还有很多很多东西... .NET新版本 ASP.NET新版本...等等..太多..实在没消化.. 分享一下也是昨天发布的新的C#6.0的部分新特性吧.. ...

  6. CSS3新特性应用之结构与布局

    一.自适应内部元素 利用width的新特性min-content实现 width新特性值介绍: fill-available,自动填充盒子模型中剩余的宽度,包含margin.padding.borde ...

  7. 【译】Meteor 新手教程:在排行榜上添加新特性

    原文:http://danneu.com/posts/6-meteor-tutorial-for-fellow-noobs-adding-features-to-the-leaderboard-dem ...

  8. 跨平台的 .NET 运行环境 Mono 3.2 新特性

    Mono 3.2 发布了,对 Mono 3.0 和 2.10 版本的支持不再继续,而且这两个分支也不再提供 bug 修复更新. Mono 3.2 主要新特性: LLVM 更新到 3.2 版本,带来更多 ...

  9. Atitit opencv版本新特性attilax总结

    Atitit opencv版本新特性attilax总结 1.1. :OpenCV 3.0 发布,史上功能最全,速度最快的版1 1.2. 应用领域2 1.3. OPENCV2.4.3改进 2.4.2就有 ...

随机推荐

  1. Croppie -一个Javascript图像Croppie

    下载 Croppie -一个Javascript图像CroppieCroppie -一个Javascript图像Croppie 安装 凉棚:凉棚安装作物 Npm: Npm安装作物 下载: croppi ...

  2. K8S-ETCD数据库备份与恢复

    kubernetes使用etcd数据库实时存储集群中的数据,安全起见,一定要备份 需要指定使用etcdctl的版本 etcd数据库备份是使用数控快照的方式进行备份的,备份后的新数据不会保留,后面创建的 ...

  3. 更简易的机器学习-pycaret的安装和环境初始化

    1.安装 pip install pycaret 在谷歌colab中还要运行: from pycaret.utils import enable_colab enable_colab() 2.获取数据 ...

  4. devops-持续集成管理之SonarQube

    1. devops-持续集成管理之SonarQube  1) 代码质量七宗罪 编码规范:是否遵守了编码规范,遵循了最佳实践. 潜在的BUG:可能在最坏情况下出现问题的代码,以及存在安全漏洞的代码. 文 ...

  5. 高度集成智能家居物联网网关WiFi通信应用的无线路由模块:模小块成长记

    大家好,我叫模小块,代号L107模块,出生在BOJINGnet大家庭里,我在物联网网关里不可或缺,或许业内专业人士和物联网工程师知道我的存在.别看我体积小(40mm25mm3mm),贴片式邮票孔接口( ...

  6. swoole热启动

    通过扫描指定的要扫描的目录,把所有文件找出来,分别md5 连接字符串,最后再md5返回 启动定时器,扫描,当前的加密值和以前一样不管,否则就重启服务,把当前赋值给旧值 . httpServer.php ...

  7. 【应用服务 App Service】如何移除App Service Response Header中包含的服务器敏感信息

    问题描述 有些情况下,当应用部署到App Service上后,在有些Response Header中,可以看见关于服务器的一些信息,这样会导致隐藏的安全问题,所以可以在web.config中移除某些关 ...

  8. 微服务nacos服务注册与发现

    一,以上一篇为基础 微服务从nacos配置中心获得配置信息 给service1, service2添加依赖 <dependency> <groupId>com.alibaba. ...

  9. Deployer 的安装与配置

    Deployer 是一个 composer 包,你可以选择以 phar 包的形式,或者以 composer 全局安装来使用它,这里只讲后者,毕竟这是推荐大家使用的方式,升级也会方便很多: $ comp ...

  10. elasticsearch mysql配置

    1,开启bin-log 2,binglog_foramt格式必须为row 3,配置server_id为1001 4,binlog-row-image 必须为full log-bin=mysql-bin ...