From 13675f7d556362cacccbeecea8a02c639b0eafe9 Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Tue, 18 Oct 2022 10:46:45 +0800 Subject: [PATCH 1/7] =?UTF-8?q?feat:=20=E5=86=85=E7=BD=AE=20IDynamicColumn?= =?UTF-8?q?sObject=20=E6=8E=A5=E5=8F=A3=E4=B8=8E=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dynamic/DynamicColumnsObject.cs | 55 +++++++++++++++++++ .../Dynamic/IDynamicColumnsObject.cs | 16 ++++++ 2 files changed, 71 insertions(+) create mode 100644 src/BootstrapBlazor/Dynamic/DynamicColumnsObject.cs create mode 100644 src/BootstrapBlazor/Dynamic/IDynamicColumnsObject.cs diff --git a/src/BootstrapBlazor/Dynamic/DynamicColumnsObject.cs b/src/BootstrapBlazor/Dynamic/DynamicColumnsObject.cs new file mode 100644 index 000000000..c4e6e2d7a --- /dev/null +++ b/src/BootstrapBlazor/Dynamic/DynamicColumnsObject.cs @@ -0,0 +1,55 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +namespace BootstrapBlazor.Shared; + +/// +/// 动态列类 +/// +public class DynamicColumnsObject : IDynamicColumnsObject +{ + /// + /// + /// + public Dictionary Columns { get; set; } + + /// + /// 获得/设置 行主键 + /// + public Guid DynamicObjectPrimaryKey { get; set; } + + /// + /// 构造函数 + /// + /// + public DynamicColumnsObject(Dictionary columnsData) + { + Columns = columnsData; + } + + /// + /// 构造函数 + /// + public DynamicColumnsObject() : this(new()) { } + + /// + /// + /// + /// + /// + public virtual object? GetValue(string propertyName) + { + return Columns.TryGetValue(propertyName, out object? v) ? v : null; + } + + /// + /// + /// + /// + /// + public virtual void SetValue(string propertyName, object? value) + { + Columns[propertyName] = value; + } +} diff --git a/src/BootstrapBlazor/Dynamic/IDynamicColumnsObject.cs b/src/BootstrapBlazor/Dynamic/IDynamicColumnsObject.cs new file mode 100644 index 000000000..2b1872d92 --- /dev/null +++ b/src/BootstrapBlazor/Dynamic/IDynamicColumnsObject.cs @@ -0,0 +1,16 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +namespace BootstrapBlazor.Components; + +/// +/// 动态对象接口 +/// +public interface IDynamicColumnsObject : IDynamicObject +{ + /// + /// 获得设置 列与列数值集合 + /// + public Dictionary Columns { get; set; } +} -- Gitee From e0d33be7f863a23821d5541ab3268ec89e6f5a33 Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Tue, 18 Oct 2022 10:50:56 +0800 Subject: [PATCH 2/7] =?UTF-8?q?feat:=20Get/SetPropertyValue=20=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=20IDynamicColumnsObject?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Services/CacheManager.cs | 49 ++++++++++++++------ 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/src/BootstrapBlazor/Services/CacheManager.cs b/src/BootstrapBlazor/Services/CacheManager.cs index a851df8ab..821ca7339 100644 --- a/src/BootstrapBlazor/Services/CacheManager.cs +++ b/src/BootstrapBlazor/Services/CacheManager.cs @@ -467,14 +467,22 @@ internal class CacheManager : ICacheManager { throw new ArgumentNullException(nameof(model)); } - var type = model.GetType(); - var cacheKey = ($"Lambda-Get-{type.FullName}", typeof(TModel), fieldName, typeof(TResult)); - var invoker = Instance.GetOrCreate(cacheKey, entry => + + return (model is IDynamicColumnsObject d) + ? (TResult)d.GetValue(fieldName)! + : GetValue(); + + TResult GetValue() { - entry.SetDynamicAssemblyPolicy(type); - return LambdaExtensions.GetPropertyValueLambda(model, fieldName).Compile(); - }); - return invoker(model); + var type = model.GetType(); + var cacheKey = ($"Lambda-Get-{type.FullName}", typeof(TModel), fieldName, typeof(TResult)); + var invoker = Instance.GetOrCreate(cacheKey, entry => + { + entry.SetDynamicAssemblyPolicy(type); + return LambdaExtensions.GetPropertyValueLambda(model, fieldName).Compile(); + }); + return invoker(model); + } } public static void SetPropertyValue(TModel model, string fieldName, TValue value) @@ -483,14 +491,27 @@ internal class CacheManager : ICacheManager { throw new ArgumentNullException(nameof(model)); } - var type = model.GetType(); - var cacheKey = ($"Lambda-Set-{type.FullName}", typeof(TModel), fieldName, typeof(TValue)); - var invoker = Instance.GetOrCreate(cacheKey, entry => + + if (model is IDynamicColumnsObject d) { - entry.SetDynamicAssemblyPolicy(type); - return LambdaExtensions.SetPropertyValueLambda(model, fieldName).Compile(); - }); - invoker(model, value); + d.SetValue(fieldName, value); + } + else + { + SetValue(); + } + + void SetValue() + { + var type = model.GetType(); + var cacheKey = ($"Lambda-Set-{type.FullName}", typeof(TModel), fieldName, typeof(TValue)); + var invoker = Instance.GetOrCreate(cacheKey, entry => + { + entry.SetDynamicAssemblyPolicy(type); + return LambdaExtensions.SetPropertyValueLambda(model, fieldName).Compile(); + }); + invoker(model, value); + } } /// -- Gitee From 1793968eaeda46a018c11d465b8a66ae0a83b997 Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Tue, 18 Oct 2022 10:52:11 +0800 Subject: [PATCH 3/7] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...a.cs => CustomDynamicColumnsObjectData.cs} | 34 ++++++------------- .../Data/CustomDynamicData.cs | 20 ++++------- .../Samples/Table/TablesDynamicObject.razor | 14 ++++++-- .../Table/TablesDynamicObject.razor.cs | 6 ++-- 4 files changed, 31 insertions(+), 43 deletions(-) rename src/BootstrapBlazor.Shared/Data/{CustomDynamicObjectData.cs => CustomDynamicColumnsObjectData.cs} (50%) diff --git a/src/BootstrapBlazor.Shared/Data/CustomDynamicObjectData.cs b/src/BootstrapBlazor.Shared/Data/CustomDynamicColumnsObjectData.cs similarity index 50% rename from src/BootstrapBlazor.Shared/Data/CustomDynamicObjectData.cs rename to src/BootstrapBlazor.Shared/Data/CustomDynamicColumnsObjectData.cs index 9aa6472a6..420d4a44c 100644 --- a/src/BootstrapBlazor.Shared/Data/CustomDynamicObjectData.cs +++ b/src/BootstrapBlazor.Shared/Data/CustomDynamicColumnsObjectData.cs @@ -7,49 +7,37 @@ namespace BootstrapBlazor.Shared; /// /// /// -public class BootstrapBlazorDynamicObjectData : IDynamicObject +public class CustomDynamicColumnsObjectData : DynamicColumnsObject { /// /// /// - public string Fix { get; set; } = ""; + public string? Fix { get; set; } /// /// /// - public Dictionary Dynamic { get; set; } - - /// - /// - /// - public Guid DynamicObjectPrimaryKey { get => new(Fix); set { } } + public CustomDynamicColumnsObjectData() : this("", new()) { } /// /// /// /// /// - public BootstrapBlazorDynamicObjectData(string fix, Dictionary data) + public CustomDynamicColumnsObjectData(string? fix, Dictionary data) { Fix = fix; - Dynamic = data; + Columns = data; } - /// - /// - /// - public BootstrapBlazorDynamicObjectData() : this("", new()) { } - /// /// /// /// /// - public object? GetValue(string propertyName) + public override object? GetValue(string propertyName) { - if (propertyName == nameof(Fix)) - return Fix; - return Dynamic.TryGetValue(propertyName, out string? v) ? v : ""; + return propertyName == nameof(Fix) ? Fix : base.GetValue(propertyName); } /// @@ -57,12 +45,10 @@ public class BootstrapBlazorDynamicObjectData : IDynamicObject /// /// /// - public void SetValue(string propertyName, object? value) + public override void SetValue(string propertyName, object? value) { - if (value is not string str) - return; if (propertyName == nameof(Fix)) - Fix = str; - Dynamic[propertyName] = str; + Fix = value?.ToString(); + Columns[propertyName] = value; } } diff --git a/src/BootstrapBlazor.Shared/Data/CustomDynamicData.cs b/src/BootstrapBlazor.Shared/Data/CustomDynamicData.cs index 63f72d569..91b7d3c7b 100644 --- a/src/BootstrapBlazor.Shared/Data/CustomDynamicData.cs +++ b/src/BootstrapBlazor.Shared/Data/CustomDynamicData.cs @@ -12,14 +12,14 @@ namespace BootstrapBlazor.Shared; public class CustomDynamicData : System.Dynamic.DynamicObject { /// - /// + /// 获得/设置 固定列 /// public string Fix { get; set; } = ""; /// /// 存储每列值信息 Key 列名 Value 为列值 /// - public Dictionary Dynamic { get; set; } + public Dictionary Columns { get; set; } /// /// @@ -29,7 +29,7 @@ public class CustomDynamicData : System.Dynamic.DynamicObject public CustomDynamicData(string fix, Dictionary data) { Fix = fix; - Dynamic = data; + Columns = data; } /// @@ -37,12 +37,6 @@ public class CustomDynamicData : System.Dynamic.DynamicObject /// public CustomDynamicData() : this("", new()) { } - /// - public override IEnumerable GetDynamicMemberNames() - { - return Dynamic.Keys.Append(nameof(Fix)); - } - /// /// /// @@ -55,9 +49,9 @@ public class CustomDynamicData : System.Dynamic.DynamicObject { result = Fix; } - else if (Dynamic.ContainsKey(binder.Name)) + else if (Columns.ContainsKey(binder.Name)) { - result = Dynamic[binder.Name]; + result = Columns[binder.Name]; } else { @@ -82,9 +76,9 @@ public class CustomDynamicData : System.Dynamic.DynamicObject Fix = v; ret = true; } - else if (Dynamic.ContainsKey(binder.Name)) + else if (Columns.ContainsKey(binder.Name)) { - Dynamic[binder.Name] = v; + Columns[binder.Name] = v; ret = true; } return ret; diff --git a/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamicObject.razor b/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamicObject.razor index 17f30af9b..3b5adcc09 100644 --- a/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamicObject.razor +++ b/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamicObject.razor @@ -11,20 +11,28 @@ @foreach (var element in DynamicColumnList) { - + + + } - @foreach (var element in StaticColumnList) { - + + + }
diff --git a/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamicObject.razor.cs b/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamicObject.razor.cs index 1a90032dd..430cb736f 100644 --- a/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamicObject.razor.cs +++ b/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamicObject.razor.cs @@ -10,7 +10,7 @@ namespace BootstrapBlazor.Shared.Samples.Table; public partial class TablesDynamicObject { [NotNull] - private IEnumerable? BootstrapDynamicItems { get; set; } + private IEnumerable? CustomDynamicItems { get; set; } private static List StaticColumnList => new() { "A", "B", "C", "Z" }; @@ -28,8 +28,8 @@ public partial class TablesDynamicObject var now = DateTime.Now; DynamicColumnList = Enumerable.Range(1, 5).Select(index => now.AddMinutes(-1 * index).ToString("HH:mm")).ToList(); - BootstrapDynamicItems = Enumerable.Range(1, 10) - .Select(index => new BootstrapBlazorDynamicObjectData(index.ToString(), StaticColumnList.ToDictionary(d => d, d => $"{d}{index}"))); + CustomDynamicItems = Enumerable.Range(1, 10) + .Select(index => new CustomDynamicColumnsObjectData(index.ToString(), StaticColumnList.ToDictionary(d => d, d => (object?)$"{d}{index}"))); } private readonly static Random random = new(); -- Gitee From 6ae8da32ac3ddb4058b7886a176dcc088674e765 Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Tue, 18 Oct 2022 12:24:15 +0800 Subject: [PATCH 4/7] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=94=B9=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E7=A9=BA=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Dynamic/DynamicColumnsObject.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Dynamic/DynamicColumnsObject.cs b/src/BootstrapBlazor/Dynamic/DynamicColumnsObject.cs index c4e6e2d7a..1381bd088 100644 --- a/src/BootstrapBlazor/Dynamic/DynamicColumnsObject.cs +++ b/src/BootstrapBlazor/Dynamic/DynamicColumnsObject.cs @@ -2,7 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ -namespace BootstrapBlazor.Shared; +namespace BootstrapBlazor.Components; /// /// 动态列类 -- Gitee From 950146ea27f78ae166add9d81bf78efc8e90e182 Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Tue, 18 Oct 2022 12:24:31 +0800 Subject: [PATCH 5/7] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20DynamicColumns?= =?UTF-8?q?Object=20=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dynamic/DynamicColumnsObjectTest.cs | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 test/UnitTest/Dynamic/DynamicColumnsObjectTest.cs diff --git a/test/UnitTest/Dynamic/DynamicColumnsObjectTest.cs b/test/UnitTest/Dynamic/DynamicColumnsObjectTest.cs new file mode 100644 index 000000000..2ae7815a9 --- /dev/null +++ b/test/UnitTest/Dynamic/DynamicColumnsObjectTest.cs @@ -0,0 +1,55 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +namespace UnitTest.Dynamic; + +public class DynamicColumnsObjectTest +{ + [Fact] + public void DynamicColumnsObject_Ok() + { + var data = new DynamicColumnsObject(); + Assert.Empty(data.Columns); + } + + [Fact] + public void DynamicColumnsObject_Columns() + { + var data = new DynamicColumnsObject(new Dictionary() + { + ["A"] = "1", + ["B"] = 1 + }); + + Assert.Equal("1", data.Columns["A"]); + Assert.Equal(1, data.Columns["B"]); + } + + [Fact] + public void DynamicColumnsObject_Key() + { + var guid = Guid.NewGuid(); + var data = new DynamicColumnsObject() + { + DynamicObjectPrimaryKey = guid + }; + + Assert.Equal(guid, data.DynamicObjectPrimaryKey); + } + + [Fact] + public void DynamicColumnsObject_Value() + { + var data = new DynamicColumnsObject(); + data.Columns["A"] = "1"; + Assert.Equal("1", data.Columns["A"]); + + data.SetValue("A", 2); + Assert.Equal(2, data.GetValue("A")); + Assert.Equal(2, data.Columns["A"]); + + Assert.Null(data.GetValue("B")); + Assert.Throws(() => data.Columns["B"]); + } +} -- Gitee From ff2c480c5d800900289f232404a79048a9afd179 Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Tue, 18 Oct 2022 12:37:16 +0800 Subject: [PATCH 6/7] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20=20Clipboard?= =?UTF-8?q?=20=E6=9C=8D=E5=8A=A1=E5=8D=95=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/Clipboard/Clipboard.cs | 3 +- test/UnitTest/Services/ClipboardTest.cs | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/UnitTest/Services/ClipboardTest.cs diff --git a/src/BootstrapBlazor/Components/Clipboard/Clipboard.cs b/src/BootstrapBlazor/Components/Clipboard/Clipboard.cs index 729200d6b..a418543c4 100644 --- a/src/BootstrapBlazor/Components/Clipboard/Clipboard.cs +++ b/src/BootstrapBlazor/Components/Clipboard/Clipboard.cs @@ -50,11 +50,12 @@ public class Clipboard : BootstrapModuleComponentBase /// Dispose 方法 /// /// - protected virtual void Dispose(bool disposing) + protected override async ValueTask DisposeAsync(bool disposing) { if (disposing) { ClipboardService.UnRegister(this); } + await base.DisposeAsync(disposing); } } diff --git a/test/UnitTest/Services/ClipboardTest.cs b/test/UnitTest/Services/ClipboardTest.cs new file mode 100644 index 000000000..1fb00092a --- /dev/null +++ b/test/UnitTest/Services/ClipboardTest.cs @@ -0,0 +1,33 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using Microsoft.Extensions.DependencyInjection; + +namespace UnitTest.Services; + +public class ClipboardServiceTest : BootstrapBlazorTestBase +{ + [Fact] + public void ClipboardService_Null() + { + var service = Context.Services.GetRequiredService(); + Assert.ThrowsAsync(() => service.Copy("test")); + } + + [Fact] + public void ClipboardService_Ok() + { + var service = Context.Services.GetRequiredService(); + var cut = Context.RenderComponent(); + var copied = false; + service.Copy("Test", () => + { + copied = true; + return Task.CompletedTask; + }); + Assert.True(copied); + + _ = cut.Instance.DisposeAsync(); + } +} -- Gitee From a9391e7470e62bf515d7a186e18a72f4c6b26029 Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Tue, 18 Oct 2022 12:39:46 +0800 Subject: [PATCH 7/7] chore: bump version 6.11.16 --- 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 e449b8dd4..b6ba1da86 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 6.11.15 + 6.11.16 -- Gitee