Skip to content

Commit 096ecdf

Browse files
committed
- 增加 DbContextOptions.AuditValue 基于 Ioc Scoped 审计值;
1 parent c74b202 commit 096ecdf

File tree

9 files changed

+417
-9
lines changed

9 files changed

+417
-9
lines changed

Extensions/FreeSql.Extensions.AggregateRoot/AggregateRootRepository/AggregateRootRepositoryAsync.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,28 @@ async Task<int> SaveTrackingChangeAsync(AggregateRootTrackingChangeInfo tracking
263263
}));
264264
}
265265

266-
var updateLogDict = tracking.UpdateLog.GroupBy(a => a.Item1).ToDictionary(a => a.Key, a => tracking.UpdateLog.Where(b => b.Item1 == a.Key).Select(b => new
266+
if (_repository.DbContextOptions.AuditValueHandler != null)
267+
{
268+
foreach (var log in tracking.UpdateLog)
269+
{
270+
var table = Orm.CodeFirst.GetTableByEntity(log.Item1);
271+
_repository.DbContextOptions.AuditValueHandler(this, new DbContextAuditValueEventArgs(Aop.AuditValueType.Update, log.Item1, log.Item3));
272+
log.Item4.Clear();
273+
foreach (var col in table.ColumnsByCs.Values)
274+
{
275+
if (table.ColumnsByCsIgnore.ContainsKey(col.CsName)) continue;
276+
if (table.ColumnsByCs.ContainsKey(col.CsName))
277+
{
278+
if (col.Attribute.IsVersion) continue;
279+
var propvalBefore = table.GetPropertyValue(log.Item2, col.CsName);
280+
var propvalAfter = table.GetPropertyValue(log.Item3, col.CsName);
281+
if (AggregateRootUtils.CompareEntityPropertyValue(col.CsType, propvalBefore, propvalAfter) == false) log.Item4.Add(col.CsName);
282+
continue;
283+
}
284+
}
285+
}
286+
}
287+
var updateLogDict = tracking.UpdateLog.GroupBy(a => a.Item1).ToDictionary(a => a.Key, a => tracking.UpdateLog.Where(b => b.Item1 == a.Key && b.Item4.Any()).Select(b => new
267288
{
268289
BeforeObject = b.Item2,
269290
AfterObject = b.Item3,

Extensions/FreeSql.Extensions.AggregateRoot/AggregateRootRepository/AggregateRootRepositorySync.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,28 @@ int SaveTrackingChange(AggregateRootTrackingChangeInfo tracking)
314314
}));
315315
}
316316

317-
var updateLogDict = tracking.UpdateLog.GroupBy(a => a.Item1).ToDictionary(a => a.Key, a => tracking.UpdateLog.Where(b => b.Item1 == a.Key).Select(b => new
317+
if (_repository.DbContextOptions.AuditValueHandler != null)
318+
{
319+
foreach (var log in tracking.UpdateLog)
320+
{
321+
var table = Orm.CodeFirst.GetTableByEntity(log.Item1);
322+
_repository.DbContextOptions.AuditValueHandler(this, new DbContextAuditValueEventArgs(Aop.AuditValueType.Update, log.Item1, log.Item3));
323+
log.Item4.Clear();
324+
foreach (var col in table.ColumnsByCs.Values)
325+
{
326+
if (table.ColumnsByCsIgnore.ContainsKey(col.CsName)) continue;
327+
if (table.ColumnsByCs.ContainsKey(col.CsName))
328+
{
329+
if (col.Attribute.IsVersion) continue;
330+
var propvalBefore = table.GetPropertyValue(log.Item2, col.CsName);
331+
var propvalAfter = table.GetPropertyValue(log.Item3, col.CsName);
332+
if (AggregateRootUtils.CompareEntityPropertyValue(col.CsType, propvalBefore, propvalAfter) == false) log.Item4.Add(col.CsName);
333+
continue;
334+
}
335+
}
336+
}
337+
}
338+
var updateLogDict = tracking.UpdateLog.GroupBy(a => a.Item1).ToDictionary(a => a.Key, a => tracking.UpdateLog.Where(b => b.Item1 == a.Key && b.Item4.Any()).Select(b => new
318339
{
319340
BeforeObject = b.Item2,
320341
AfterObject = b.Item3,

Extensions/FreeSql.Extensions.AggregateRoot/FreeSql.Extensions.AggregateRoot.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
</PropertyGroup>
2424

2525
<ItemGroup>
26-
<None Include="../../readme.md" Pack="true" PackagePath="\"/>
26+
<None Include="../../readme.md" Pack="true" PackagePath="\" />
2727
<None Include="../../logo.png" Pack="true" PackagePath="\" />
2828
</ItemGroup>
2929

3030
<ItemGroup>
31-
<PackageReference Include="FreeSql.DbContext" Version="3.2.811-preview20240125" />
31+
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
3232
</ItemGroup>
3333

3434
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">

FreeSql.DbContext/DbContext/DbContextOptions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
using FreeSql.Internal.Model;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -47,5 +48,34 @@ public bool EnableAddOrUpdateNavigateList
4748
/// 实体变化事件
4849
/// </summary>
4950
public Action<List<DbContext.EntityChangeReport.ChangeInfo>> OnEntityChange { get; set; }
51+
52+
/// <summary>
53+
/// DbContext/Repository 审计值事件,适合 Scoped IOC 中获取登陆信息
54+
/// </summary>
55+
public event EventHandler<DbContextAuditValueEventArgs> AuditValue;
56+
public EventHandler<DbContextAuditValueEventArgs> AuditValueHandler => AuditValue;
57+
}
58+
59+
public class DbContextAuditValueEventArgs : EventArgs
60+
{
61+
public DbContextAuditValueEventArgs(Aop.AuditValueType auditValueType, Type entityType, object obj)
62+
{
63+
this.AuditValueType = auditValueType;
64+
this.EntityType = entityType;
65+
this.Object = obj;
66+
}
67+
68+
/// <summary>
69+
/// 类型
70+
/// </summary>
71+
public Aop.AuditValueType AuditValueType { get; }
72+
/// <summary>
73+
/// 类型
74+
/// </summary>
75+
public Type EntityType { get; }
76+
/// <summary>
77+
/// 实体对象
78+
/// </summary>
79+
public object Object { get; }
5080
}
5181
}

FreeSql.DbContext/DbSet/DbSet.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,27 @@ protected virtual IInsert<TEntity> OrmInsert()
6666
if (_db.Options.NoneParameter != null) insert.NoneParameter(_db.Options.NoneParameter.Value);
6767
return insert;
6868
}
69-
protected virtual IInsert<TEntity> OrmInsert(TEntity data)
69+
protected virtual IInsert<TEntity> OrmInsert(TEntity entity)
7070
{
7171
var insert = OrmInsert();
72-
if (data != null) (insert as InsertProvider<TEntity>)._source.Add(data); //防止 Aop.AuditValue 触发两次
72+
if (entity != null)
73+
{
74+
(insert as InsertProvider<TEntity>)._source.Add(entity); //防止 Aop.AuditValue 触发两次
75+
if (_db.Options.AuditValueHandler != null)
76+
_db.Options.AuditValueHandler(_db, new DbContextAuditValueEventArgs(Aop.AuditValueType.Insert, _table.Type, entity));
77+
}
7378
return insert;
7479
}
75-
protected virtual IInsert<TEntity> OrmInsert(IEnumerable<TEntity> data)
80+
protected virtual IInsert<TEntity> OrmInsert(IEnumerable<TEntity> entitys)
7681
{
7782
var insert = OrmInsert();
78-
if (data != null) (insert as InsertProvider<TEntity>)._source.AddRange(data.Where(a => a != null)); //防止 Aop.AuditValue 触发两次
83+
if (entitys != null)
84+
{
85+
(insert as InsertProvider<TEntity>)._source.AddRange(entitys.Where(a => a != null)); //防止 Aop.AuditValue 触发两次
86+
if (_db.Options.AuditValueHandler != null)
87+
foreach (var item in entitys)
88+
_db.Options.AuditValueHandler(_db, new DbContextAuditValueEventArgs(Aop.AuditValueType.Insert, _table.Type, item));
89+
}
7990
return insert;
8091
}
8192

@@ -84,7 +95,13 @@ protected virtual IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys)
8495
var update = _db.OrmOriginal.Update<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction());
8596
if (_db.Options.NoneParameter != null) update.NoneParameter(_db.Options.NoneParameter.Value);
8697
if (_db.Options.EnableGlobalFilter == false) update.DisableGlobalFilter();
87-
if (entitys != null) (update as UpdateProvider<TEntity>)._source.AddRange(entitys.Where(a => a != null)); //防止 Aop.AuditValue 触发两次
98+
if (entitys != null)
99+
{
100+
(update as UpdateProvider<TEntity>)._source.AddRange(entitys.Where(a => a != null)); //防止 Aop.AuditValue 触发两次
101+
if (_db.Options.AuditValueHandler != null)
102+
foreach (var item in entitys)
103+
_db.Options.AuditValueHandler(_db, new DbContextAuditValueEventArgs(Aop.AuditValueType.Update, _table.Type, item));
104+
}
88105
return update;
89106
}
90107
protected virtual IDelete<TEntity> OrmDelete(object dywhere)

FreeSql.DbContext/FreeSql.DbContext.xml

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

FreeSql.DbContext/UnitOfWork/UnitOfWorkManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public void Binding(IBaseRepository repository)
7272
{
7373
var repoInfo = new RepoInfo(repository);
7474
repository.UnitOfWork = Current;
75+
if (_repos.Any(a => a.Repository == repository)) return;
7576
_repos.Add(repoInfo);
7677
}
7778
void SetAllRepositoryUow()

FreeSql/Extensions/FreeSqlGlobalExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ string LocalDisplayCsharpParameter(ParameterInfo lp)
163163
public static object CreateInstanceGetDefaultValue(this Type that)
164164
{
165165
if (that == null) return null;
166+
if (that == typeof(void)) return null;
166167
if (that == typeof(string)) return default(string);
167168
if (that == typeof(Guid)) return default(Guid);
168169
if (that == typeof(byte[])) return default(byte[]);

0 commit comments

Comments
 (0)