From 5cd585afe9fa045cfa961e0194f75c9e57c1de51 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Fri, 24 May 2019 17:42:03 +0800 Subject: [PATCH] update --- AOP.md | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++ DbContext.md | 1 + Repository.md | 1 + _Sidebar.md | 1 + 事务.md | 1 + 分区分表.md | 1 + 安装.md | 1 + 工作单元.md | 1 + 性能.md | 3 +- 更新日志.md | 1 + 读写分离.md | 3 +- 过滤器.md | 1 + 12 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 AOP.md diff --git a/AOP.md b/AOP.md new file mode 100644 index 0000000..3fb276e --- /dev/null +++ b/AOP.md @@ -0,0 +1,121 @@ +FreeSql AOP 已有的功能介绍,未来为会根据用户需求不断增强。 + +## 审计 CRUD + +马云说过,996是修福报。对于多数程序员来说,加班是好事。。。起码不是闲人,不会下岗。 + +当如果因为某个 sql 骚操作耗时很高,没有一个相关的审计功能,排查起来可以说无从下手,福报与你紧紧相随(哈哈)。 + +FreeSql 支持简单的类似功能: + +```csharp +fsql.Aop.CurdAfter = (s, e) => { + if (e.ElapsedMilliseconds > 200) { + //记录日志 + //发送短信给负责人 + } +}; +``` + +是的,只需要一个事件,就可以对全局起到作用。 + +除了 CurdAfter,还有一个 CurdBefore (在执行 sql 之前触发)。 + +## 审计迁移脚本 + +FreeSql 自带迁移功能,那么迁移的 SQL 语句长啥样,你可能会好奇。 + +- 比如创建表时; + +- 比如添加字段时; + +- 比如修改表名、修改字段名时; + +- 又比如字段类型更改之后时; + +这些操作在 FreeSql.CodeFirst 实现下基本不需要理会,而且我们只推荐在开发环境使用自动迁移的功能,正式环境可使用其他工具替代此操作。 + +但我们仍然可能需要对项目做完整的日志记录。 + +fsql.Aop.SyncStructureBefore、fsql.Aop.SyncStructureAfter 这两个事件将排上用场。 + +## 自定义实体特性 + +比如项目内已经使用了其它 orm,如 efcore,这样意味着实体中可能存在 [Key],但它与 FreeSql [Column(IsPrimary = true] 不同。 + +Q: FreeSql 实体特性为啥这么别扭? + +A: 为了考虑一致性用法,全部封装在 ColumnAttribute 下,这样用户使用起来,不用到处 using 或者 回忆特性应该用哪个名字,如自增 [Column(IsIdentity = true)] 即可。 + +FreeSql 提供 AOP 自定义特性功能,实现与多个 orm 共同拥有一套实体特性,可避免重复定义特性。 + +```csharp +fsql.Aop.ConfigEntity = (s, e) => { + var attr = e.EntityType.GetCustomAttributes( + typeof(System.ComponentModel.DataAnnotations.Schema.TableAttribute), false).FirstOrDefault() + as System.ComponentModel.DataAnnotations.Schema.TableAttribute; + if (attr != null) + e.ModifyResult.Name = attr.Name; //表名 +}; + +fsql.Aop.ConfigEntityProperty = (s, e) => { + if (e.Property.GetCustomAttributes( + typeof(System.ComponentModel.DataAnnotations.KeyAttribute), false).Any()) + e.ModifyResult.IsPrimary = true; //主键 +}; +``` + +就这样,FreeSql 的实体特性就可以和 EFCore 那样设定了。其他自增、乐观锁等,依葫芦画瓢便是。 + +## 自定义表达式 + +FreeSql 内部表达式支持非常丰富,对各大数据库的兼容度也做得很好。 + +> 有关表达式支持到的程度,可点击查看详细wiki:[https://github.com/2881099/FreeSql/wiki/%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%87%BD%E6%95%B0](https://github.com/2881099/FreeSql/wiki/%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%87%BD%E6%95%B0) + +即便如此丰富,也仍然无法满足用户需求,FreeSql 对外开放了自定义表达式解析接口: + +```csharp +fsql.Aop.ParseExpression = (s, e) => { + if (e.Expression.NodeType == Call && e.Expression.Name == "get_Item") + e.Result = "1111"; +}; +``` + +这个解析有点复杂,当 e.Expression 很复杂的时候,我们还提供了 e.FreeParse 方法,使用它相当于调用 FreeSql 内置表达式解析引擎,辅助您进行解析。 + +## Aop.Where + +FreeSql 提供的 ISelect、IDelete、IUpdate 三大对象,都可以使用 .Where(lambda) 操作,也可以是 .Where(sql) 操作。 + +Aop.Where 的定位是可拦截 Where 条件。 + +```csharp +fsql.Aop.Where = (s, e) => { + if (e.Parameter[0]?.ToString() == "1") + e.IsCancel = true; +}; +``` + +## Aop.ToList + +监控 ToList 返回的的数据,用于拦截重新装饰。 + +所有通过 FreeSql.Select 查询返回的时候,都可以在这个事件上进行重新装饰。 + +## 参考资料 + +- [《数据库事务》](https://github.com/2881099/FreeSql/wiki/%e4%ba%8b%e5%8a%a1) +- [《性能》](https://github.com/2881099/FreeSql/wiki/%e6%80%a7%e8%83%bd) +* [《CodeFirst模式开发介绍》](https://github.com/2881099/FreeSql/wiki/CodeFirst) + * [《CodeFirst模式之一:实体特性》](https://github.com/2881099/FreeSql/wiki/%e5%ae%9e%e4%bd%93%e7%89%b9%e6%80%a7) + * [《CodeFirst模式之二:外部配置实体》](https://github.com/2881099/FreeSql/wiki/FluentApi) + * [《CodeFirst模式之三:自定义特性》](https://github.com/2881099/FreeSql/wiki/%e8%87%aa%e5%ae%9a%e4%b9%89%e7%89%b9%e6%80%a7) + * [《CodeFirst模式之四:类型映射》](https://github.com/2881099/FreeSql/wiki/%e7%b1%bb%e5%9e%8b%e6%98%a0%e5%b0%84) + * [《CodeFirst模式之五:迁移结构》](https://github.com/2881099/FreeSql/wiki/CodeFirst#%e8%bf%81%e7%a7%bb%e7%bb%93%e6%9e%84) +- [《DbFirst模式开发介绍》](https://github.com/2881099/FreeSql/wiki/DbFirst) + - [《DbFirst模式之一:使用模板生成器》](https://github.com/2881099/FreeSql/wiki/DbFirst#%e7%94%9f%e6%88%90%e5%99%a8) +- [《优化之:缓存之路》](https://github.com/2881099/FreeSql/wiki/%e7%bc%93%e5%ad%98) +- [《分区、分表、分库》](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e5%8c%ba%e5%88%86%e8%a1%a8) +- [《过滤器、全局过滤器》](https://github.com/2881099/FreeSql/wiki/%e8%bf%87%e6%bb%a4%e5%99%a8) +- [《租户》](https://github.com/2881099/FreeSql/wiki/%e7%a7%9f%e6%88%b7) \ No newline at end of file diff --git a/DbContext.md b/DbContext.md index e51ad9a..1d0778c 100644 --- a/DbContext.md +++ b/DbContext.md @@ -161,4 +161,5 @@ Guid Id 的情况下,执行三次命令:前两次插入合并执行,update - [《分区、分表、分库》](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e5%8c%ba%e5%88%86%e8%a1%a8) - [《租户》](https://github.com/2881099/FreeSql/wiki/%e7%a7%9f%e6%88%b7) - [《过滤器、全局过滤器》](https://github.com/2881099/FreeSql/wiki/%e8%bf%87%e6%bb%a4%e5%99%a8) +- [《AOP》](https://github.com/2881099/FreeSql/wiki/AOP) - [《UnitOfWork》](https://github.com/2881099/FreeSql/wiki/%e5%b7%a5%e4%bd%9c%e5%8d%95%e5%85%83) diff --git a/Repository.md b/Repository.md index 00c5078..8349241 100644 --- a/Repository.md +++ b/Repository.md @@ -149,5 +149,6 @@ using (var uow = fsql.CreateUnitOfWork()) { - [《分区、分表、分库》](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e5%8c%ba%e5%88%86%e8%a1%a8) - [《租户》](https://github.com/2881099/FreeSql/wiki/%e7%a7%9f%e6%88%b7) - [《过滤器、全局过滤器》](https://github.com/2881099/FreeSql/wiki/%e8%bf%87%e6%bb%a4%e5%99%a8) +- [《AOP》](https://github.com/2881099/FreeSql/wiki/AOP) - [《UnitOfWork》](https://github.com/2881099/FreeSql/wiki/%e5%b7%a5%e4%bd%9c%e5%8d%95%e5%85%83) - [《DbContext》](https://github.com/2881099/FreeSql/wiki/DbContext) \ No newline at end of file diff --git a/_Sidebar.md b/_Sidebar.md index f97202d..e8bf821 100644 --- a/_Sidebar.md +++ b/_Sidebar.md @@ -41,6 +41,7 @@ * [日期/时间](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0#%e6%97%a5%e6%9c%9f) * [其他](https://github.com/2881099/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%e5%85%a8%e8%a7%88) * [事务](https://github.com/2881099/FreeSql/wiki/%e4%ba%8b%e5%8a%a1) +* [AOP](https://github.com/2881099/FreeSql/wiki/AOP) * [读写分离](https://github.com/2881099/FreeSql/wiki/%e8%af%bb%e5%86%99%e5%88%86%e7%a6%bb) * [分区分表](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e5%8c%ba%e5%88%86%e8%a1%a8) * [租户](https://github.com/2881099/FreeSql/wiki/%e7%a7%9f%e6%88%b7) diff --git a/事务.md b/事务.md index 5973860..a87a676 100644 --- a/事务.md +++ b/事务.md @@ -98,6 +98,7 @@ using (var ctx = fsql.CreateDbContext()) { - [《优化之:缓存之路》](https://github.com/2881099/FreeSql/wiki/%e7%bc%93%e5%ad%98) - [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository) - [《过滤器、全局过滤器》](https://github.com/2881099/FreeSql/wiki/%e8%bf%87%e6%bb%a4%e5%99%a8) +- [《AOP》](https://github.com/2881099/FreeSql/wiki/AOP) - [《UnitOfWork》](https://github.com/2881099/FreeSql/wiki/%e5%b7%a5%e4%bd%9c%e5%8d%95%e5%85%83) - [《学习FreeSql之一:添加数据》](https://github.com/2881099/FreeSql/wiki/%e6%b7%bb%e5%8a%a0) - [《学习FreeSql之二:删除数据》](https://github.com/2881099/FreeSql/wiki/%e5%88%a0%e9%99%a4) diff --git a/分区分表.md b/分区分表.md index efbf7a7..59f1050 100644 --- a/分区分表.md +++ b/分区分表.md @@ -67,6 +67,7 @@ fsql.GetGuidRepository().Select.FromRepository(logRepository) - [《优化之:缓存之路》](https://github.com/2881099/FreeSql/wiki/%e7%bc%93%e5%ad%98) - [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository) - [《过滤器、全局过滤器》](https://github.com/2881099/FreeSql/wiki/%e8%bf%87%e6%bb%a4%e5%99%a8) +- [《AOP》](https://github.com/2881099/FreeSql/wiki/AOP) - [《UnitOfWork》](https://github.com/2881099/FreeSql/wiki/%e5%b7%a5%e4%bd%9c%e5%8d%95%e5%85%83) - [《学习FreeSql之一:添加数据》](https://github.com/2881099/FreeSql/wiki/%e6%b7%bb%e5%8a%a0) - [《学习FreeSql之二:删除数据》](https://github.com/2881099/FreeSql/wiki/%e5%88%a0%e9%99%a4) diff --git a/安装.md b/安装.md index 1135b17..3a35b57 100644 --- a/安装.md +++ b/安装.md @@ -43,3 +43,4 @@ FreeSql 除了支持基本的增删查改功能外,还支持基于现有数据 - [《优化之:延时加载》](https://github.com/2881099/FreeSql/wiki/%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bd) - [《优化之:贪婪加载》](https://github.com/2881099/FreeSql/wiki/%e8%b4%aa%e5%a9%aa%e5%8a%a0%e8%bd%bd) - [《Expression 表达式函数》](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0) +- [《AOP》](https://github.com/2881099/FreeSql/wiki/AOP) \ No newline at end of file diff --git a/工作单元.md b/工作单元.md index df1d58e..fc98be8 100644 --- a/工作单元.md +++ b/工作单元.md @@ -36,4 +36,5 @@ using (var uow = fsql.CreateUnitOfWork()) { - [《分区、分表、分库》](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e5%8c%ba%e5%88%86%e8%a1%a8) - [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository) - [《过滤器、全局过滤器》](https://github.com/2881099/FreeSql/wiki/%e8%bf%87%e6%bb%a4%e5%99%a8) +- [《AOP》](https://github.com/2881099/FreeSql/wiki/AOP) - [《DbContext》](https://github.com/2881099/FreeSql/wiki/DbContext) \ No newline at end of file diff --git a/性能.md b/性能.md index 58cbbf5..e42bd0e 100644 --- a/性能.md +++ b/性能.md @@ -141,4 +141,5 @@ public void QueryList() { - [《优化之:缓存之路》](https://github.com/2881099/FreeSql/wiki/%e7%bc%93%e5%ad%98) - [《优化之:延时加载》](https://github.com/2881099/FreeSql/wiki/%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bd) - [《优化之:贪婪加载》](https://github.com/2881099/FreeSql/wiki/%e8%b4%aa%e5%a9%aa%e5%8a%a0%e8%bd%bd) -- [《Expression 表达式函数》](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0) \ No newline at end of file +- [《Expression 表达式函数》](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0) +- [《AOP》](https://github.com/2881099/FreeSql/wiki/AOP) \ No newline at end of file diff --git a/更新日志.md b/更新日志.md index f51ce78..61cd4f9 100644 --- a/更新日志.md +++ b/更新日志.md @@ -6,6 +6,7 @@ - 补充 IUpdate.Set(a => a.Click == 10),简化 Set 更新单个字段表达式; - 优化 延时导航属性的错误提醒,当无法匹配错误,转到重写类 get 时抛出(实现延时导航属性,与普通导航一起使用); - 优化 ICodeFirst.SyncStructure 错误提示,当使用不可迁移实体时; +- 优化 实体数据属性 DbDefaultValue 处理; ## v0.5.21 diff --git a/读写分离.md b/读写分离.md index 0bd1782..8e35e68 100644 --- a/读写分离.md +++ b/读写分离.md @@ -42,4 +42,5 @@ select.Master().WhereId(a => a.Id == 1).ToOne(); //强制读【主库】 - [《优化之:缓存之路》](https://github.com/2881099/FreeSql/wiki/%e7%bc%93%e5%ad%98) - [《分区、分表、分库》](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e5%8c%ba%e5%88%86%e8%a1%a8) - [《过滤器、全局过滤器》](https://github.com/2881099/FreeSql/wiki/%e8%bf%87%e6%bb%a4%e5%99%a8) -- [《租户》](https://github.com/2881099/FreeSql/wiki/%e7%a7%9f%e6%88%b7) \ No newline at end of file +- [《租户》](https://github.com/2881099/FreeSql/wiki/%e7%a7%9f%e6%88%b7) +- [《AOP》](https://github.com/2881099/FreeSql/wiki/AOP) \ No newline at end of file diff --git a/过滤器.md b/过滤器.md index ac4e8f3..4838225 100644 --- a/过滤器.md +++ b/过滤器.md @@ -121,6 +121,7 @@ repos1.Select.ToSql() - [《租户》](https://github.com/2881099/FreeSql/wiki/%e7%a7%9f%e6%88%b7) - [《读写分离》](https://github.com/2881099/FreeSql/wiki/%e8%af%bb%e5%86%99%e5%88%86%e7%a6%bb) - [《分区、分表、分库》](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e5%8c%ba%e5%88%86%e8%a1%a8) +- [《AOP》](https://github.com/2881099/FreeSql/wiki/AOP) - [《仓储层Repository》](https://github.com/2881099/FreeSql/wiki/Repository) - [《UnitOfWork》](https://github.com/2881099/FreeSql/wiki/%e5%b7%a5%e4%bd%9c%e5%8d%95%e5%85%83) - [《DbContext》](https://github.com/2881099/FreeSql/wiki/DbContext) \ No newline at end of file