wrapper for features & reduce dup code

This commit is contained in:
2023-12-06 07:40:32 +01:00
parent 0ecbde3fd7
commit d2c8986d51
2 changed files with 42 additions and 19 deletions

View File

@@ -9,7 +9,7 @@
<PropertyGroup> <PropertyGroup>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>2.1.0</Version> <Version>2.2.0</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -17,14 +17,15 @@ namespace Pilz.Plugins.Advanced
private readonly List<PluginFeature> features = new(); private readonly List<PluginFeature> features = new();
/// <summary> /// <summary>
/// Returns a collection of all registred <see cref="PluginFeature"/> instances. /// A wrapper of all registred <see cref="PluginFeature"/> instances.
/// </summary> /// </summary>
public IReadOnlyList<PluginFeature> Features => features.AsReadOnly(); public FeatureController Features { get; init; }
/// <summary> /// <summary>
/// A wrapper for all registred <see cref="PluginModule"/> instances. /// A wrapper for all registred <see cref="PluginModule"/> instances.
/// </summary> /// </summary>
public ModuleController Modules { get; init; } public ModuleController Modules { get; init; }
/// <summary> /// <summary>
/// A wrapper for all registred <see cref="PluginFunction"/> instances. /// A wrapper for all registred <see cref="PluginFunction"/> instances.
/// </summary> /// </summary>
@@ -32,6 +33,7 @@ namespace Pilz.Plugins.Advanced
public PluginFeatureController() public PluginFeatureController()
{ {
Features = new(this);
Functions = new(this); Functions = new(this);
Modules = new(this); Modules = new(this);
} }
@@ -47,43 +49,64 @@ namespace Pilz.Plugins.Advanced
features.Remove(module); features.Remove(module);
} }
public class ModuleController public class FeatureController
{ {
private readonly PluginFeatureController controller; protected readonly PluginFeatureController controller;
public ModuleController(PluginFeatureController controller) public FeatureController(PluginFeatureController controller)
{ {
this.controller = controller; this.controller = controller;
} }
public IEnumerable<PluginModule> GetAll() public virtual IEnumerable<PluginFeature> GetAll()
{ {
return controller.features.OfType<PluginModule>(); return controller.features.AsReadOnly();
} }
public IEnumerable<PluginModule> Get(string moduleType) public virtual IEnumerable<PluginFeature> Get(string moduleType)
{
return controller.features.Where(n => n.Type == moduleType);
}
public virtual PluginFeature? GetFirst(string moduleType)
{
return controller.features.FirstOrDefault(n => n.Type == moduleType);
}
}
public class FeatureController<T> : FeatureController where T : PluginFeature
{
public FeatureController(PluginFeatureController controller) : base(controller)
{
}
public override IEnumerable<T> GetAll()
{
return controller.features.OfType<T>();
}
public override IEnumerable<T> Get(string moduleType)
{ {
return GetAll().Where(n => n.Type == moduleType); return GetAll().Where(n => n.Type == moduleType);
} }
public override T? GetFirst(string moduleType)
{
return GetAll().FirstOrDefault(n => n.Type == moduleType);
}
} }
public class FunctionController public class ModuleController : FeatureController<PluginModule>
{ {
private readonly PluginFeatureController controller; public ModuleController(PluginFeatureController controller) : base(controller)
public FunctionController(PluginFeatureController controller)
{ {
this.controller = controller; }
} }
public IEnumerable<PluginFunction> GetAll() public class FunctionController : FeatureController<PluginFunction>
{ {
return controller.features.OfType<PluginFunction>(); public FunctionController(PluginFeatureController controller) : base(controller)
}
public IEnumerable<PluginFunction> Get(string functionType)
{ {
return GetAll().Where(n => n.Type == functionType);
} }
public void ExecuteAll(string functionType) public void ExecuteAll(string functionType)