From 976b6fb15d8137b06b49c2610d135c449ec86bfa Mon Sep 17 00:00:00 2001 From: sanchime Date: Sat, 8 Apr 2023 08:35:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=B3=9B=E5=9E=8B=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Features/DefaultFeature.cs | 15 +++++++-------- Core/Features/Feature.cs | 17 +++++++---------- Core/Features/FeatureException.cs | 9 ++++----- Core/Features/FeatureExceptionContext.cs | 9 ++++----- Core/Features/FeatureList.cs | 6 +++--- Core/Features/FeatureWrapper.cs | 18 +++++++++--------- Core/Models/Chip.cs | 1 + Core/Models/IMetadata.cs | 14 ++++++++++++++ Example/Program.cs | 6 ++++-- 9 files changed, 53 insertions(+), 42 deletions(-) diff --git a/Core/Features/DefaultFeature.cs b/Core/Features/DefaultFeature.cs index b1f5bcc..7758c26 100644 --- a/Core/Features/DefaultFeature.cs +++ b/Core/Features/DefaultFeature.cs @@ -6,11 +6,10 @@ namespace Demo.Features; /// 默认的功能类,做委托容器用 /// /// -public sealed class DefaultFeature : Feature - where TMetadata : IMetadata +public sealed class DefaultFeature : Feature { - public DefaultFeature(string name, FeatureFunction? executeFunction = null, - FeatureFunctionAsync? executeFunctionAsync = null, + public DefaultFeature(string name, FeatureFunction? executeFunction = null, + FeatureFunctionAsync? executeFunctionAsync = null, int order = 1) { _executeFunction = executeFunction; @@ -19,16 +18,16 @@ public sealed class DefaultFeature : Feature Order = order; } - private readonly FeatureFunction? _executeFunction; + private readonly FeatureFunction? _executeFunction; - private readonly FeatureFunctionAsync? _executeFunctionAsync; + private readonly FeatureFunctionAsync? _executeFunctionAsync; - public override void Execute(TMetadata metadata) + public override void Execute(IMetadata metadata) { _executeFunction?.Invoke(this, metadata); } - public override async ValueTask ExecuteAsync(TMetadata metadata) + public override async ValueTask ExecuteAsync(IMetadata metadata) { if (_executeFunctionAsync is not null) { diff --git a/Core/Features/Feature.cs b/Core/Features/Feature.cs index d107981..306599c 100644 --- a/Core/Features/Feature.cs +++ b/Core/Features/Feature.cs @@ -5,8 +5,7 @@ namespace Demo.Features; /// /// 抽象功能类 /// -public abstract class Feature - where TMetadata : IMetadata +public abstract class Feature { /// /// 执行顺序 @@ -17,27 +16,25 @@ public abstract class Feature public string Name { get; init; } = String.Empty; - public abstract void Execute(TMetadata metadata); + public abstract void Execute(IMetadata metadata); - public virtual ValueTask ExecuteAsync(TMetadata metadata) + public virtual ValueTask ExecuteAsync(IMetadata metadata) { Execute(metadata); return ValueTask.CompletedTask; } - public virtual void OnException(FeatureExceptionContext context) + public virtual void OnException(FeatureExceptionContext context) { throw context.Exception; } - public virtual ValueTask OnExceptionAsync(FeatureExceptionContext context) + public virtual ValueTask OnExceptionAsync(FeatureExceptionContext context) { throw context.Exception; } } -public delegate void FeatureFunction(Feature feature, TMetadata metadata) - where TMetadata : IMetadata; +public delegate void FeatureFunction(Feature feature, IMetadata metadata); -public delegate ValueTask FeatureFunctionAsync(Feature feature, TMetadata metadata) - where TMetadata : IMetadata; \ No newline at end of file +public delegate ValueTask FeatureFunctionAsync(Feature feature, IMetadata metadata); \ No newline at end of file diff --git a/Core/Features/FeatureException.cs b/Core/Features/FeatureException.cs index 7542381..a97c040 100644 --- a/Core/Features/FeatureException.cs +++ b/Core/Features/FeatureException.cs @@ -2,15 +2,14 @@ using Demo.Models; namespace Demo.Features; -public class FeatureException : Exception - where TMetadata : IMetadata +public class FeatureException : Exception { - public TMetadata Metadata { get; } - public Feature Feature { get; } + public IMetadata Metadata { get; } + public Feature Feature { get; } private Exception _innerException; - public FeatureException(TMetadata metadata, Feature feature, Exception ex) : base(ex.Message) + public FeatureException(IMetadata metadata, Feature feature, Exception ex) : base(ex.Message) { Metadata = metadata; Feature = feature; diff --git a/Core/Features/FeatureExceptionContext.cs b/Core/Features/FeatureExceptionContext.cs index 762c76d..7372b88 100644 --- a/Core/Features/FeatureExceptionContext.cs +++ b/Core/Features/FeatureExceptionContext.cs @@ -5,12 +5,11 @@ namespace Demo.Features; /// /// 功能运行时异常上下文 /// -public class FeatureExceptionContext - where TMetadata : IMetadata +public class FeatureExceptionContext { - public Feature Feature { get; } - public FeatureException Exception { get; } - public FeatureExceptionContext(Feature feature, FeatureException exception) + public Feature Feature { get; } + public FeatureException Exception { get; } + public FeatureExceptionContext(Feature feature, FeatureException exception) { Feature = feature; Exception = exception; diff --git a/Core/Features/FeatureList.cs b/Core/Features/FeatureList.cs index 8d9f7a9..e47c91e 100644 --- a/Core/Features/FeatureList.cs +++ b/Core/Features/FeatureList.cs @@ -7,11 +7,11 @@ namespace Demo.Features; /// 功能列表容器 /// /// -public class FeatureList : Dictionary> +public class FeatureList : Dictionary where TMetadata : IMetadata { - public FeatureList(IDictionary>? list = null) - : base(list ?? new Dictionary>()) + public FeatureList(IDictionary? list = null) + : base(list ?? new Dictionary()) { } } \ No newline at end of file diff --git a/Core/Features/FeatureWrapper.cs b/Core/Features/FeatureWrapper.cs index 4579a87..24d0ce7 100644 --- a/Core/Features/FeatureWrapper.cs +++ b/Core/Features/FeatureWrapper.cs @@ -34,7 +34,7 @@ public class FeatureWrapper /// /// public FeatureWrapper WithFeature(TFeature feature) - where TFeature : Feature + where TFeature : Feature { if (feature is null) { @@ -57,7 +57,7 @@ public class FeatureWrapper /// /// /// - public FeatureWrapper WithFeature(string name, FeatureFunction feature, int order = 1) + public FeatureWrapper WithFeature(string name, FeatureFunction feature, int order = 1) { if (String.IsNullOrWhiteSpace(name)) { @@ -65,7 +65,7 @@ public class FeatureWrapper } if (feature is not null) { - Features.TryAdd(name, new DefaultFeature(name, feature, order: order)); + Features.TryAdd(name, new DefaultFeature(name, feature, order: order)); } return this; @@ -78,7 +78,7 @@ public class FeatureWrapper /// /// /// - public FeatureWrapper WithFeature(string name, FeatureFunctionAsync feature, int order = 1) + public FeatureWrapper WithFeature(string name, FeatureFunctionAsync feature, int order = 1) { if (String.IsNullOrWhiteSpace(name)) { @@ -86,13 +86,13 @@ public class FeatureWrapper } if (feature is not null) { - Features.TryAdd(name, new DefaultFeature(name, executeFunctionAsync: feature, order: order)); + Features.TryAdd(name, new DefaultFeature(name, executeFunctionAsync: feature, order: order)); } return this; } - public void Execute() + public void ExecuteAll() { foreach (var feature in Features.Values.OrderBy(f => f.Order)) { @@ -106,7 +106,7 @@ public class FeatureWrapper } catch (Exception ex) { - feature.OnException(new FeatureExceptionContext(feature, new FeatureException(_metadata, feature, ex))); + feature.OnException(new FeatureExceptionContext(feature, new FeatureException(_metadata, feature, ex))); } } } @@ -123,7 +123,7 @@ public class FeatureWrapper } } - public async ValueTask ExecuteAsync() + public async ValueTask ExecuteAllAsync() { foreach (var feature in Features.Values.OrderBy(f => f.Order)) { @@ -137,7 +137,7 @@ public class FeatureWrapper } catch (Exception ex) { - await feature.OnExceptionAsync(new FeatureExceptionContext(feature, new FeatureException(_metadata, feature, ex))); + await feature.OnExceptionAsync(new FeatureExceptionContext(feature, new FeatureException(_metadata, feature, ex))); } } } diff --git a/Core/Models/Chip.cs b/Core/Models/Chip.cs index e6fc154..db71754 100644 --- a/Core/Models/Chip.cs +++ b/Core/Models/Chip.cs @@ -5,6 +5,7 @@ namespace Demo.Models; public abstract class Chip : IMetadata { + public abstract Guid SignId { get; } /// /// 属性 /// diff --git a/Core/Models/IMetadata.cs b/Core/Models/IMetadata.cs index e41097e..59bb3b9 100644 --- a/Core/Models/IMetadata.cs +++ b/Core/Models/IMetadata.cs @@ -7,8 +7,22 @@ namespace Demo.Models; /// public interface IMetadata { + string Name { get; } + + Guid SignId { get; } /// /// 属性 /// MetadataPropertySet Properties { get; } + + public IMetadata SetProperty(string name, object? value) + { + Properties.SetValue(name, value); + return this; + } + + public TValue? GetProperty(string name) + { + return Properties.GetValue(name).Cast(); + } } \ No newline at end of file diff --git a/Example/Program.cs b/Example/Program.cs index d94c4f5..6c7965e 100644 --- a/Example/Program.cs +++ b/Example/Program.cs @@ -5,11 +5,11 @@ try IcndChip icndChip = new IcndChip(); icndChip.SetProperty("Code", "1234") .SetProperty("Name", "芯片"); - icndChip.Features.Execute(); // 执行所有功能 + icndChip.Features.ExecuteAll(); // 执行所有功能 icndChip.Features .DropFeature("场频自适应功能") - .Execute(); + .ExecuteAll(); icndChip.Features.Execute("场频自适应功能"); @@ -26,6 +26,8 @@ catch (Exception ex) } public class IcndChip : Chip { + public override Guid SignId => Guid.Parse("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4"); + protected override void Initialize() { Name = "ICND芯片";