From d2c8986d5139879612e36d5327b81b915f1d9305 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Wed, 6 Dec 2023 07:40:32 +0100 Subject: [PATCH] wrapper for features & reduce dup code --- .../Pilz.Plugins.Advanced.csproj | 2 +- .../PluginFeatureController.cs | 59 +++++++++++++------ 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/Pilz.Plugins.Advanced/Pilz.Plugins.Advanced.csproj b/Pilz.Plugins.Advanced/Pilz.Plugins.Advanced.csproj index 46d765a..11249c5 100644 --- a/Pilz.Plugins.Advanced/Pilz.Plugins.Advanced.csproj +++ b/Pilz.Plugins.Advanced/Pilz.Plugins.Advanced.csproj @@ -9,7 +9,7 @@ True - 2.1.0 + 2.2.0 diff --git a/Pilz.Plugins.Advanced/PluginFeatureController.cs b/Pilz.Plugins.Advanced/PluginFeatureController.cs index c301a13..2ff7567 100644 --- a/Pilz.Plugins.Advanced/PluginFeatureController.cs +++ b/Pilz.Plugins.Advanced/PluginFeatureController.cs @@ -17,14 +17,15 @@ namespace Pilz.Plugins.Advanced private readonly List features = new(); /// - /// Returns a collection of all registred instances. + /// A wrapper of all registred instances. /// - public IReadOnlyList Features => features.AsReadOnly(); + public FeatureController Features { get; init; } /// /// A wrapper for all registred instances. /// public ModuleController Modules { get; init; } + /// /// A wrapper for all registred instances. /// @@ -32,6 +33,7 @@ namespace Pilz.Plugins.Advanced public PluginFeatureController() { + Features = new(this); Functions = new(this); Modules = new(this); } @@ -47,43 +49,64 @@ namespace Pilz.Plugins.Advanced 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; } - public IEnumerable GetAll() + public virtual IEnumerable GetAll() { - return controller.features.OfType(); + return controller.features.AsReadOnly(); } - public IEnumerable Get(string moduleType) + public virtual IEnumerable Get(string moduleType) { - return GetAll().Where(n => n.Type == 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 FunctionController + public class FeatureController : FeatureController where T : PluginFeature { - private readonly PluginFeatureController controller; - - public FunctionController(PluginFeatureController controller) + public FeatureController(PluginFeatureController controller) : base(controller) { - this.controller = controller; } - public IEnumerable GetAll() + public override IEnumerable GetAll() { - return controller.features.OfType(); + return controller.features.OfType(); } - public IEnumerable Get(string functionType) + public override IEnumerable Get(string moduleType) + { + return GetAll().Where(n => n.Type == moduleType); + } + + public override T? GetFirst(string moduleType) + { + return GetAll().FirstOrDefault(n => n.Type == moduleType); + } + } + + public class ModuleController : FeatureController + { + public ModuleController(PluginFeatureController controller) : base(controller) + { + } + } + + public class FunctionController : FeatureController + { + public FunctionController(PluginFeatureController controller) : base(controller) { - return GetAll().Where(n => n.Type == functionType); } public void ExecuteAll(string functionType)