update

28810
2019-05-24 17:42:03 +08:00
parent fe216d5ef1
commit 5cd585afe9
12 changed files with 134 additions and 2 deletions

121
AOP.md Normal file

@@ -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)

@@ -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)

@@ -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)

@@ -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)

@@ -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)

@@ -67,6 +67,7 @@ fsql.GetGuidRepository<User>().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)

@@ -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)

@@ -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)

@@ -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)
- [《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)

@@ -6,6 +6,7 @@
- 补充 IUpdate.Set(a => a.Click == 10),简化 Set 更新单个字段表达式;
- 优化 延时导航属性的错误提醒,当无法匹配错误,转到重写类 get 时抛出(实现延时导航属性,与普通导航一起使用);
- 优化 ICodeFirst.SyncStructure 错误提示,当使用不可迁移实体时;
- 优化 实体数据属性 DbDefaultValue 处理;
## v0.5.21

@@ -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)
- [《租户》](https://github.com/2881099/FreeSql/wiki/%e7%a7%9f%e6%88%b7)
- [《AOP》](https://github.com/2881099/FreeSql/wiki/AOP)

@@ -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)