返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
ORM

Implementing “Triggers” and Soft Delete in FreeSQL

Introduction Recently I started a new project, tech stack AspNetCore + FreeSQL This ORM is really great to use, and the documentation is quite complete too. Here I’ll record two feature implementations related to “triggers” Recording update time when modifying an entity Model code All of my models are derived from this ModelBase, which comes with three property fields public abstract class ModelBase : ISoftDelete { public DateTime CreatedT

曦远 (DealiAxy)约 8 分钟
目录4 个章节

Introduction

Recently I started a new project, tech stack AspNetCore + FreeSQL

This ORM is really great to use, and the documentation is quite complete too. Here I’ll record two feature implementations related to “triggers”

Recording update time when modifying an entity

Model code

All of my models are derived from this ModelBase, which comes with three property fields

public abstract class ModelBase : ISoftDelete {
    public DateTime CreatedTime { get; set; } = DateTime.Now;
    public DateTime ModifiedTime { get; set; } = DateTime.Now;
    public bool IsDeleted { get; set; } = false;
}

Now I need to automatically update the ModifiedTime field whenever each entity is modified

You can use the “audit property values” feature in the AOP provided by FreeSQL

According to the official introduction: this feature can uniformly handle certain values on insert/update, such as a snowflake-algorithm value for a property, a created-time value, or even a business value.

Configure it when creating the FreeSQL instance

Check the entity type; if it is a ModelBase type, then set the ModifiedTime field to the current modification time

// 实体变化事件监听
freeSql.Aop.AuditValue += (sender, args) => {
    if (args.Object is ModelBase modelBase) {
        modelBase.ModifiedTime = DateTime.Now;
    }
};

Besides that, there’s another feature for monitoring entity modifications. I used this one at first, but found that it only executes after the entity has already been modified and saved, so it can’t implement the feature of recording the update time. Still, it’s quite useful for auditing, so I’ll note it down~

fsql.Aop.CurdAfter += (s, e) => {
    Console.WriteLine($"ManagedThreadId:{Thread.CurrentThread.ManagedThreadId};"+
    $" FullName:{e.EntityType.FullName} ElapsedMilliseconds:{e.ElapsedMilliseconds}ms, {e.Sql}");
};

Implementing soft delete

Current designs generally use soft delete to improve fault tolerance, so that data can still be recovered after accidental deletion.

The DjangoStarter framework I previously wrapped implemented soft delete itself (by overriding DjangoORM’s objects manager)

Coming to FreeSQL, according to the official documentation, you can implement soft delete directly using the filter feature.

First define an interface

public interface ISoftDelete {
    bool IsDeleted { get; set; }
}

Actually, using the ModelBase above would also work

But I saw the official docs used it this way, so I followed along~ (interfaces are more generic; some entities may not need created-time and modified-time fields)

Then configure it when creating the FreeSQL instance

freeSql.GlobalFilter.Apply<ISoftDelete>("IsDeleted", a => a.IsDeleted == false);

This way, when reading data, items whose IsDeleted field is true are automatically filtered out

When deleting, don’t execute repo.Delete directly; instead set the IsDeleted field to true

Of course, you can also intercept all delete operations the same way as the AOP above.

References

_EOF_

Implementing “Triggers” and Soft Delete in FreeSQL

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

没有学会正确审视自己,不能说是真正的成长

前言 某一天在洗澡的时候,我突然思考起来,最近几年,最大的收获是什么? 从专业技术知识想到了为人处世,想来想去,觉得最大的收获,大概就是终于初步学会审视自己了。 学会批判自己 批判别人不算本事,因为我们总是可以很容易的找出别人的缺点,就算他真的做的很完美无可挑剔了,还是能够从鸡蛋里挑骨头,然而反过来批判自己却不是每个人都能做得到的,认识到自己的错误是很难的一件事情,更何况是正确地审视自己。 曾子曰

2022-11-07 · 2017 · 约 1 分钟

告别单调,Django后台主页改造

前言 之前我做了个Django的项目,为了让管理后台更加美观,我对Django(应该说是SimpleUI的)默认的Admin后台主页进行改造,具体可以看这篇文章:项目完成 - 基于Django3.x版本 - 开发部署小结 之前的两篇关于Django3.x开发部署小结的文章介绍的都是数据库、接口、性能方面的,看到有朋友留言对后台主页的改造表示有兴趣,所以就写一篇来介绍一下~ 分析 改造基于我定制的

2022-06-28 · Django · 约 1 分钟

GitLab的安装、配置、使用

前言 上周去参与“中国数字经济创新发展大会”了,然后又忙新项目的事情,博客又有一段时间没有更新,今天周一事情比较少,立刻开始写文,最近有挺多值得记录的东西~ 进入正文,最近我们搭了个Gitlab,并且把正在开发的项目代码都迁移到自建的Gitlab上,整个过程还是比较流畅的~~(就是有点费内存~~ PS:立即下单给这台服务器买了四条16G内存组四通道~ 在此之前,我们团队一直使用Gitee企业版作为

2022-07-21 · DevOps · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…