移除泛型设计

This commit is contained in:
sanchime 2023-04-08 08:35:57 +08:00
parent bd0c9bbf4a
commit 976b6fb15d
9 changed files with 53 additions and 42 deletions

View File

@ -6,11 +6,10 @@ namespace Demo.Features;
/// 默认的功能类,做委托容器用 /// 默认的功能类,做委托容器用
/// </summary> /// </summary>
/// <typeparam name="TMetadata"></typeparam> /// <typeparam name="TMetadata"></typeparam>
public sealed class DefaultFeature<TMetadata> : Feature<TMetadata> public sealed class DefaultFeature : Feature
where TMetadata : IMetadata
{ {
public DefaultFeature(string name, FeatureFunction<TMetadata>? executeFunction = null, public DefaultFeature(string name, FeatureFunction? executeFunction = null,
FeatureFunctionAsync<TMetadata>? executeFunctionAsync = null, FeatureFunctionAsync? executeFunctionAsync = null,
int order = 1) int order = 1)
{ {
_executeFunction = executeFunction; _executeFunction = executeFunction;
@ -19,16 +18,16 @@ public sealed class DefaultFeature<TMetadata> : Feature<TMetadata>
Order = order; Order = order;
} }
private readonly FeatureFunction<TMetadata>? _executeFunction; private readonly FeatureFunction? _executeFunction;
private readonly FeatureFunctionAsync<TMetadata>? _executeFunctionAsync; private readonly FeatureFunctionAsync? _executeFunctionAsync;
public override void Execute(TMetadata metadata) public override void Execute(IMetadata metadata)
{ {
_executeFunction?.Invoke(this, metadata); _executeFunction?.Invoke(this, metadata);
} }
public override async ValueTask ExecuteAsync(TMetadata metadata) public override async ValueTask ExecuteAsync(IMetadata metadata)
{ {
if (_executeFunctionAsync is not null) if (_executeFunctionAsync is not null)
{ {

View File

@ -5,8 +5,7 @@ namespace Demo.Features;
/// <summary> /// <summary>
/// 抽象功能类 /// 抽象功能类
/// </summary> /// </summary>
public abstract class Feature<TMetadata> public abstract class Feature
where TMetadata : IMetadata
{ {
/// <summary> /// <summary>
/// 执行顺序 /// 执行顺序
@ -17,27 +16,25 @@ public abstract class Feature<TMetadata>
public string Name { get; init; } = String.Empty; 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); Execute(metadata);
return ValueTask.CompletedTask; return ValueTask.CompletedTask;
} }
public virtual void OnException(FeatureExceptionContext<TMetadata> context) public virtual void OnException(FeatureExceptionContext context)
{ {
throw context.Exception; throw context.Exception;
} }
public virtual ValueTask OnExceptionAsync(FeatureExceptionContext<TMetadata> context) public virtual ValueTask OnExceptionAsync(FeatureExceptionContext context)
{ {
throw context.Exception; throw context.Exception;
} }
} }
public delegate void FeatureFunction<TMetadata>(Feature<TMetadata> feature, TMetadata metadata) public delegate void FeatureFunction(Feature feature, IMetadata metadata);
where TMetadata : IMetadata;
public delegate ValueTask FeatureFunctionAsync<TMetadata>(Feature<TMetadata> feature, TMetadata metadata) public delegate ValueTask FeatureFunctionAsync(Feature feature, IMetadata metadata);
where TMetadata : IMetadata;

View File

@ -2,15 +2,14 @@ using Demo.Models;
namespace Demo.Features; namespace Demo.Features;
public class FeatureException<TMetadata> : Exception public class FeatureException : Exception
where TMetadata : IMetadata
{ {
public TMetadata Metadata { get; } public IMetadata Metadata { get; }
public Feature<TMetadata> Feature { get; } public Feature Feature { get; }
private Exception _innerException; private Exception _innerException;
public FeatureException(TMetadata metadata, Feature<TMetadata> feature, Exception ex) : base(ex.Message) public FeatureException(IMetadata metadata, Feature feature, Exception ex) : base(ex.Message)
{ {
Metadata = metadata; Metadata = metadata;
Feature = feature; Feature = feature;

View File

@ -5,12 +5,11 @@ namespace Demo.Features;
/// <summary> /// <summary>
/// 功能运行时异常上下文 /// 功能运行时异常上下文
/// </summary> /// </summary>
public class FeatureExceptionContext<TMetadata> public class FeatureExceptionContext
where TMetadata : IMetadata
{ {
public Feature<TMetadata> Feature { get; } public Feature Feature { get; }
public FeatureException<TMetadata> Exception { get; } public FeatureException Exception { get; }
public FeatureExceptionContext(Feature<TMetadata> feature, FeatureException<TMetadata> exception) public FeatureExceptionContext(Feature feature, FeatureException exception)
{ {
Feature = feature; Feature = feature;
Exception = exception; Exception = exception;

View File

@ -7,11 +7,11 @@ namespace Demo.Features;
/// 功能列表容器 /// 功能列表容器
/// </summary> /// </summary>
/// <typeparam name="TMetadata"></typeparam> /// <typeparam name="TMetadata"></typeparam>
public class FeatureList<TMetadata> : Dictionary<string, Feature<TMetadata>> public class FeatureList<TMetadata> : Dictionary<string, Feature>
where TMetadata : IMetadata where TMetadata : IMetadata
{ {
public FeatureList(IDictionary<string, Feature<TMetadata>>? list = null) public FeatureList(IDictionary<string, Feature>? list = null)
: base(list ?? new Dictionary<string, Feature<TMetadata>>()) : base(list ?? new Dictionary<string, Feature>())
{ {
} }
} }

View File

@ -34,7 +34,7 @@ public class FeatureWrapper<TMetadata>
/// <typeparam name="TFeature"></typeparam> /// <typeparam name="TFeature"></typeparam>
/// <returns></returns> /// <returns></returns>
public FeatureWrapper<TMetadata> WithFeature<TFeature>(TFeature feature) public FeatureWrapper<TMetadata> WithFeature<TFeature>(TFeature feature)
where TFeature : Feature<TMetadata> where TFeature : Feature
{ {
if (feature is null) if (feature is null)
{ {
@ -57,7 +57,7 @@ public class FeatureWrapper<TMetadata>
/// <param name="feature"></param> /// <param name="feature"></param>
/// <param name="order"></param> /// <param name="order"></param>
/// <returns></returns> /// <returns></returns>
public FeatureWrapper<TMetadata> WithFeature(string name, FeatureFunction<TMetadata> feature, int order = 1) public FeatureWrapper<TMetadata> WithFeature(string name, FeatureFunction feature, int order = 1)
{ {
if (String.IsNullOrWhiteSpace(name)) if (String.IsNullOrWhiteSpace(name))
{ {
@ -65,7 +65,7 @@ public class FeatureWrapper<TMetadata>
} }
if (feature is not null) if (feature is not null)
{ {
Features.TryAdd(name, new DefaultFeature<TMetadata>(name, feature, order: order)); Features.TryAdd(name, new DefaultFeature(name, feature, order: order));
} }
return this; return this;
@ -78,7 +78,7 @@ public class FeatureWrapper<TMetadata>
/// <param name="feature"></param> /// <param name="feature"></param>
/// <param name="order"></param> /// <param name="order"></param>
/// <returns></returns> /// <returns></returns>
public FeatureWrapper<TMetadata> WithFeature(string name, FeatureFunctionAsync<TMetadata> feature, int order = 1) public FeatureWrapper<TMetadata> WithFeature(string name, FeatureFunctionAsync feature, int order = 1)
{ {
if (String.IsNullOrWhiteSpace(name)) if (String.IsNullOrWhiteSpace(name))
{ {
@ -86,13 +86,13 @@ public class FeatureWrapper<TMetadata>
} }
if (feature is not null) if (feature is not null)
{ {
Features.TryAdd(name, new DefaultFeature<TMetadata>(name, executeFunctionAsync: feature, order: order)); Features.TryAdd(name, new DefaultFeature(name, executeFunctionAsync: feature, order: order));
} }
return this; return this;
} }
public void Execute() public void ExecuteAll()
{ {
foreach (var feature in Features.Values.OrderBy(f => f.Order)) foreach (var feature in Features.Values.OrderBy(f => f.Order))
{ {
@ -106,7 +106,7 @@ public class FeatureWrapper<TMetadata>
} }
catch (Exception ex) catch (Exception ex)
{ {
feature.OnException(new FeatureExceptionContext<TMetadata>(feature, new FeatureException<TMetadata>(_metadata, feature, ex))); feature.OnException(new FeatureExceptionContext(feature, new FeatureException(_metadata, feature, ex)));
} }
} }
} }
@ -123,7 +123,7 @@ public class FeatureWrapper<TMetadata>
} }
} }
public async ValueTask ExecuteAsync() public async ValueTask ExecuteAllAsync()
{ {
foreach (var feature in Features.Values.OrderBy(f => f.Order)) foreach (var feature in Features.Values.OrderBy(f => f.Order))
{ {
@ -137,7 +137,7 @@ public class FeatureWrapper<TMetadata>
} }
catch (Exception ex) catch (Exception ex)
{ {
await feature.OnExceptionAsync(new FeatureExceptionContext<TMetadata>(feature, new FeatureException<TMetadata>(_metadata, feature, ex))); await feature.OnExceptionAsync(new FeatureExceptionContext(feature, new FeatureException(_metadata, feature, ex)));
} }
} }
} }

View File

@ -5,6 +5,7 @@ namespace Demo.Models;
public abstract class Chip : IMetadata public abstract class Chip : IMetadata
{ {
public abstract Guid SignId { get; }
/// <summary> /// <summary>
/// 属性 /// 属性
/// </summary> /// </summary>

View File

@ -7,8 +7,22 @@ namespace Demo.Models;
/// </summary> /// </summary>
public interface IMetadata public interface IMetadata
{ {
string Name { get; }
Guid SignId { get; }
/// <summary> /// <summary>
/// 属性 /// 属性
/// </summary> /// </summary>
MetadataPropertySet Properties { get; } MetadataPropertySet Properties { get; }
public IMetadata SetProperty(string name, object? value)
{
Properties.SetValue(name, value);
return this;
}
public TValue? GetProperty<TValue>(string name)
{
return Properties.GetValue(name).Cast<TValue>();
}
} }

View File

@ -5,11 +5,11 @@ try
IcndChip icndChip = new IcndChip(); IcndChip icndChip = new IcndChip();
icndChip.SetProperty("Code", "1234") icndChip.SetProperty("Code", "1234")
.SetProperty("Name", "芯片"); .SetProperty("Name", "芯片");
icndChip.Features.Execute(); // 执行所有功能 icndChip.Features.ExecuteAll(); // 执行所有功能
icndChip.Features icndChip.Features
.DropFeature("场频自适应功能") .DropFeature("场频自适应功能")
.Execute(); .ExecuteAll();
icndChip.Features.Execute("场频自适应功能"); icndChip.Features.Execute("场频自适应功能");
@ -26,6 +26,8 @@ catch (Exception ex)
} }
public class IcndChip : Chip public class IcndChip : Chip
{ {
public override Guid SignId => Guid.Parse("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4");
protected override void Initialize() protected override void Initialize()
{ {
Name = "ICND芯片"; Name = "ICND芯片";