using Microsoft.Extensions.DependencyInjection; using System; namespace zero.Core.Plugins { public class PluginCollection { IServiceCollection Services; IZeroPluginBuilder Builder; public PluginCollection(IServiceCollection services, IZeroPluginBuilder builder) { Services = services; Builder = builder; } /// /// Adds a zero plugin /// public void Add() where T : class, IZeroPlugin, new() { Services.AddScoped(); AddPluginServices(); } /// /// Adds a zero plugin /// public void Add(Func implementationFactory) where T : class, IZeroPlugin, new() { Services.AddScoped(implementationFactory); AddPluginServices(); } /// /// Creates a temporary instance of the plugin to add additional services /// /// void AddPluginServices() 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"); } } } }