Skip to content

Commit 66e0f2e

Browse files
Improved audit logic
1 parent a6a0303 commit 66e0f2e

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

src/shared/Z.EF.Plus.Audit.Shared/Extensions/DbSet`AuditEntry/DbSet`AuditEntry.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,33 @@ public static IQueryable<AuditEntry> Where<T>(this DbSet<AuditEntry> set, T entr
2828
}
2929

3030
public static IQueryable<TAuditEntry> Where<TAuditEntry, T>(this DbSet<TAuditEntry> set, T entry) where TAuditEntry : AuditEntry where T : class
31+
{
32+
return set.Where<TAuditEntry, T>(AuditManager.DefaultConfiguration, entry);
33+
}
34+
35+
public static IQueryable<AuditEntry> Where<T>(this DbSet<AuditEntry> set, params object[] keyValues) where T : class
36+
{
37+
return set.Where<AuditEntry, T>(keyValues);
38+
}
39+
40+
public static IQueryable<TAuditEntry> Where<TAuditEntry, T>(this DbSet<TAuditEntry> set, params object[] keyValues) where TAuditEntry : AuditEntry where T : class
41+
{
42+
return set.Where<TAuditEntry, T>(AuditManager.DefaultConfiguration, keyValues);
43+
}
44+
45+
public static IQueryable<AuditEntry> Where<T>(this DbSet<AuditEntry> set, AuditConfiguration auditConfiguration, T entry) where T : class
46+
{
47+
return set.Where<AuditEntry, T>(auditConfiguration, entry);
48+
}
49+
50+
public static IQueryable<TAuditEntry> Where<TAuditEntry, T>(this DbSet<TAuditEntry> set, AuditConfiguration auditConfiguration, T entry) where TAuditEntry : AuditEntry where T : class
3151
{
3252
var context = set.GetDbContext();
3353
var keyNames = context.GetKeyNames<T>();
3454

35-
var name = AuditManager.DefaultConfiguration.DataAnnotationEntityDisplayName(typeof(T));
55+
var name = auditConfiguration.EntityNameFactory != null ?
56+
auditConfiguration.EntityNameFactory(typeof(T)) :
57+
typeof(T).Name;
3658

3759
if (entry == null)
3860
{
@@ -55,17 +77,19 @@ public static IQueryable<TAuditEntry> Where<TAuditEntry, T>(this DbSet<TAuditEnt
5577
return query;
5678
}
5779

58-
public static IQueryable<AuditEntry> Where<T>(this DbSet<AuditEntry> set, params object[] keyValues) where T : class
80+
public static IQueryable<AuditEntry> Where<T>(this DbSet<AuditEntry> set, AuditConfiguration auditConfiguration, params object[] keyValues) where T : class
5981
{
60-
return set.Where<AuditEntry, T>(keyValues);
82+
return set.Where<AuditEntry, T>(auditConfiguration, keyValues);
6183
}
6284

63-
public static IQueryable<TAuditEntry> Where<TAuditEntry, T>(this DbSet<TAuditEntry> set, params object[] keyValues) where TAuditEntry : AuditEntry where T : class
85+
public static IQueryable<TAuditEntry> Where<TAuditEntry, T>(this DbSet<TAuditEntry> set, AuditConfiguration auditConfiguration, params object[] keyValues) where TAuditEntry : AuditEntry where T : class
6486
{
6587
var context = set.GetDbContext();
6688
var keyNames = context.GetKeyNames<T>();
6789

68-
var name = AuditManager.DefaultConfiguration.DataAnnotationEntityDisplayName(typeof(T));
90+
var name = auditConfiguration.EntityNameFactory != null ?
91+
auditConfiguration.EntityNameFactory(typeof(T)) :
92+
typeof(T).Name;
6993

7094
var query = set.Where(x => x.EntityTypeName == name);
7195

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Z.EntityFramework.Plus
6+
{
7+
public static partial class AuditExtensions
8+
{
9+
/// <summary>Get the audit display name used in the Audit method (use the AuditManager.DefaultConfiguration).</summary>
10+
/// <param name="this">The type.</param>
11+
/// <returns>The audit display name used in the Audit method (use the AuditManager.DefaultConfiguration).</returns>
12+
public static string GetAuditDisplayName(this Type @this)
13+
{
14+
return @this.GetAuditDisplayName(AuditManager.DefaultConfiguration);
15+
}
16+
/// <summary>Get the audit display name used in the Audit method.</summary>
17+
/// <param name="this">The type.</param>
18+
/// <param name="auditConfiguration">The audit configuration.</param>
19+
/// <returns>The audit display name used in the Audit method.</returns>
20+
public static string GetAuditDisplayName(this Type @this, AuditConfiguration auditConfiguration)
21+
{
22+
return auditConfiguration.EntityNameFactory != null ?
23+
auditConfiguration.EntityNameFactory(@this) :
24+
@this.Name;
25+
}
26+
}
27+
}

src/shared/Z.EF.Plus.Audit.Shared/Z.EF.Plus.Audit.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@
5353
<Compile Include="$(MSBuildThisFileDirectory)Extensions\DbContext\SaveChanges.cs" />
5454
<Compile Include="$(MSBuildThisFileDirectory)Extensions\DbContext\SaveChangesAsync.cs" />
5555
<Compile Include="$(MSBuildThisFileDirectory)Extensions\DbSet`AuditEntry\DbSet`AuditEntry.cs" />
56+
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Type\GetAuditDisplayName.cs" />
5657
</ItemGroup>
5758
</Project>

0 commit comments

Comments
 (0)