ChipDemo/Core/Features/FeatureWrapper.cs

48 lines
1.1 KiB
C#

using System.ComponentModel.Design;
using Demo.Models;
namespace Demo.Features;
public class FeatureWrapper : IFeatureWrapper
{
private readonly IMetadata _metadata;
protected FeatureList Features { get; }
public FeatureWrapper(IMetadata metadata, FeatureList? features = null)
{
_metadata = metadata;
Features = features ?? new FeatureList();
}
public void ExecuteAll()
{
foreach (var feature in Features.Values.OrderBy(f => f.Order))
{
try
{
feature.Execute(_metadata);
if (feature.Finished)
{
break;
}
}
catch (Exception ex)
{
feature.OnException(new FeatureExceptionContext(feature, new FeatureException(_metadata, feature, ex)));
}
}
}
public void Execute(string name)
{
if (Features.TryGetValue(name, out var feature))
{
feature.Execute(_metadata);
}
else
{
throw new KeyNotFoundException(name);
}
}
}