From fda74d306bc87554cdf8cb0093ec304a551bbf67 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 22 Oct 2026 08:46:57 +0800 Subject: [PATCH] Cache MudBlazor sort functions by property path instead of column --- .../AbpMudExtensibleDataGrid.razor.cs | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/framework/src/Volo.Abp.MudBlazorUI/Components/AbpMudExtensibleDataGrid.razor.cs b/framework/src/Volo.Abp.MudBlazorUI/Components/AbpMudExtensibleDataGrid.razor.cs index 8c0bee460e..94c72cb58c 100644 --- a/framework/src/Volo.Abp.MudBlazorUI/Components/AbpMudExtensibleDataGrid.razor.cs +++ b/framework/src/Volo.Abp.MudBlazorUI/Components/AbpMudExtensibleDataGrid.razor.cs @@ -32,9 +32,10 @@ public partial class AbpMudExtensibleDataGrid : ComponentBase private readonly Dictionary, string> _sortFuncToPropertyMap = new(); /// - /// Keeps one sort function per column, so re-renders don't compile new ones and grow . + /// Keeps one sort function per sort path, so re-renders reuse it instead of growing . + /// Keyed by path rather than by column, because a column's data can change between renders. /// - private readonly Dictionary> _columnSortFuncs = new(); + private readonly Dictionary> _sortFuncsByPath = new(); [Parameter] public Func, CancellationToken, Task>>? ServerData { get; set; } @@ -205,11 +206,6 @@ public partial class AbpMudExtensibleDataGrid : ComponentBase return null; } - if (_columnSortFuncs.TryGetValue(column, out var cachedSortFunc)) - { - return cachedSortFunc; - } - // Determine the property path for sorting string? propertyPath = null; if (!string.IsNullOrEmpty(column.PropertyName)) @@ -226,6 +222,12 @@ public partial class AbpMudExtensibleDataGrid : ComponentBase return null; } + var cacheKey = "property:" + propertyPath; + if (_sortFuncsByPath.TryGetValue(cacheKey, out var cachedSortFunc)) + { + return cachedSortFunc; + } + var properties = propertyPath.Split('.'); // Build a lambda expression that directly accesses the property @@ -254,7 +256,7 @@ public partial class AbpMudExtensibleDataGrid : ComponentBase // Register the mapping so we can resolve property name from SortDefinition.SortFunc later _sortFuncToPropertyMap[sortFunc] = propertyPath; - _columnSortFuncs[column] = sortFunc; + _sortFuncsByPath[cacheKey] = sortFunc; return sortFunc; } @@ -266,11 +268,6 @@ public partial class AbpMudExtensibleDataGrid : ComponentBase return null; } - if (_columnSortFuncs.TryGetValue(column, out var cachedSortFunc)) - { - return cachedSortFunc; - } - var match = ExtensionPropertiesRegex.Match(column.Data); if (!match.Success) { @@ -278,6 +275,12 @@ public partial class AbpMudExtensibleDataGrid : ComponentBase } var propertyName = match.Groups[1].Value; + var cacheKey = "extension:" + propertyName; + if (_sortFuncsByPath.TryGetValue(cacheKey, out var cachedSortFunc)) + { + return cachedSortFunc; + } + Func sortFunc = item => { var entity = item as IHasExtraProperties; @@ -286,7 +289,7 @@ public partial class AbpMudExtensibleDataGrid : ComponentBase // Register the mapping so we can resolve property name from SortDefinition.SortFunc later _sortFuncToPropertyMap[sortFunc] = propertyName; - _columnSortFuncs[column] = sortFunc; + _sortFuncsByPath[cacheKey] = sortFunc; return sortFunc; }