Skip to content

Commit 0fb26cf

Browse files
Add IgnoreEntityAddedDefaultValue
Add IgnoreEntityAddedDefaultValue
1 parent b11d271 commit 0fb26cf

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

src/shared/Z.EF.Plus.Audit.Shared/Audit/AuditEntityAdded.cs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Linq;
1717

1818
#elif EFCORE
19+
using System;
1920
using Microsoft.EntityFrameworkCore;
2021
using Microsoft.EntityFrameworkCore.ChangeTracking;
2122

@@ -56,8 +57,9 @@ public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry obje
5657
{
5758
var name = record.GetName(i);
5859
var value = record.GetValue(i);
60+
var type = record.GetFieldType(i);
5961

60-
if (auditEntry.Parent.Configuration.UseNullForDBNullValue && value == DBNull.Value)
62+
if (auditEntry.Parent.Configuration.UseNullForDBNullValue && value == DBNull.Value)
6163
{
6264
value = null;
6365
}
@@ -74,14 +76,26 @@ public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry obje
7476
auditEntry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(auditEntry, objectStateEntry, string.Concat(prefix, name), null, value)) :
7577
new AuditEntryProperty();
7678

77-
auditEntryProperty.Build(auditEntry, string.Concat(prefix, name), null, value);
79+
if (auditEntry.Parent.CurrentOrDefaultConfiguration.IgnoreEntityAddedDefaultValue && type != typeof(object) && value != null)
80+
{
81+
var checkDefaultValue = DefaultValue(type);
82+
if (checkDefaultValue != null && checkDefaultValue.Equals(value))
83+
{
84+
value = null;
85+
}
86+
}
87+
88+
auditEntryProperty.Build(auditEntry, string.Concat(prefix, name), null, value);
7889
auditEntry.Properties.Add(auditEntryProperty);
7990
}
8091
}
8192
}
93+
94+
95+
8296
#elif EFCORE
83-
/// <summary>Audit entity added.</summary>
84-
/// <param name="objectStateEntry">The object state entry.</param>
97+
/// <summary>Audit entity added.</summary>
98+
/// <param name="objectStateEntry">The object state entry.</param>
8599
public static void AuditEntityAdded(AuditEntry entry, EntityEntry objectStateEntry)
86100
{
87101
foreach (var propertyEntry in objectStateEntry.Metadata.GetProperties())
@@ -92,13 +106,33 @@ public static void AuditEntityAdded(AuditEntry entry, EntityEntry objectStateEnt
92106
{
93107
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
94108
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, null, property.CurrentValue)) :
95-
new AuditEntryProperty();
109+
new AuditEntryProperty();
110+
111+
var value = property.CurrentValue;
96112

97-
auditEntryProperty.Build(entry, propertyEntry.Name, null, property.CurrentValue);
113+
if (entry.Parent.CurrentOrDefaultConfiguration.IgnoreEntityAddedDefaultValue && property.Metadata.FieldInfo != null && property.Metadata.FieldInfo.FieldType != typeof(object) && property.CurrentValue != null)
114+
{
115+
var checkDefaultValue = DefaultValue(property.Metadata.FieldInfo.FieldType);
116+
if (checkDefaultValue != null && checkDefaultValue.Equals(property.CurrentValue))
117+
{
118+
value = null;
119+
}
120+
}
121+
122+
auditEntryProperty.Build(entry, propertyEntry.Name, null, value);
98123
entry.Properties.Add(auditEntryProperty);
99124
}
100125
}
101126
}
102127
#endif
103-
}
128+
129+
// https://stackoverflow.com/questions/2490244/default-value-of-a-type-at-runtime
130+
internal static object DefaultValue(Type type)
131+
{
132+
if (type.IsValueType)
133+
return Activator.CreateInstance(type);
134+
135+
return null;
136+
}
137+
}
104138
}

src/shared/Z.EF.Plus.Audit.Shared/AuditConfiguration.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public AuditConfiguration()
4949
}
5050

5151
internal Func<Type, string> EntityNameFactory { get; set; }
52-
internal Func<Type, string, string> PropertyNameFactory { get; set; }
52+
internal Func<Type, string, string> PropertyNameFactory { get; set; }
5353

54-
/// <summary>Gets or sets the audit entry factory.</summary>
55-
/// <value>The audit entry factory.</value>
56-
public Func<AuditEntryFactoryArgs, AuditEntry> AuditEntryFactory { get; set; }
54+
/// <summary>Gets or sets the audit entry factory.</summary>
55+
/// <value>The audit entry factory.</value>
56+
public Func<AuditEntryFactoryArgs, AuditEntry> AuditEntryFactory { get; set; }
5757

5858
/// <summary>Gets or sets the audit entry property factory.</summary>
5959
/// <value>The audit entry property factory.</value>
@@ -95,9 +95,13 @@ public AuditConfiguration()
9595
/// <value>true if ignore entity soft deleted, false if not.</value>
9696
public bool IgnoreEntitySoftDeleted { get; set; }
9797

98-
/// <summary>Gets or sets a value indicating whether the association with Added state are audited.</summary>
99-
/// <value>true if association with Added state are audited, false if not.</value>
100-
public bool IgnoreRelationshipAdded { get; set; }
98+
/// <summary>Gets or sets if default value should be considered as null.</summary>
99+
/// <value>true if default value should be considered as null.</value>
100+
public bool IgnoreEntityAddedDefaultValue { get; set; }
101+
102+
/// <summary>Gets or sets a value indicating whether the association with Added state are audited.</summary>
103+
/// <value>true if association with Added state are audited, false if not.</value>
104+
public bool IgnoreRelationshipAdded { get; set; }
101105

102106
/// <summary>Gets or sets a value indicating whether the association with Deleted state are audited.</summary>
103107
/// <value>true if association with Deleted state are audited, false if not.</value>
@@ -156,6 +160,7 @@ public AuditConfiguration Clone()
156160
IgnoreEntityAdded = IgnoreEntityAdded,
157161
IgnoreEntityModified = IgnoreEntityModified,
158162
IgnoreEntityDeleted = IgnoreEntityDeleted,
163+
IgnoreEntityAddedDefaultValue = IgnoreEntityAddedDefaultValue,
159164
IgnoreEntitySoftAdded = IgnoreEntitySoftAdded,
160165
IgnoreEntitySoftDeleted = IgnoreEntitySoftDeleted,
161166
IgnorePropertyUnchanged = IgnorePropertyUnchanged,

0 commit comments

Comments
 (0)