From 175aa809974acb9ae00df6a21fdfad931252d6f5 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 08:53:22 +0800 Subject: [PATCH 1/9] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=80=A7?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/TypeExtensions.cs | 4 +-- src/BootstrapBlazor/Services/CacheManager.cs | 32 +++++++++++++++++++ src/BootstrapBlazor/Utils/Utility.cs | 6 ++-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/BootstrapBlazor/Extensions/TypeExtensions.cs b/src/BootstrapBlazor/Extensions/TypeExtensions.cs index ccefaf184d5..d175ff26f66 100644 --- a/src/BootstrapBlazor/Extensions/TypeExtensions.cs +++ b/src/BootstrapBlazor/Extensions/TypeExtensions.cs @@ -10,9 +10,9 @@ namespace BootstrapBlazor.Components; internal static class TypeExtensions { - public static PropertyInfo? GetPropertyByName(this Type type, string propertyName) => type.GetRuntimeProperties().FirstOrDefault(p => p.Name == propertyName); + public static PropertyInfo? GetPropertyByName(this Type type, string propertyName) => CacheManager.GetRuntimeProperties(type).Find(p => p.Name == propertyName); - public static FieldInfo? GetFieldByName(this Type type, string fieldName) => type.GetRuntimeFields().FirstOrDefault(p => p.Name == fieldName); + public static FieldInfo? GetFieldByName(this Type type, string fieldName) => CacheManager.GetRuntimeFields(type).Find(p => p.Name == fieldName); public static async Task IsAuthorizedAsync(this Type type, Task? authenticateState, IAuthorizationPolicyProvider? authorizePolicy, IAuthorizationService? authorizeService, object? resource = null) { diff --git a/src/BootstrapBlazor/Services/CacheManager.cs b/src/BootstrapBlazor/Services/CacheManager.cs index 52ac65d1ae7..3358cb916a3 100644 --- a/src/BootstrapBlazor/Services/CacheManager.cs +++ b/src/BootstrapBlazor/Services/CacheManager.cs @@ -720,4 +720,36 @@ public static object GetFormatterInvoker(Type type, Func> private static Func> InvokeFormatterAsync(Func> formatter) => new(v => formatter(v)); #endregion + + #region TypeExtensions + /// + /// 通过指定类型获得所有属性信息 + /// + /// + /// + public static List GetRuntimeProperties(Type type) + { + var cacheKey = $"{nameof(GetRuntimeProperties)}-{type.GetUniqueTypeName()}"; + return Instance.GetOrCreate(cacheKey, entry => + { + entry.SetDynamicAssemblyPolicy(type); + return type.GetRuntimeProperties().ToList(); + }); + } + + /// + /// 通过指定类型获得所有字段信息 + /// + /// + /// + public static List GetRuntimeFields(Type type) + { + var cacheKey = $"{nameof(GetRuntimeFields)}-{type.GetUniqueTypeName()}"; + return Instance.GetOrCreate(cacheKey, entry => + { + entry.SetDynamicAssemblyPolicy(type); + return type.GetRuntimeFields().ToList(); + })!; + } + #endregion } diff --git a/src/BootstrapBlazor/Utils/Utility.cs b/src/BootstrapBlazor/Utils/Utility.cs index 7e7dc4925f0..d85523777c4 100644 --- a/src/BootstrapBlazor/Utils/Utility.cs +++ b/src/BootstrapBlazor/Utils/Utility.cs @@ -213,13 +213,13 @@ public static class Utility /// public static void Reset(TModel source, TModel model) where TModel : class { - var v = model; + var modelType = model.GetType(); foreach (var pi in source.GetType().GetRuntimeProperties().Where(p => p.IsCanWrite())) { - var pInfo = v.GetType().GetPropertyByName(pi.Name); + var pInfo = modelType.GetPropertyByName(pi.Name); if (pInfo != null) { - pi.SetValue(source, pInfo.GetValue(v)); + pi.SetValue(source, pInfo.GetValue(model)); } } } From 7a80fa3c6d8b66d318cc7a37f8e49290876770e2 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 08:53:53 +0800 Subject: [PATCH 2/9] =?UTF-8?q?refactor:=20=E7=B2=BE=E7=AE=80=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/Table/Table.razor.Search.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.Search.cs b/src/BootstrapBlazor/Components/Table/Table.razor.Search.cs index 7088f29a28d..a8d716c1105 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.Search.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.Search.cs @@ -246,7 +246,7 @@ protected IEnumerable GetCustomerSearches() protected List GetAdvanceSearches() { var searches = new List(); - if (ShowAdvancedSearch && CustomerSearchModel == null && SearchModel != null) + if (ShowAdvancedSearch && CustomerSearchModel == null) { var callback = GetAdvancedSearchFilterCallback ?? new Func?>((p, model) => { From 7b705141714f77a13fffa60363bf0ee0caafe44f Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 09:04:05 +0800 Subject: [PATCH 3/9] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=8A=A0=20CreateSear?= =?UTF-8?q?chModelCallback=20=E5=9B=9E=E8=B0=83=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs | 8 ++++++++ .../Components/Table/Table.razor.Search.cs | 2 +- src/BootstrapBlazor/Components/Table/Table.razor.cs | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs b/src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs index 68a1b7bb729..0d3acf7326a 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs @@ -304,6 +304,14 @@ private TItem CreateTItem() return item; } + /// + /// 获得/设置 新建搜索模型回调方法 默认 null 未设置时先 尝试使用 回调,再使用默认无参构造函数创建 + /// + [Parameter] + public Func? CreateSearchModelCallback { get; set; } + + private TItem CreateSearchModel() => CreateSearchModelCallback?.Invoke() ?? CreateTItem(); + /// /// 单选模式下选择行时调用此方法 /// diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.Search.cs b/src/BootstrapBlazor/Components/Table/Table.razor.Search.cs index a8d716c1105..5598899dcc8 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.Search.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.Search.cs @@ -139,7 +139,7 @@ protected async Task ResetSearchClick() } else if (SearchTemplate == null) { - Utility.Reset(SearchModel, CreateTItem()); + Utility.Reset(SearchModel, CreateSearchModel()); } PageIndex = 1; diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.cs b/src/BootstrapBlazor/Components/Table/Table.razor.cs index ad3f66d870e..010f55aeaf4 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.cs @@ -859,7 +859,7 @@ private void OnInitParameters() TreeNodeLoadingIcon ??= IconTheme.GetIconByKey(ComponentIcons.TableTreeNodeLoadingIcon); AdvancedSortButtonIcon ??= IconTheme.GetIconByKey(ComponentIcons.TableAdvancedSortButtonIcon); - SearchModel ??= CreateTItem(); + SearchModel ??= CreateSearchModel(); } /// From b614e4c23d2001a4a8050f1ba7691548bf128cf3 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 09:13:31 +0800 Subject: [PATCH 4/9] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Extensions/IQueryableExtensionsTest.cs | 6 +++--- test/UnitTest/Extensions/LambadaExtensionsTest.cs | 2 +- test/UnitTest/Extensions/ObjectExtensionsTest.cs | 2 +- test/UnitTest/Extensions/QueryPageOptionsExtensionsTest.cs | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/UnitTest/Extensions/IQueryableExtensionsTest.cs b/test/UnitTest/Extensions/IQueryableExtensionsTest.cs index cbce7e00b65..c4a1d1b67ee 100644 --- a/test/UnitTest/Extensions/IQueryableExtensionsTest.cs +++ b/test/UnitTest/Extensions/IQueryableExtensionsTest.cs @@ -4,7 +4,7 @@ namespace UnitTest.Extensions; -public class IQueryableExtensionsTest +public class IQueryableExtensionsTest : BootstrapBlazorTestBase { [Fact] public void Where_Ok() @@ -28,8 +28,8 @@ public void Sort_Ok() new() { Name = "Test2" } }.AsQueryable(); - Assert.Equal("Test2", foos.Sort("Name", SortOrder.Desc, true).First().Name); - Assert.Equal("Test1", foos.Sort("Name", SortOrder.Desc, false).First().Name); + Assert.Equal("Test2", foos.Sort("Name", SortOrder.Desc, true).First().Name); + Assert.Equal("Test1", foos.Sort("Name", SortOrder.Desc, false).First().Name); } [Fact] diff --git a/test/UnitTest/Extensions/LambadaExtensionsTest.cs b/test/UnitTest/Extensions/LambadaExtensionsTest.cs index c7de7eda78d..725911582ac 100644 --- a/test/UnitTest/Extensions/LambadaExtensionsTest.cs +++ b/test/UnitTest/Extensions/LambadaExtensionsTest.cs @@ -8,7 +8,7 @@ namespace UnitTest.Extensions; -public class LambadaExtensionsTest +public class LambadaExtensionsTest : BootstrapBlazorTestBase { [Fact] public void GetFilterFunc_Null() diff --git a/test/UnitTest/Extensions/ObjectExtensionsTest.cs b/test/UnitTest/Extensions/ObjectExtensionsTest.cs index 0858051c94a..f0674fb47dc 100644 --- a/test/UnitTest/Extensions/ObjectExtensionsTest.cs +++ b/test/UnitTest/Extensions/ObjectExtensionsTest.cs @@ -7,7 +7,7 @@ namespace UnitTest.Extensions; -public class ObjectExtensionsTest +public class ObjectExtensionsTest : BootstrapBlazorTestBase { [Theory] [InlineData(null, "")] diff --git a/test/UnitTest/Extensions/QueryPageOptionsExtensionsTest.cs b/test/UnitTest/Extensions/QueryPageOptionsExtensionsTest.cs index 88c37c2930f..1e6e67a491b 100644 --- a/test/UnitTest/Extensions/QueryPageOptionsExtensionsTest.cs +++ b/test/UnitTest/Extensions/QueryPageOptionsExtensionsTest.cs @@ -4,7 +4,7 @@ namespace UnitTest.Extensions; -public class QueryPageOptionsExtensionsTest +public class QueryPageOptionsExtensionsTest : BootstrapBlazorTestBase { private readonly Foo[] _foos; From 74b202a3a86c87ba8e2ab7b5a73a66010d11e44a Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 10:53:36 +0800 Subject: [PATCH 5/9] chore: bump version 8.10.2-beta02 --- src/BootstrapBlazor/BootstrapBlazor.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index 7fa0a472304..d77361a2ee0 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 8.10.2-beta01 + 8.10.2-beta02 From f28ce89d3c8925ce16c659a9f8d434935bc42a01 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 11:03:57 +0800 Subject: [PATCH 6/9] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Performance/ReflectionTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/UnitTest/Performance/ReflectionTest.cs b/test/UnitTest/Performance/ReflectionTest.cs index 55cf5187730..1d5395430f9 100644 --- a/test/UnitTest/Performance/ReflectionTest.cs +++ b/test/UnitTest/Performance/ReflectionTest.cs @@ -6,7 +6,7 @@ namespace UnitTest.Performance; -public class ReflectionTest(ITestOutputHelper logger) +public class ReflectionTest(ITestOutputHelper logger) : BootstrapBlazorTestBase { private ITestOutputHelper Logger { get; } = logger; From 5987e93584dc81b1320e2c8831c296c929201d67 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 11:29:34 +0800 Subject: [PATCH 7/9] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/TableTest.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/UnitTest/Components/TableTest.cs b/test/UnitTest/Components/TableTest.cs index 09a30ebd908..980c1e23a13 100644 --- a/test/UnitTest/Components/TableTest.cs +++ b/test/UnitTest/Components/TableTest.cs @@ -3849,7 +3849,6 @@ public void SearchTemplate_Ok() pb.AddChildContent>(pb => { pb.Add(a => a.ShowSearch, true); - pb.Add(a => a.SearchModel, new Foo()); pb.Add(a => a.SearchMode, SearchMode.Top); pb.Add(a => a.RenderMode, TableRenderMode.Table); pb.Add(a => a.SearchTemplate, foo => builder => builder.AddContent(0, "test_SearchTemplate")); @@ -3863,8 +3862,14 @@ public void SearchTemplate_Ok() }); }); }); - cut.Contains("test_SearchTemplate"); + + var table = cut.FindComponent>(); + table.SetParametersAndRender(pb => + { + pb.Add(a => a.SearchModel, null); + pb.Add(a => a.CreateSearchModelCallback, () => new Foo()); + }); } [Fact] From a80374f05e506fd8c6679116b9ce2ad113787704 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 11:37:47 +0800 Subject: [PATCH 8/9] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/TableTest.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/UnitTest/Components/TableTest.cs b/test/UnitTest/Components/TableTest.cs index 980c1e23a13..d778ae52c08 100644 --- a/test/UnitTest/Components/TableTest.cs +++ b/test/UnitTest/Components/TableTest.cs @@ -3838,6 +3838,14 @@ public async Task CustomerSearchTemplate_Ok() var resetButton = cut.Find(".fa-trash-can"); await cut.InvokeAsync(() => resetButton.Click()); Assert.Null(searchModel.Name); + + var table = cut.FindComponent>(); + table.SetParametersAndRender(pb => + { + pb.Add(a => a.ShowAdvancedSearch, false); + }); + await cut.InvokeAsync(() => resetButton.Click()); + Assert.Null(searchModel.Name); } [Fact] @@ -3865,11 +3873,14 @@ public void SearchTemplate_Ok() cut.Contains("test_SearchTemplate"); var table = cut.FindComponent>(); + Assert.NotNull(table.Instance.SearchModel); + table.SetParametersAndRender(pb => { pb.Add(a => a.SearchModel, null); pb.Add(a => a.CreateSearchModelCallback, () => new Foo()); }); + Assert.NotNull(table.Instance.SearchModel); } [Fact] From f4058b72350c07a7cd60c484672c4124200fcc6e Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 11:42:48 +0800 Subject: [PATCH 9/9] =?UTF-8?q?refactor:=20=E5=88=A0=E9=99=A4=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=E6=8E=89=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/ValidateForm/ValidateForm.razor.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs b/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs index 1dcf2fbae60..ffa34608a94 100644 --- a/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs +++ b/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs @@ -632,13 +632,6 @@ public void NotifyFieldChanged(in FieldIdentifier fieldIdentifier, object? value OnFieldValueChanged?.Invoke(fieldIdentifier.FieldName, value); } - //private readonly List _invalidComponents = []; - - //internal void AddValidationComponent(string id) - //{ - // _invalidComponents.Add(id); - //} - /// /// 获取 当前表单值改变的属性集合 ///