From 9ee6c1180dac8bc411e5048e4536d6e731216187 Mon Sep 17 00:00:00 2001 From: "H. Utku Maden" Date: Fri, 14 Nov 2025 22:28:21 +0300 Subject: [PATCH] Add new functions to extension system. --- Dashboard.Common/Pal/Application.cs | 17 +++++++++++++++-- Dashboard.Common/Pal/DeviceContext.cs | 20 ++++++++++++++++---- Dashboard.Common/Pal/IContextBase.cs | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/Dashboard.Common/Pal/Application.cs b/Dashboard.Common/Pal/Application.cs index 2780248..453c3a7 100644 --- a/Dashboard.Common/Pal/Application.cs +++ b/Dashboard.Common/Pal/Application.cs @@ -108,6 +108,11 @@ namespace Dashboard.Pal return _extensions.Contains() || _preloadedExtensions.Contains(); } + public bool ExtensionPreload(Func loader) where T : IApplicationExtension + { + return _preloadedExtensions.Add(loader); + } + public bool ExtensionPreload() where T : IApplicationExtension, new() { return _preloadedExtensions.Add(() => new T()); @@ -131,8 +136,7 @@ namespace Dashboard.Pal } else { - extension = (T?)typeof(T).GetConstructor(BindingFlags.Public, [])?.Invoke([]) ?? - throw new Exception("Constructor not found."); + extension = Activator.CreateInstance(); } _extensions.Add(extension); @@ -142,6 +146,15 @@ namespace Dashboard.Pal return extension; } + public bool ExtensionLoad(T instance) where T : IApplicationExtension + { + if (_extensions.Contains(instance)) + return false; + + _extensions.Add(instance); + return true; + } + protected virtual void Dispose(bool isDisposing) { if (!isDisposing) return; diff --git a/Dashboard.Common/Pal/DeviceContext.cs b/Dashboard.Common/Pal/DeviceContext.cs index f83adac..acf04eb 100644 --- a/Dashboard.Common/Pal/DeviceContext.cs +++ b/Dashboard.Common/Pal/DeviceContext.cs @@ -10,7 +10,7 @@ namespace Dashboard.Pal private readonly TypeDictionary> _preloadedExtensions = new TypeDictionary>(true); - private Dictionary _attributes = new Dictionary(); + private readonly Dictionary _attributes = new Dictionary(); public abstract string DriverName { get; } @@ -40,6 +40,11 @@ namespace Dashboard.Pal return _extensions.Contains() || _preloadedExtensions.Contains(); } + public bool ExtensionPreload(Func loader) where T : IDeviceContextExtension + { + return _preloadedExtensions.Add(loader); + } + public bool ExtensionPreload() where T : IDeviceContextExtension, new() { return _preloadedExtensions.Add(() => new T()); @@ -63,9 +68,7 @@ namespace Dashboard.Pal } else { - extension =(T)( - typeof(T).GetConstructor(BindingFlags.Public, []) ?.Invoke([]) - ?? throw new Exception("Could not find a suitable constructor for the given extension.")); + extension = Activator.CreateInstance(); } _extensions.Add(extension); @@ -75,6 +78,15 @@ namespace Dashboard.Pal return extension; } + public bool ExtensionLoad(T instance) where T : IDeviceContextExtension + { + if (_extensions.Contains(instance)) + return false; + + _extensions.Add(instance); + return true; + } + public void SetAttribute(string name, object? v) { if (v != null) diff --git a/Dashboard.Common/Pal/IContextBase.cs b/Dashboard.Common/Pal/IContextBase.cs index b9b6c00..04154f7 100644 --- a/Dashboard.Common/Pal/IContextBase.cs +++ b/Dashboard.Common/Pal/IContextBase.cs @@ -58,12 +58,31 @@ namespace Dashboard.Pal /// bool ExtensionPreload() where T : TExtension, new(); + /// + /// Preload extensions, to be lazy loaded when required. + /// + /// The loader delegate. + /// The extension to preload. + /// + /// True if the extension was added to the preload set. Otherwise, already loaded or another extension + /// exists which provides this. + /// + bool ExtensionPreload(Func loader) where T : TExtension; + /// /// Require an extension. /// /// The extension to require. /// The extension instance. T ExtensionRequire() where T : TExtension; + + /// + /// Load an extension. + /// + /// The extension instance. + /// The extension to require. + /// True if the extension was loaded, false if there was already one. + bool ExtensionLoad(T instance) where T : TExtension; } ///