From 91aed77ae1f45fb7351e97efafcd22d78bda97f3 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Tue, 6 Aug 2024 14:57:53 +0800 Subject: [PATCH] 1 --- AOP.md | 75 ++++++++++++++++------------------------------------------ 1 file changed, 21 insertions(+), 54 deletions(-) diff --git a/AOP.md b/AOP.md index aec9c07..cf42068 100644 --- a/AOP.md +++ b/AOP.md @@ -1,3 +1,5 @@ +# AOP✨ + FreeSql AOP 已有的功能介绍,未来为会根据用户需求不断增强。 ## 审计命令(如何监视 SQL?) @@ -33,10 +35,10 @@ fsql.Aop.CommandAfter += (s, e) => 实现插入/更新时统一处理某些值,比如某属性的雪花算法值、创建时间值、甚至是业务值。 ```csharp -fsql.Aop.AuditValue += (s, e) => +fsql.Aop.AuditValue += (s, e) => { - if (e.Column.CsType == typeof(long) && - e.Property.GetCustomAttribute(false) != null && + if (e.Column.CsType == typeof(long) && + e.Property.GetCustomAttribute(false) != null && e.Value?.ToString() == "0") e.Value = new Snowflake().GetId(); }; @@ -47,47 +49,25 @@ class Order { //... } ``` -当属性的类型是 long,并且标记了 [Snowflake],并且当前值是 0,那么在插入/更新时它的值将设置为雪花id值。 + +当属性的类型是 long,并且标记了 [Snowflake],并且当前值是 0,那么在插入/更新时它的值将设置为雪花 id 值。 > 说明:SnowflakeAttribute 是使用者您来定义,new Snowflake().GetId() 也是由使用者您来实现 -如果命名规范,可以在 aop 里判断,if (e.Property.Name == "createtime") e.Value = DateTime.Now; +如果命名规范,可以在 aop 里判断,`if (e.Property.Name == "createtime") e.Value = DateTime.Now;` > v3.2.666 可设置 e.ObjectAuditBreak = true 中断对象审计,变相实现每个对象只触发一次 AuditValue 事件 -## 审计命令 - -fsql.Aop.CommandBefore、fsql.Aop.CommandAfter 这两个事件触发所有 SQL 命令的执行前、和执行后。 - -执行后的事件会附带异常信息、耗时信息等。 - -建议在开发模式下开启无参数化模式,new FreeSqlBuilder().UseNoneCommandParameter(true)。 - -> 提示:new FreeSqlBuilder().UseMonitorCommand 也可以审计命令执行前后。 - -```csharp -fsql.Aop.CommandBefore += (s, e) => -{ - //e.Command.CommandText = null; 可拦截命令 -}; - -fsql.Aop.CommandAfter += (s, e) => -{ - if (e.Exception != null) - { - //做一些日志记录的操作。以下为示例。 - Trace.WriteLine($"Message:{e.Exception.Message }\r\nStackTrace:{e.Exception.StackTrace}\r\nCommandText:{e.Command.CommandText}"); - } -}; -``` - ## 审计迁移脚本 FreeSql 自带迁移功能,那么迁移的 SQL 语句长啥样,你可能会好奇。 - 比如创建表时; + - 比如添加字段时; + - 比如修改表名、修改字段名时; + - 又比如字段类型更改之后时; 这些操作在 FreeSql.CodeFirst 实现下基本不需要理会,而且我们只推荐在开发环境使用自动迁移的功能,正式环境可使用其他工具替代此操作。 @@ -116,8 +96,8 @@ fsql.Aop.ConfigEntity += (s, e) => { ```csharp fsql.Aop.ConfigEntityProperty += (s, e) => { - if (e.Property.PropertyType.IsEnum) - e.ModifyResult.MapType = typeof(int); + if (e.Property.PropertyType.IsEnum) + e.ModifyResult.MapType = typeof(int); }; ``` @@ -136,7 +116,7 @@ fsql1.Aop.ConfigEntityProperty += (s, e) => }; ``` -## 自定义实体特性 +### 自定义实体特性 比如项目内已经使用了其它 orm,如 efcore,这样意味着实体中可能存在 [Key],但它与 FreeSql [Column(IsPrimary = true] 不同。 @@ -149,13 +129,13 @@ FreeSql 提供 AOP 自定义特性功能,实现与多个 orm 共同拥有一 > v1.4.0+ 已自动识别 EFCore 实体特性 Key/Required/NotMapped/MaxLength/StringLength/DatabaseGenerated/Table/Column ```csharp -fsql.Aop.ConfigEntity += (s, e) => +fsql.Aop.ConfigEntity += (s, e) => { var attr = e.EntityType.GetCustomAttributes(typeof(MyTableAttribute), false).FirstOrDefault() as MyTableAttribute; if (attr != null) e.ModifyResult.Name = attr.Name; //表名 }; -fsql.Aop.ConfigEntityProperty += (s, e) => +fsql.Aop.ConfigEntityProperty += (s, e) => { var attr = e.Property.GetCustomAttributes(typeof(MyColumnAttribute), false).FirstOrDefault() as MyColumnAttribute; if (attr != null) @@ -177,7 +157,7 @@ class MyTableAttribute : Attribute this.Name = name; } } -class MyColumnAttribute : Attribute +class MyColumnAttribute : Attribute { public string Name { get; } public MyColumnAttribute(string name) @@ -187,14 +167,14 @@ class MyColumnAttribute : Attribute } ``` -## Ado.net 读取拦截 +## Ado .net 读取拦截 ```csharp fsql.Aop.AuditDataReader += (_, e) => { - if (e.DataReader.GetFieldType(e.Index) == typeof(string) && + if (e.DataReader.GetFieldType(e.Index) == typeof(string) && e.Value == DBNull.Value) - e.Value = ""; + e.Value = ""; }; ``` @@ -202,7 +182,7 @@ fsql.Aop.AuditDataReader += (_, e) => FreeSql 内部表达式支持非常丰富,对各大数据库的兼容度也做得很好。 -> 有关表达式支持到的程度,可点击查看详细wiki:[%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%87%BD%E6%95%B0](%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%87%BD%E6%95%B0) +> 有关表达式支持的程度,可参阅:[表达式函数](expression-function.md) 即便如此丰富,也仍然无法满足用户需求,FreeSql 对外开放了自定义表达式解析接口: @@ -216,19 +196,6 @@ fsql.Aop.ParseExpression += (s, e) => 这个解析有点复杂,当 `e.Expression` 很复杂的时候,我们还提供了 `e.FreeParse` 方法,使用它相当于调用 `FreeSql` 内置表达式解析引擎,辅助您进行解析。 -## 修改decimal默认特性 -因为默认decimal只支持decimal(10,2),范围太小,我们可以全局修改decimal类型的支持范围,比如支持decimal(18,6) -```csharp -fsql1.Aop.ConfigEntityProperty += (s, e) => -{ - if (e.Property.PropertyType == typeof(decimal)|| e.Property.PropertyType == typeof(decimal?)) - { - e.ModifyResult.Precision = 18; - e.ModifyResult.Scale = 6; - } -}; -``` - ## 参考资料 - [《数据库事务》](%e4%ba%8b%e5%8a%a1)