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

Optimize the performance of AbpEfCoreNavigationHelper. #19538

Merged
merged 1 commit into from
Apr 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess,
if (entityChangeList != null)
{
EntityHistoryHelper.UpdateChangeList(entityChangeList);
AbpEfCoreNavigationHelper.Clear();
auditLog!.EntityChanges.AddRange(entityChangeList);
Logger.LogDebug($"Added {entityChangeList.Count} entity changes to the current audit log");
}
Expand Down Expand Up @@ -343,6 +344,18 @@ protected virtual void PublishEventsForTrackedEntity(EntityEntry entry)
EntityChangeEventHelper.PublishEntityUpdatedEvent(entry.Entity);
}
}
else if (EntityChangeOptions.Value.PublishEntityUpdatedEventWhenNavigationChanges &&
(entry.Navigations.Any(x => x.IsModified) || AbpEfCoreNavigationHelper.IsEntityEntryNavigationChanged(entry)))
{
if (entry.Entity is ISoftDelete && entry.Entity.As<ISoftDelete>().IsDeleted)
{
EntityChangeEventHelper.PublishEntityDeletedEvent(entry.Entity);
}
else
{
EntityChangeEventHelper.PublishEntityUpdatedEvent(entry.Entity);
}
}
break;

case EntityState.Deleted:
Expand All @@ -353,7 +366,7 @@ protected virtual void PublishEventsForTrackedEntity(EntityEntry entry)

if (EntityChangeOptions.Value.PublishEntityUpdatedEventWhenNavigationChanges)
{
foreach (var entityEntry in ChangeTracker.Entries().Where(x => x.State == EntityState.Unchanged && AbpEfCoreNavigationHelper.IsEntityEntryNavigationChanged(x)))
foreach (var entityEntry in AbpEfCoreNavigationHelper.GetChangedEntityEntries())
{
if (entityEntry.Entity is ISoftDelete && entityEntry.Entity.As<ISoftDelete>().IsDeleted)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,21 @@ namespace Volo.Abp.EntityFrameworkCore.ChangeTrackers;
/// </summary>
public class AbpEfCoreNavigationHelper : ITransientDependency
{
private Dictionary<string, List<AbpEntityEntryNavigationProperty>> EntityEntryNavigationProperties { get; } = new ();
protected Dictionary<string, List<AbpEntityEntryNavigationProperty>> EntityEntryNavigationProperties { get; } = new Dictionary<string, List<AbpEntityEntryNavigationProperty>>();

public virtual void ChangeTracker_Tracked(ChangeTracker changeTracker, object? sender, EntityTrackedEventArgs e)
{
foreach (var entry in changeTracker.Entries())
{
EntityEntryTrackedOrStateChanged(entry);
}
EntityEntryTrackedOrStateChanged(e.Entry);
DetectChanges();
}

public virtual void ChangeTracker_StateChanged(ChangeTracker changeTracker, object? sender, EntityStateChangedEventArgs e)
{
foreach (var entry in changeTracker.Entries())
{
EntityEntryTrackedOrStateChanged(entry);
}
EntityEntryTrackedOrStateChanged(e.Entry);
DetectChanges();
}

private void EntityEntryTrackedOrStateChanged(EntityEntry entityEntry)
protected virtual void EntityEntryTrackedOrStateChanged(EntityEntry entityEntry)
{
if (entityEntry.State != EntityState.Unchanged)
{
Expand All @@ -50,44 +46,79 @@ private void EntityEntryTrackedOrStateChanged(EntityEntry entityEntry)
var index = 0;
foreach (var navigationEntry in entityEntry.Navigations.Where(navigation => !navigation.IsModified))
{
if (!navigationEntry.IsLoaded && navigationEntry.CurrentValue == null)
var navigationProperty = navigationProperties.FirstOrDefault(x => x.Index == index);
if (navigationProperty != null)
{
index++;
continue;
if (navigationProperty.Value == null || IsCollectionAndEmpty(navigationProperty.Value))
{
navigationProperty.Value = navigationEntry.CurrentValue;
}
}

var currentValue = navigationEntry.CurrentValue;
if (navigationEntry.CurrentValue is ICollection collection)
else
{
currentValue = collection.Cast<object?>().ToList();
navigationProperties.Add(new AbpEntityEntryNavigationProperty(index, navigationEntry.Metadata.Name, navigationEntry.CurrentValue, false, entityEntry, navigationEntry));
}

var navigationProperty = navigationProperties.FirstOrDefault(x => x.Index == index);
if (navigationProperty != null)
index++;
}
}

protected virtual void DetectChanges()
{
foreach (var entityEntryNavigationProperties in EntityEntryNavigationProperties)
{
foreach (var navigationProperty in entityEntryNavigationProperties.Value.Where(x => !x.IsChanged && x.EntityEntry.State == EntityState.Unchanged))
{
if (!navigationProperty.IsChanged && (navigationProperty.Value == null || IsCollectionAndEmpty(navigationProperty.Value)))
if (navigationProperty.NavigationEntry.IsModified)
{
navigationProperty.Value = currentValue;
navigationProperty.IsChanged = currentValue != null && !IsCollectionAndEmpty(currentValue);
navigationProperty.IsChanged = true;
continue;
}

if (!navigationProperty.IsChanged && navigationProperty.Value != null && !IsCollectionAndEmpty(navigationProperty.Value))
if (navigationProperty.Value == null || IsCollectionAndEmpty(navigationProperty.Value))
{
navigationProperty.Value = currentValue;
navigationProperty.IsChanged = currentValue == null || IsCollectionAndEmpty(currentValue);
if (navigationProperty.NavigationEntry.CurrentValue != null || IsCollectionAndNotEmpty(navigationProperty.NavigationEntry.CurrentValue))
{
if (navigationProperty.NavigationEntry.CurrentValue is ICollection collection)
{
navigationProperty.Value = collection.Cast<object?>().ToList();
}
else
{
navigationProperty.Value = navigationProperty.NavigationEntry.CurrentValue;
}
}
}
}
else
{
navigationProperties.Add(new AbpEntityEntryNavigationProperty(index, navigationEntry.Metadata.Name, currentValue, false));
}

index++;
if (navigationProperty.Value != null || IsCollectionAndNotEmpty(navigationProperty.Value))
{
if (navigationProperty.NavigationEntry.CurrentValue == null || IsCollectionAndEmpty(navigationProperty.NavigationEntry.CurrentValue))
{
if (IsCollectionAndEmpty(navigationProperty.Value) && IsCollectionAndEmpty(navigationProperty.NavigationEntry.CurrentValue))
{
continue;
}

navigationProperty.IsChanged = true;
}
}
}
}
}

public bool IsEntityEntryNavigationChanged(EntityEntry entityEntry)
public virtual List<EntityEntry> GetChangedEntityEntries()
{
DetectChanges();
return EntityEntryNavigationProperties
.SelectMany(x => x.Value)
.Where(x => x.NavigationEntry.IsModified || x.IsChanged)
.Select(x => x.EntityEntry)
.ToList();
}

public virtual bool IsEntityEntryNavigationChanged(EntityEntry entityEntry)
{
DetectChanges();
if (entityEntry.State == EntityState.Modified)
{
return true;
Expand All @@ -99,32 +130,19 @@ public bool IsEntityEntryNavigationChanged(EntityEntry entityEntry)
return false;
}

var index = 0;
foreach (var navigationEntry in entityEntry.Navigations)
if (EntityEntryNavigationProperties.TryGetValue(entryId, out var navigationProperties))
{
if (navigationEntry.IsModified || (navigationEntry is ReferenceEntry && navigationEntry.As<ReferenceEntry>().TargetEntry?.State == EntityState.Modified))
{
return true;
}

EntityEntryTrackedOrStateChanged(entityEntry);

if (EntityEntryNavigationProperties.TryGetValue(entryId, out var navigationProperties))
{
var navigationProperty = navigationProperties.FirstOrDefault(x => x.Index == index);
if (navigationProperty != null && navigationProperty.IsChanged)
{
return true;
}
}

index++;
return navigationProperties.Any(x => x.IsChanged) ||
navigationProperties.Any(x => x.NavigationEntry.IsModified) ||
navigationProperties.Any(x =>
x.NavigationEntry is ReferenceEntry &&
x.NavigationEntry.As<ReferenceEntry>().TargetEntry?.State == EntityState.Modified);
}

return false;
}

public bool IsEntityEntryNavigationChanged(NavigationEntry navigationEntry, int index)
public virtual bool IsEntityEntryNavigationChanged(NavigationEntry navigationEntry, int index)
{
if (navigationEntry.IsModified || (navigationEntry is ReferenceEntry && navigationEntry.As<ReferenceEntry>().TargetEntry?.State == EntityState.Modified))
{
Expand All @@ -149,6 +167,11 @@ public bool IsEntityEntryNavigationChanged(NavigationEntry navigationEntry, int
return false;
}

public void Clear()
{
EntityEntryNavigationProperties.Clear();
}

private string? GetEntityId(EntityEntry entityEntry)
{
return entityEntry.Entity is IEntity entryEntity && entryEntity.GetKeys().Length == 1
Expand All @@ -160,4 +183,9 @@ private bool IsCollectionAndEmpty(object? value)
{
return value is ICollection && value is ICollection collection && collection.Count == 0;
}

private bool IsCollectionAndNotEmpty(object? value)
{
return value is ICollection && value is ICollection collection && collection.Count != 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Microsoft.EntityFrameworkCore.ChangeTracking;

namespace Volo.Abp.EntityFrameworkCore.ChangeTrackers;

public class AbpEntityEntryNavigationProperty
Expand All @@ -10,11 +12,17 @@ public class AbpEntityEntryNavigationProperty

public bool IsChanged { get; set; }

public AbpEntityEntryNavigationProperty(int index, string name, object? value, bool isChanged)
public EntityEntry EntityEntry { get; set; }

public NavigationEntry NavigationEntry { get; set; }

public AbpEntityEntryNavigationProperty(int index, string name, object? value, bool isChanged, EntityEntry entityEntry, NavigationEntry navigationEntry)
{
Index = index;
Name = name;
Value = value;
IsChanged = isChanged;
EntityEntry = entityEntry;
NavigationEntry = navigationEntry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ protected virtual bool ShouldSaveEntityHistory(EntityEntry entityEntry, bool def

protected virtual bool HasNavigationPropertiesChanged(EntityEntry entityEntry)
{
return entityEntry.State == EntityState.Unchanged && Options.SaveEntityHistoryWhenNavigationChanges && AbpEfCoreNavigationHelper != null && AbpEfCoreNavigationHelper.IsEntityEntryNavigationChanged(entityEntry);
return entityEntry.State == EntityState.Unchanged &&
Options.SaveEntityHistoryWhenNavigationChanges &&
AbpEfCoreNavigationHelper != null &&
AbpEfCoreNavigationHelper.IsEntityEntryNavigationChanged(entityEntry);
}

protected virtual bool ShouldSavePropertyHistory(PropertyEntry propertyEntry, bool defaultValue)
Expand Down
Loading