Files
mixtape/zero.Core/Plugins/PluginCollection.cs
T
2020-05-13 14:15:21 +02:00

57 lines
1.3 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using System;
namespace zero.Core.Plugins
{
public class PluginCollection
{
IServiceCollection Services;
IZeroPluginConfiguration Builder;
public PluginCollection(IServiceCollection services, IZeroPluginConfiguration builder)
{
Services = services;
Builder = builder;
}
/// <summary>
/// Adds a zero plugin
/// </summary>
public void Add<T>() where T : class, IZeroPlugin, new()
{
Services.AddScoped<IZeroPlugin, T>();
AddPluginServices<T>();
}
/// <summary>
/// Adds a zero plugin
/// </summary>
public void Add<T>(Func<IServiceProvider, T> implementationFactory) where T : class, IZeroPlugin, new()
{
Services.AddScoped<IZeroPlugin, T>(implementationFactory);
AddPluginServices<T>();
}
/// <summary>
/// Creates a temporary instance of the plugin to add additional services
/// </summary>
/// <typeparam name="T"></typeparam>
void AddPluginServices<T>() where T : class, IZeroPlugin, new()
{
try
{
new T().Configure(Services, Builder);
}
catch
{
throw new Exception($"Plugin \"{nameof(T)}\" needs an additional parameterless constructor as ConfigureServices() is called before the DI container is built");
}
}
}
}