Skip to content

Commit b57d35a

Browse files
2881028810
authored andcommitted
- 增加 Aop.AuditValue 事件,在插入/更新数据时审计属性值;
1 parent baa6c41 commit b57d35a

File tree

12 files changed

+344
-9
lines changed

12 files changed

+344
-9
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Text;
5+
using Xunit;
6+
7+
namespace FreeSql.Tests.MySqlConnector
8+
{
9+
public class MySqlAop
10+
{
11+
12+
class TestAuditValue
13+
{
14+
public Guid id { get; set; }
15+
[Now]
16+
public DateTime createtime { get; set; }
17+
}
18+
class NowAttribute: Attribute { }
19+
20+
[Fact]
21+
public void AuditValue()
22+
{
23+
var now = DateTime.Now;
24+
var item = new TestAuditValue();
25+
26+
EventHandler<Aop.AuditValueEventArgs> audit = (s, e) =>
27+
{
28+
if (e.Property.GetCustomAttribute<NowAttribute>(false) != null)
29+
e.Value = DateTime.Now;
30+
};
31+
g.mysql.Aop.AuditValue += audit;
32+
33+
g.mysql.Insert(item).ExecuteAffrows();
34+
35+
g.mysql.Aop.AuditValue -= audit;
36+
37+
Assert.Equal(item.createtime.Date, now.Date);
38+
}
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Text;
5+
using Xunit;
6+
7+
namespace FreeSql.Tests.MySql
8+
{
9+
public class MySqlAopTest
10+
{
11+
12+
class TestAuditValue
13+
{
14+
public Guid id { get; set; }
15+
[Now]
16+
public DateTime createtime { get; set; }
17+
}
18+
class NowAttribute: Attribute { }
19+
20+
[Fact]
21+
public void AuditValue()
22+
{
23+
var now = DateTime.Now;
24+
var item = new TestAuditValue();
25+
26+
EventHandler<Aop.AuditValueEventArgs> audit = (s, e) =>
27+
{
28+
if (e.Property.GetCustomAttribute<NowAttribute>(false) != null)
29+
e.Value = DateTime.Now;
30+
};
31+
g.mysql.Aop.AuditValue += audit;
32+
33+
g.mysql.Insert(item).ExecuteAffrows();
34+
35+
g.mysql.Aop.AuditValue -= audit;
36+
37+
Assert.Equal(item.createtime.Date, now.Date);
38+
}
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Text;
5+
using Xunit;
6+
7+
namespace FreeSql.Tests.Oracle
8+
{
9+
public class OracleAopTest
10+
{
11+
12+
class TestAuditValue
13+
{
14+
public Guid id { get; set; }
15+
[Now]
16+
public DateTime createtime { get; set; }
17+
}
18+
class NowAttribute: Attribute { }
19+
20+
[Fact]
21+
public void AuditValue()
22+
{
23+
var now = DateTime.Now;
24+
var item = new TestAuditValue();
25+
26+
EventHandler<Aop.AuditValueEventArgs> audit = (s, e) =>
27+
{
28+
if (e.Property.GetCustomAttribute<NowAttribute>(false) != null)
29+
e.Value = DateTime.Now;
30+
};
31+
g.oracle.Aop.AuditValue += audit;
32+
33+
g.oracle.Insert(item).ExecuteAffrows();
34+
35+
g.oracle.Aop.AuditValue -= audit;
36+
37+
Assert.Equal(item.createtime.Date, now.Date);
38+
}
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Text;
5+
using Xunit;
6+
7+
namespace FreeSql.Tests.PostgreSQL
8+
{
9+
public class PostgreSQLAopTest
10+
{
11+
12+
class TestAuditValue
13+
{
14+
public Guid id { get; set; }
15+
[Now]
16+
public DateTime createtime { get; set; }
17+
}
18+
class NowAttribute: Attribute { }
19+
20+
[Fact]
21+
public void AuditValue()
22+
{
23+
var now = DateTime.Now;
24+
var item = new TestAuditValue();
25+
26+
EventHandler<Aop.AuditValueEventArgs> audit = (s, e) =>
27+
{
28+
if (e.Property.GetCustomAttribute<NowAttribute>(false) != null)
29+
e.Value = DateTime.Now;
30+
};
31+
g.pgsql.Aop.AuditValue += audit;
32+
33+
g.pgsql.Insert(item).ExecuteAffrows();
34+
35+
g.pgsql.Aop.AuditValue -= audit;
36+
37+
Assert.Equal(item.createtime.Date, now.Date);
38+
}
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Text;
5+
using Xunit;
6+
7+
namespace FreeSql.Tests.SqlServer
8+
{
9+
public class SqlServerAopTest
10+
{
11+
12+
class TestAuditValue
13+
{
14+
public Guid id { get; set; }
15+
[Now]
16+
public DateTime createtime { get; set; }
17+
}
18+
class NowAttribute: Attribute { }
19+
20+
[Fact]
21+
public void AuditValue()
22+
{
23+
var now = DateTime.Now;
24+
var item = new TestAuditValue();
25+
26+
EventHandler<Aop.AuditValueEventArgs> audit = (s, e) =>
27+
{
28+
if (e.Property.GetCustomAttribute<NowAttribute>(false) != null)
29+
e.Value = DateTime.Now;
30+
};
31+
g.sqlserver.Aop.AuditValue += audit;
32+
33+
g.sqlserver.Insert(item).ExecuteAffrows();
34+
35+
g.sqlserver.Aop.AuditValue -= audit;
36+
37+
Assert.Equal(item.createtime.Date, now.Date);
38+
}
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Text;
5+
using Xunit;
6+
7+
namespace FreeSql.Tests.Sqlite
8+
{
9+
public class SqliteAopTest
10+
{
11+
12+
class TestAuditValue
13+
{
14+
public Guid id { get; set; }
15+
[Now]
16+
public DateTime createtime { get; set; }
17+
}
18+
class NowAttribute: Attribute { }
19+
20+
[Fact]
21+
public void AuditValue()
22+
{
23+
var now = DateTime.Now;
24+
var item = new TestAuditValue();
25+
26+
EventHandler<Aop.AuditValueEventArgs> audit = (s, e) =>
27+
{
28+
if (e.Property.GetCustomAttribute<NowAttribute>(false) != null)
29+
e.Value = DateTime.Now;
30+
};
31+
g.sqlite.Aop.AuditValue += audit;
32+
33+
g.sqlite.Insert(item).ExecuteAffrows();
34+
35+
g.sqlite.Aop.AuditValue -= audit;
36+
37+
Assert.Equal(item.createtime.Date, now.Date);
38+
}
39+
}
40+
}

FreeSql/FreeSql.xml

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/Interface/IAop.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using FreeSql.DataAnnotations;
22
using FreeSql.DatabaseModel;
3+
using FreeSql.Internal.Model;
34
using System;
45
using System.Collections.Generic;
56
using System.Data.Common;
@@ -53,6 +54,11 @@ public interface IAop
5354
/// CodeFirst迁移,执行完成触发
5455
/// </summary>
5556
EventHandler<Aop.SyncStructureAfterEventArgs> SyncStructureAfter { get; set; }
57+
58+
/// <summary>
59+
/// Insert/Update自动值处理, e.Column.SetMapValue(
60+
/// </summary>
61+
EventHandler<Aop.AuditValueEventArgs> AuditValue { get; set; }
5662
}
5763
}
5864

@@ -264,4 +270,43 @@ public SyncStructureAfterEventArgs(SyncStructureBeforeEventArgs before, string s
264270
/// </summary>
265271
public long ElapsedMilliseconds => this.Stopwatch.ElapsedMilliseconds;
266272
}
273+
274+
public class AuditValueEventArgs : EventArgs
275+
{
276+
public AuditValueEventArgs(AutoValueType autoValueType, ColumnInfo column, PropertyInfo property, object value)
277+
{
278+
this.AutoValueType = autoValueType;
279+
this.Column = column;
280+
this.Property = property;
281+
this.Value = value;
282+
}
283+
284+
/// <summary>
285+
/// 类型
286+
/// </summary>
287+
public AutoValueType AutoValueType { get; }
288+
/// <summary>
289+
/// 属性列的元数据
290+
/// </summary>
291+
public ColumnInfo Column { get; }
292+
/// <summary>
293+
/// 反射的属性信息
294+
/// </summary>
295+
public PropertyInfo Property { get; }
296+
/// <summary>
297+
/// 获取实体的属性值,也可以设置实体的属性新值
298+
/// </summary>
299+
public object Value
300+
{
301+
get => _value;
302+
set
303+
{
304+
_value = value;
305+
this.IsChanged = true;
306+
}
307+
}
308+
private object _value;
309+
internal bool IsChanged { get; private set; }
310+
}
311+
public enum AutoValueType { Update, Insert }
267312
}

FreeSql/Internal/CommonProvider/AopProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ public class AopProvider : IAop
1717
public EventHandler<Aop.CurdAfterEventArgs> CurdAfter { get; set; }
1818
public EventHandler<Aop.SyncStructureBeforeEventArgs> SyncStructureBefore { get; set; }
1919
public EventHandler<Aop.SyncStructureAfterEventArgs> SyncStructureAfter { get; set; }
20+
public EventHandler<Aop.AuditValueEventArgs> AuditValue { get; set; }
2021
}
2122
}

FreeSql/Internal/CommonProvider/InsertProvider.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,13 @@ public virtual string ToSql()
559559
object val = col.GetMapValue(d);
560560
if (col.Attribute.IsPrimary && col.Attribute.MapType.NullableTypeOrThis() == typeof(Guid) && (val == null || (Guid)val == Guid.Empty))
561561
col.SetMapValue(d, val = FreeUtil.NewMongodbId());
562+
if (_orm.Aop.AuditValue != null)
563+
{
564+
var auditArgs = new Aop.AuditValueEventArgs(Aop.AutoValueType.Insert, col, _table.Properties[col.CsName], val);
565+
_orm.Aop.AuditValue(this, auditArgs);
566+
if (auditArgs.Value != null)
567+
col.SetMapValue(d, val = auditArgs.Value);
568+
}
562569
if (_noneParameter)
563570
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, col.Attribute.MapType, val));
564571
else

0 commit comments

Comments
 (0)