From be5ede0da13ee2ae9b915ff8ac9fcf596f90a406 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 18 Feb 2025 13:46:20 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20UseCache=20?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs b/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs index 187674547f9..49a9b3da75e 100644 --- a/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs +++ b/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs @@ -28,6 +28,11 @@ public class DataTableDynamicContext : DynamicObjectContext private Action? AddAttributesCallback { get; set; } + /// + /// 获得/设置 是否启用内部缓存 默认 true 启用 + /// + public bool UseCache { get; set; } = true; + /// /// 负责将 DataRow 与 Items 关联起来方便查找提高效率 /// From fe1bc467ac682200489a2a7b35f30a9f1963d566 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 18 Feb 2025 13:46:36 +0800 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20=E6=98=BE=E7=A4=BA=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs b/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs index 49a9b3da75e..38a53c09827 100644 --- a/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs +++ b/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs @@ -109,6 +109,10 @@ private static bool GetShownColumns(ITableColumn col, IEnumerable? invis /// public override IEnumerable GetItems() { + if (!UseCache) + { + Items = null; + } Items ??= BuildItems(); return Items; } From d97e6a429cb5cdad3238349c98ec71c40603b147 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 18 Feb 2025 13:48:30 +0800 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=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/Dynamic/DataTableDynamicContext.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs b/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs index 38a53c09827..ec98af44d11 100644 --- a/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs +++ b/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs @@ -109,11 +109,14 @@ private static bool GetShownColumns(ITableColumn col, IEnumerable? invis /// public override IEnumerable GetItems() { - if (!UseCache) + if (UseCache) { - Items = null; + Items ??= BuildItems(); + } + else + { + Items = BuildItems(); } - Items ??= BuildItems(); return Items; } From 2412354daa635b3940e6a97f31f75b1278c805f9 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 18 Feb 2025 14:11:37 +0800 Subject: [PATCH 4/4] =?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 --- .../Components/DataTableDynamicContextTest.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 test/UnitTest/Components/DataTableDynamicContextTest.cs diff --git a/test/UnitTest/Components/DataTableDynamicContextTest.cs b/test/UnitTest/Components/DataTableDynamicContextTest.cs new file mode 100644 index 00000000000..78b9d7a6f93 --- /dev/null +++ b/test/UnitTest/Components/DataTableDynamicContextTest.cs @@ -0,0 +1,38 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the Apache 2.0 License +// See the LICENSE file in the project root for more information. +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone + +using System.Data; + +namespace UnitTest.Components; + +public class DataTableDynamicContextTest : BootstrapBlazorTestBase +{ + [Fact] + public void UseCache_Ok() + { + var table = new DataTable(); + table.Columns.Add("Id", typeof(int)); + table.Rows.Add(1); + table.AcceptChanges(); + + var context = new DataTableDynamicContext(table); + Assert.True(context.UseCache); + + var data = context.GetItems(); + Assert.Single(data); + Assert.Equal(1, data.First().GetValue("Id")); + + // 增加数据 + table.Rows.Add(2); + var data2 = context.GetItems(); + Assert.Equal(data, data2); + + // 关闭缓存 + context.UseCache = false; + table.Rows.Add(3); + data2 = context.GetItems(); + Assert.Equal(3, data2.Count()); + } +}