移除泛型设计

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>
/// <typeparam name="TMetadata"></typeparam>
public sealed class DefaultFeature<TMetadata> : Feature<TMetadata>
where TMetadata : IMetadata
public sealed class DefaultFeature : Feature
{
public DefaultFeature(string name, FeatureFunction<TMetadata>? executeFunction = null,
FeatureFunctionAsync<TMetadata>? 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<TMetadata> : Feature<TMetadata>
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);
}
public override async ValueTask ExecuteAsync(TMetadata metadata)
public override async ValueTask ExecuteAsync(IMetadata metadata)
{
if (_executeFunctionAsync is not null)
{

View File

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

View File

@ -2,15 +2,14 @@ using Demo.Models;
namespace Demo.Features;
public class FeatureException<TMetadata> : Exception
where TMetadata : IMetadata
public class FeatureException : Exception
{
public TMetadata Metadata { get; }
public Feature<TMetadata> Feature { get; }
public IMetadata Metadata { get; }
public Feature Feature { get; }
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;
Feature = feature;

View File

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

View File

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

View File

@ -34,7 +34,7 @@ public class FeatureWrapper<TMetadata>
/// <typeparam name="TFeature"></typeparam>
/// <returns></returns>
public FeatureWrapper<TMetadata> WithFeature<TFeature>(TFeature feature)
where TFeature : Feature<TMetadata>
where TFeature : Feature
{
if (feature is null)
{
@ -57,7 +57,7 @@ public class FeatureWrapper<TMetadata>
/// <param name="feature"></param>
/// <param name="order"></param>
/// <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))
{
@ -65,7 +65,7 @@ public class FeatureWrapper<TMetadata>
}
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;
@ -78,7 +78,7 @@ public class FeatureWrapper<TMetadata>
/// <param name="feature"></param>
/// <param name="order"></param>
/// <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))
{
@ -86,13 +86,13 @@ public class FeatureWrapper<TMetadata>
}
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;
}
public void Execute()
public void ExecuteAll()
{
foreach (var feature in Features.Values.OrderBy(f => f.Order))
{
@ -106,7 +106,7 @@ public class FeatureWrapper<TMetadata>
}
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))
{
@ -137,7 +137,7 @@ public class FeatureWrapper<TMetadata>
}
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 Guid SignId { get; }
/// <summary>
/// 属性
/// </summary>

View File

@ -7,8 +7,22 @@ namespace Demo.Models;
/// </summary>
public interface IMetadata
{
string Name { get; }
Guid SignId { get; }
/// <summary>
/// 属性
/// </summary>
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.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芯片";