Skip to content

Commit

Permalink
- 增加 DbContextOptions.AuditValue 基于 Ioc Scoped 审计值;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Jan 29, 2024
1 parent c74b202 commit 096ecdf
Show file tree
Hide file tree
Showing 9 changed files with 417 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,28 @@ async Task<int> SaveTrackingChangeAsync(AggregateRootTrackingChangeInfo tracking
}));
}

var updateLogDict = tracking.UpdateLog.GroupBy(a => a.Item1).ToDictionary(a => a.Key, a => tracking.UpdateLog.Where(b => b.Item1 == a.Key).Select(b => new
if (_repository.DbContextOptions.AuditValueHandler != null)
{
foreach (var log in tracking.UpdateLog)
{
var table = Orm.CodeFirst.GetTableByEntity(log.Item1);
_repository.DbContextOptions.AuditValueHandler(this, new DbContextAuditValueEventArgs(Aop.AuditValueType.Update, log.Item1, log.Item3));
log.Item4.Clear();
foreach (var col in table.ColumnsByCs.Values)
{
if (table.ColumnsByCsIgnore.ContainsKey(col.CsName)) continue;
if (table.ColumnsByCs.ContainsKey(col.CsName))
{
if (col.Attribute.IsVersion) continue;
var propvalBefore = table.GetPropertyValue(log.Item2, col.CsName);
var propvalAfter = table.GetPropertyValue(log.Item3, col.CsName);
if (AggregateRootUtils.CompareEntityPropertyValue(col.CsType, propvalBefore, propvalAfter) == false) log.Item4.Add(col.CsName);
continue;
}
}
}
}
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
{
BeforeObject = b.Item2,
AfterObject = b.Item3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,28 @@ int SaveTrackingChange(AggregateRootTrackingChangeInfo tracking)
}));
}

var updateLogDict = tracking.UpdateLog.GroupBy(a => a.Item1).ToDictionary(a => a.Key, a => tracking.UpdateLog.Where(b => b.Item1 == a.Key).Select(b => new
if (_repository.DbContextOptions.AuditValueHandler != null)
{
foreach (var log in tracking.UpdateLog)
{
var table = Orm.CodeFirst.GetTableByEntity(log.Item1);
_repository.DbContextOptions.AuditValueHandler(this, new DbContextAuditValueEventArgs(Aop.AuditValueType.Update, log.Item1, log.Item3));
log.Item4.Clear();
foreach (var col in table.ColumnsByCs.Values)
{
if (table.ColumnsByCsIgnore.ContainsKey(col.CsName)) continue;
if (table.ColumnsByCs.ContainsKey(col.CsName))
{
if (col.Attribute.IsVersion) continue;
var propvalBefore = table.GetPropertyValue(log.Item2, col.CsName);
var propvalAfter = table.GetPropertyValue(log.Item3, col.CsName);
if (AggregateRootUtils.CompareEntityPropertyValue(col.CsType, propvalBefore, propvalAfter) == false) log.Item4.Add(col.CsName);
continue;
}
}
}
}
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
{
BeforeObject = b.Item2,
AfterObject = b.Item3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
</PropertyGroup>

<ItemGroup>
<None Include="../../readme.md" Pack="true" PackagePath="\"/>
<None Include="../../readme.md" Pack="true" PackagePath="\" />
<None Include="../../logo.png" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FreeSql.DbContext" Version="3.2.811-preview20240125" />
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">
Expand Down
30 changes: 30 additions & 0 deletions FreeSql.DbContext/DbContext/DbContextOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using FreeSql.Internal.Model;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -47,5 +48,34 @@ public bool EnableAddOrUpdateNavigateList
/// 实体变化事件
/// </summary>
public Action<List<DbContext.EntityChangeReport.ChangeInfo>> OnEntityChange { get; set; }

/// <summary>
/// DbContext/Repository 审计值事件,适合 Scoped IOC 中获取登陆信息
/// </summary>
public event EventHandler<DbContextAuditValueEventArgs> AuditValue;
public EventHandler<DbContextAuditValueEventArgs> AuditValueHandler => AuditValue;
}

public class DbContextAuditValueEventArgs : EventArgs
{
public DbContextAuditValueEventArgs(Aop.AuditValueType auditValueType, Type entityType, object obj)
{
this.AuditValueType = auditValueType;
this.EntityType = entityType;
this.Object = obj;
}

/// <summary>
/// 类型
/// </summary>
public Aop.AuditValueType AuditValueType { get; }
/// <summary>
/// 类型
/// </summary>
public Type EntityType { get; }
/// <summary>
/// 实体对象
/// </summary>
public object Object { get; }
}
}
27 changes: 22 additions & 5 deletions FreeSql.DbContext/DbSet/DbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,27 @@ protected virtual IInsert<TEntity> OrmInsert()
if (_db.Options.NoneParameter != null) insert.NoneParameter(_db.Options.NoneParameter.Value);
return insert;
}
protected virtual IInsert<TEntity> OrmInsert(TEntity data)
protected virtual IInsert<TEntity> OrmInsert(TEntity entity)
{
var insert = OrmInsert();
if (data != null) (insert as InsertProvider<TEntity>)._source.Add(data); //防止 Aop.AuditValue 触发两次
if (entity != null)
{
(insert as InsertProvider<TEntity>)._source.Add(entity); //防止 Aop.AuditValue 触发两次
if (_db.Options.AuditValueHandler != null)
_db.Options.AuditValueHandler(_db, new DbContextAuditValueEventArgs(Aop.AuditValueType.Insert, _table.Type, entity));
}
return insert;
}
protected virtual IInsert<TEntity> OrmInsert(IEnumerable<TEntity> data)
protected virtual IInsert<TEntity> OrmInsert(IEnumerable<TEntity> entitys)
{
var insert = OrmInsert();
if (data != null) (insert as InsertProvider<TEntity>)._source.AddRange(data.Where(a => a != null)); //防止 Aop.AuditValue 触发两次
if (entitys != null)
{
(insert as InsertProvider<TEntity>)._source.AddRange(entitys.Where(a => a != null)); //防止 Aop.AuditValue 触发两次
if (_db.Options.AuditValueHandler != null)
foreach (var item in entitys)
_db.Options.AuditValueHandler(_db, new DbContextAuditValueEventArgs(Aop.AuditValueType.Insert, _table.Type, item));
}
return insert;
}

Expand All @@ -84,7 +95,13 @@ protected virtual IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys)
var update = _db.OrmOriginal.Update<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction());
if (_db.Options.NoneParameter != null) update.NoneParameter(_db.Options.NoneParameter.Value);
if (_db.Options.EnableGlobalFilter == false) update.DisableGlobalFilter();
if (entitys != null) (update as UpdateProvider<TEntity>)._source.AddRange(entitys.Where(a => a != null)); //防止 Aop.AuditValue 触发两次
if (entitys != null)
{
(update as UpdateProvider<TEntity>)._source.AddRange(entitys.Where(a => a != null)); //防止 Aop.AuditValue 触发两次
if (_db.Options.AuditValueHandler != null)
foreach (var item in entitys)
_db.Options.AuditValueHandler(_db, new DbContextAuditValueEventArgs(Aop.AuditValueType.Update, _table.Type, item));
}
return update;
}
protected virtual IDelete<TEntity> OrmDelete(object dywhere)
Expand Down
29 changes: 29 additions & 0 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions FreeSql.DbContext/UnitOfWork/UnitOfWorkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void Binding(IBaseRepository repository)
{
var repoInfo = new RepoInfo(repository);
repository.UnitOfWork = Current;
if (_repos.Any(a => a.Repository == repository)) return;
_repos.Add(repoInfo);
}
void SetAllRepositoryUow()
Expand Down
1 change: 1 addition & 0 deletions FreeSql/Extensions/FreeSqlGlobalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ string LocalDisplayCsharpParameter(ParameterInfo lp)
public static object CreateInstanceGetDefaultValue(this Type that)
{
if (that == null) return null;
if (that == typeof(void)) return null;
if (that == typeof(string)) return default(string);
if (that == typeof(Guid)) return default(Guid);
if (that == typeof(byte[])) return default(byte[]);
Expand Down
Loading

0 comments on commit 096ecdf

Please sign in to comment.