Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove redundant dictionary lookups #631

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Audit.EntityFramework/AuditEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static EntityFrameworkEvent GetEntityFrameworkEvent(this AuditEvent audit
return (auditEvent as AuditEventEntityFramework).EntityFrameworkEvent;
}
// For backwards compatibility
return auditEvent.CustomFields.ContainsKey("EntityFrameworkEvent")
? Core.Configuration.JsonAdapter.ToObject<EntityFrameworkEvent>(auditEvent.CustomFields["EntityFrameworkEvent"])
return auditEvent.CustomFields.TryGetValue("EntityFrameworkEvent", out var field)
? Core.Configuration.JsonAdapter.ToObject<EntityFrameworkEvent>(field)
: null;
}

Expand All @@ -49,8 +49,8 @@ public static CommandEvent GetCommandEntityFrameworkEvent(this AuditEvent auditE
{
return (auditEvent as AuditEventCommandEntityFramework).CommandEvent;
}
return auditEvent.CustomFields.ContainsKey("CommandEvent")
? Core.Configuration.JsonAdapter.ToObject<CommandEvent>(auditEvent.CustomFields["CommandEvent"])
return auditEvent.CustomFields.TryGetValue("CommandEvent", out var field)
? Core.Configuration.JsonAdapter.ToObject<CommandEvent>(field)
: null;
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/Audit.EntityFramework/DbContextHelper.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ private bool HasPropertyValue(IAuditDbContext context, EntityEntry entry, string
return false;
}
var overrideProperties = EnsurePropertiesOverrideAttrCache(entityType);
if (overrideProperties != null && overrideProperties.ContainsKey(propName))
if (overrideProperties != null && overrideProperties.TryGetValue(propName, out var property))
{
// Property overriden with AuditOverride attribute
value = overrideProperties[propName].Value;
value = property.Value;
return true;
}
if (context.EntitySettings != null && context.EntitySettings.TryGetValue(entityType, out EfEntitySettings settings))
Expand Down
4 changes: 2 additions & 2 deletions src/Audit.EntityFramework/DbContextHelper.Net45.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ private bool HasPropertyValue(IAuditDbContext context, DbEntityEntry entry, stri
return false;
}
var overrideProperties = EnsurePropertiesOverrideAttrCache(entityType);
if (overrideProperties != null && overrideProperties.ContainsKey(propName))
if (overrideProperties != null && overrideProperties.TryGetValue(propName, out var property))
{
// Property overriden with AuditOverride attribute
value = overrideProperties[propName].Value;
value = property.Value;
return true;
}
if (context.EntitySettings != null && context.EntitySettings.TryGetValue(entityType, out EfEntitySettings settings))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,19 +315,20 @@ private void SetAuditEntityMatchedProperties(Type definingType, EventEntry entry
{
var entity = entry.Entry.Entity;
var auditFields = GetPropertiesToSet(auditType);
var columnValues = entry.ColumnValues;
#if EF_CORE
var entityFields = entry.GetEntry().Metadata.GetProperties();
foreach (var prop in entityFields.Where(af => auditFields.ContainsKey(af.Name)))
{
var colName = DbContextHelper.GetColumnName(prop);
var value = entry.ColumnValues.ContainsKey(colName) ? entry.ColumnValues[colName] : prop.PropertyInfo?.GetValue(entity);
var value = columnValues.TryGetValue(colName, out var columnValue) ? columnValue : prop.PropertyInfo?.GetValue(entity);
auditFields[prop.Name].SetValue(auditEntity, value);
}
#else
var entityFields = GetPropertiesToGet(definingType);
foreach (var prop in entityFields.Where(af => auditFields.ContainsKey(af.Key)))
{
var value = entry.ColumnValues.ContainsKey(prop.Key) ? entry.ColumnValues[prop.Key] : prop.Value.GetValue(entity);
var value = columnValues.TryGetValue(prop.Key, out var columnValue) ? columnValue : prop.Value.GetValue(entity);
auditFields[prop.Key].SetValue(auditEntity, value);
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/Audit.HttpClient/AuditEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public static HttpAction GetHttpAction(this AuditEvent auditEvent)
return (auditEvent as AuditEventHttpClient).Action;
}
// For backwards compatibility
return auditEvent.CustomFields.ContainsKey("Action")
? Configuration.JsonAdapter.ToObject<HttpAction>(auditEvent.CustomFields["Action"])
return auditEvent.CustomFields.TryGetValue("Action", out var field)
? Configuration.JsonAdapter.ToObject<HttpAction>(field)
: null;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Audit.Mvc/AuditEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static AuditAction GetMvcAuditAction(this AuditEvent auditEvent)
return (auditEvent as AuditEventMvcAction).Action;
}
// For backwards compatibility
return auditEvent.CustomFields.ContainsKey("Action")
? Configuration.JsonAdapter.ToObject<AuditAction>(auditEvent.CustomFields["Action"])
return auditEvent.CustomFields.TryGetValue("Action", out var field)
? Configuration.JsonAdapter.ToObject<AuditAction>(field)
: null;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Audit.WCF.Client/AuditEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public static WcfClientAction GetWcfClientAction(this AuditEvent auditEvent)
{
return (auditEvent as AuditEventWcfClient).WcfClientEvent;
}
return auditEvent.CustomFields.ContainsKey("WcfClientEvent")
? Configuration.JsonAdapter.ToObject<WcfClientAction>(auditEvent.CustomFields["WcfClientEvent"])
return auditEvent.CustomFields.TryGetValue("WcfClientEvent", out var field)
? Configuration.JsonAdapter.ToObject<WcfClientAction>(field)
: null;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Audit.WCF.Client/AuditMessageInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public void AfterReceiveReply(ref Message reply, object correlationState)
auditWcfEvent.IsFault = reply.IsFault;
auditWcfEvent.ResponseAction = reply.Headers?.Action;
auditWcfEvent.ResponseBody = reply.ToString();
if (reply.Properties.ContainsKey("httpResponse"))
if (reply.Properties.TryGetValue("httpResponse", out var property))
{
var res = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
var res = property as HttpResponseMessageProperty;
auditWcfEvent.ResponseStatuscode = res?.StatusCode;
auditWcfEvent.ResponseHeaders = _includeResponseHeaders ? GetHeaders(res?.Headers?.ToString()) : null;
}
Expand Down Expand Up @@ -94,9 +94,9 @@ private WcfClientAction CreateWcfClientAction(Message request)
RequestBody = request.ToString(),
MessageId = request.Headers?.MessageId?.ToString()
};
if (request.Properties.ContainsKey("httpRequest"))
if (request.Properties.TryGetValue("httpRequest", out var property))
{
var req = request.Properties["httpRequest"] as HttpRequestMessageProperty;
var req = property as HttpRequestMessageProperty;
action.HttpMethod = req?.Method;
action.RequestHeaders = _includeRequestHeaders ? GetHeaders(req?.Headers?.ToString()) : null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.WCF/AuditOperationInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private WcfEvent CreateWcfAuditEvent(object instance, object[] inputs)
var securityContext = ServiceSecurityContext.Current;
var operationContext = OperationContext.Current;
var imProps = OperationContext.Current.IncomingMessageProperties;
var endpoint = imProps.ContainsKey(RemoteEndpointMessageProperty.Name) ? imProps[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty : null;
var endpoint = imProps.TryGetValue(RemoteEndpointMessageProperty.Name, out var prop) ? prop as RemoteEndpointMessageProperty : null;
return new WcfEvent()
{
Action = _operation.Action,
Expand Down
4 changes: 2 additions & 2 deletions src/Audit.WebApi/AuditApiAdapter.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ private async Task<AuditApiAction> CreateOrUpdateAction(ActionExecutingContext a
var httpContext = actionContext.HttpContext;
var actionDescriptor = actionContext.ActionDescriptor as ControllerActionDescriptor;
AuditApiAction action = null;
if (httpContext.Items.ContainsKey(AuditApiHelper.AuditApiActionKey))
if (httpContext.Items.TryGetValue(AuditApiHelper.AuditApiActionKey, out var item))
{
action = httpContext.Items[AuditApiHelper.AuditApiActionKey] as AuditApiAction;
action = item as AuditApiAction;
}
if (action == null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Audit.WebApi/AuditEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static AuditApiAction GetWebApiAuditAction(this AuditEvent auditEvent)
return (auditEvent as AuditEventWebApi).Action;
}
// For backwards compatibility
return auditEvent.CustomFields.ContainsKey("Action")
? Configuration.JsonAdapter.ToObject<AuditApiAction>(auditEvent.CustomFields["Action"])
return auditEvent.CustomFields.TryGetValue("Action", out var field)
? Configuration.JsonAdapter.ToObject<AuditApiAction>(field)
: null;
}
}
Expand Down
Loading