Plugins little refactor
This commit is contained in:
@@ -11,7 +11,7 @@ public class PluginManager
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly List<Type> _processorTypes = new();
|
||||
private readonly List<Type> _importerTypes = new();
|
||||
private readonly List<IDataExporter> _exporters = new();
|
||||
private readonly List<Type> _exporterTypes = new();
|
||||
private readonly List<IPlugin> _plugins = new();
|
||||
|
||||
public PluginManager(ILogger<PluginManager> logger, IServiceProvider serviceProvider)
|
||||
@@ -42,10 +42,11 @@ public class PluginManager
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("Loaded {ProcessorCount} processors and {ImporterCount} importers from {AssemblyCount} assemblies",
|
||||
_logger.LogInformation("Loaded {ProcessorCount} processors, {ImporterCount} importers, and {ExporterCount} exporters from {AssemblyCount} assemblies",
|
||||
_processorTypes.Count,
|
||||
_importerTypes.Count,
|
||||
dllFiles.Length); // Zmień z _plugins.Count na assemblyFiles.Length
|
||||
_exporterTypes.Count,
|
||||
dllFiles.Length);
|
||||
}
|
||||
|
||||
private void LoadPluginFromAssembly(string assemblyPath)
|
||||
@@ -70,6 +71,12 @@ public class PluginManager
|
||||
_importerTypes.Add(type);
|
||||
_logger.LogDebug("Registered importer: {Type}", type.Name); // Information -> Debug
|
||||
}
|
||||
|
||||
if (typeof(IDataExporter).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
|
||||
{
|
||||
_exporterTypes.Add(type);
|
||||
_logger.LogDebug("Registered exporter: {Type}", type.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -84,14 +91,15 @@ public class PluginManager
|
||||
{
|
||||
try
|
||||
{
|
||||
using var scope = _serviceProvider.CreateScope();
|
||||
var scope = _serviceProvider.CreateScope();
|
||||
var instance = (IDataProcessor)ActivatorUtilities.CreateInstance(scope.ServiceProvider, type);
|
||||
|
||||
if (instance.CanProcess(processorType))
|
||||
{
|
||||
var scopedProvider = _serviceProvider.CreateScope().ServiceProvider;
|
||||
return (IDataProcessor)ActivatorUtilities.CreateInstance(scopedProvider, type);
|
||||
return instance;
|
||||
}
|
||||
|
||||
scope.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -107,14 +115,15 @@ public class PluginManager
|
||||
{
|
||||
try
|
||||
{
|
||||
using var scope = _serviceProvider.CreateScope();
|
||||
var scope = _serviceProvider.CreateScope();
|
||||
var instance = (IDataImporter)ActivatorUtilities.CreateInstance(scope.ServiceProvider, type);
|
||||
|
||||
if (instance.CanImport(importerType))
|
||||
{
|
||||
var scopedProvider = _serviceProvider.CreateScope().ServiceProvider;
|
||||
return (IDataImporter)ActivatorUtilities.CreateInstance(scopedProvider, type);
|
||||
return instance;
|
||||
}
|
||||
|
||||
scope.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -126,7 +135,27 @@ public class PluginManager
|
||||
|
||||
public IDataExporter? GetExporter(string exporterType)
|
||||
{
|
||||
return _exporters.FirstOrDefault(e => e.CanExport(exporterType));
|
||||
foreach (var type in _exporterTypes)
|
||||
{
|
||||
try
|
||||
{
|
||||
var scope = _serviceProvider.CreateScope();
|
||||
var instance = (IDataExporter)ActivatorUtilities.CreateInstance(scope.ServiceProvider, type);
|
||||
|
||||
if (instance.CanExport(exporterType))
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
|
||||
scope.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to create exporter instance of type {Type}", type.Name);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public int GetPluginsCount() => _processorTypes.Count + _importerTypes.Count + _exporters.Count;
|
||||
|
||||
public int GetPluginsCount() => _processorTypes.Count + _importerTypes.Count + _exporterTypes.Count;
|
||||
}
|
||||
Reference in New Issue
Block a user