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