2020-05-13 14:06:11 +02:00
using Microsoft.Extensions.DependencyInjection ;
using System ;
2020-05-14 13:32:33 +02:00
using zero.Core.Options ;
2020-05-13 14:06:11 +02:00
namespace zero.Core.Plugins
{
public class PluginCollection
{
IServiceCollection Services ;
2020-05-14 13:32:33 +02:00
IZeroOptions Options ;
2020-05-13 14:06:11 +02:00
2020-05-14 13:32:33 +02:00
public PluginCollection ( IServiceCollection services , IZeroOptions options )
2020-05-13 14:06:11 +02:00
{
Services = services ;
2020-05-14 13:32:33 +02:00
Options = options ;
2020-05-13 14:06:11 +02:00
}
/// <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
{
2020-05-14 13:32:33 +02:00
new T (). Configure ( Services , Options );
2020-05-13 14:06:11 +02:00
}
catch
{
throw new Exception ( $"Plugin \" { nameof ( T )} \ " needs an additional parameterless constructor as ConfigureServices() is called before the DI container is built" );
}
}
}
}