Class PluginServiceBase
- Namespace
- SharpConsoleUI.Plugins
- Assembly
- SharpConsoleUI.dll
Abstract base class for implementing service plugins. This class simplifies the implementation of IPluginService by providing helper methods for registering operations and handling parameter extraction.
public abstract class PluginServiceBase : IPluginService
- Inheritance
-
PluginServiceBase
- Implements
- Inherited Members
Remarks
Plugin authors can inherit from this class and use the RegisterOperation methods in their constructor or initialization code to define available operations. The Execute method is automatically implemented to dispatch to registered handlers.
Example:
public class MyService : PluginServiceBase
{
public override string ServiceName => "MyService";
public override string Description => "My custom service";
public MyService()
{
RegisterOperation("GetValue", "Gets a value", () => GetValue());
RegisterOperation("SetValue", "Sets a value",
new[] { new ServiceParameter("value", typeof(int), true) },
(p) => SetValue(GetParameter<int>(p, "value")));
}
private int GetValue() => 42;
private void SetValue(int value) { /* ... */ }
}
Properties
Description
Gets a human-readable description of what this service provides.
public abstract string Description { get; }
Property Value
ServiceName
Gets the unique name of this service. This is used to retrieve the service from the plugin system.
public abstract string ServiceName { get; }
Property Value
Examples
"Diagnostics", "Logger", "Authentication"
Methods
Execute(string, Dictionary<string, object>?)
Executes a named operation with optional parameters.
public object? Execute(string operationName, Dictionary<string, object>? parameters = null)
Parameters
operationNamestringThe name of the operation to execute
parametersDictionary<string, object>Optional dictionary of parameter name/value pairs
Returns
- object
The result of the operation, or null if the operation returns void
Remarks
Parameter values should match the types declared in the operation metadata. The caller is responsible for casting the return value to the expected type based on the operation metadata.
Exceptions
- InvalidOperationException
Thrown if the operation name is unknown or parameters are invalid
GetAvailableOperations()
Gets the list of operations this service supports, with full metadata about parameters, return types, and descriptions. This enables runtime discovery and self-documenting service interfaces.
public IReadOnlyList<ServiceOperation> GetAvailableOperations()
Returns
- IReadOnlyList<ServiceOperation>
A read-only list of operation metadata
GetParameter<T>(Dictionary<string, object>?, string)
Helper method to extract a required parameter from the parameters dictionary. Throws an exception if the parameter is missing or has the wrong type.
protected static T GetParameter<T>(Dictionary<string, object>? parameters, string name)
Parameters
parametersDictionary<string, object>The parameters dictionary
namestringThe parameter name
Returns
- T
The parameter value
Type Parameters
TThe expected parameter type
Exceptions
- InvalidOperationException
Thrown if the parameter is missing or invalid
GetParameter<T>(Dictionary<string, object>?, string, T)
Helper method to extract an optional parameter from the parameters dictionary. Returns the default value if the parameter is missing.
protected static T GetParameter<T>(Dictionary<string, object>? parameters, string name, T defaultValue)
Parameters
parametersDictionary<string, object>The parameters dictionary
namestringThe parameter name
defaultValueTThe default value to return if the parameter is missing
Returns
- T
The parameter value or the default value
Type Parameters
TThe expected parameter type
RegisterOperation(string, string, Action)
Registers an operation with no parameters and no return value.
protected void RegisterOperation(string name, string description, Action handler)
Parameters
namestringThe operation name
descriptionstringHuman-readable description
handlerActionThe handler function
RegisterOperation(string, string, IReadOnlyList<ServiceParameter>, Func<Dictionary<string, object>?, object?>, Type?)
Registers an operation with parameters and an optional return value. The handler receives a Dictionary of parameters and is responsible for extracting and validating them.
protected void RegisterOperation(string name, string description, IReadOnlyList<ServiceParameter> parameters, Func<Dictionary<string, object>?, object?> handler, Type? returnType = null)
Parameters
namestringThe operation name
descriptionstringHuman-readable description
parametersIReadOnlyList<ServiceParameter>Parameter metadata
handlerFunc<Dictionary<string, object>, object>The handler function that receives parameters
returnTypeTypeThe return type, or null for void operations
RegisterOperation<TResult>(string, string, Func<TResult>)
Registers an operation with no parameters that returns a value.
protected void RegisterOperation<TResult>(string name, string description, Func<TResult> handler)
Parameters
namestringThe operation name
descriptionstringHuman-readable description
handlerFunc<TResult>The handler function
Type Parameters
TResultThe return type