using Microsoft.Extensions.DependencyInjection; namespace Common.StartupHelpers; public static class ServiceExtensions { /// /// Adds a factory for subcontrols to be created. /// For example a UserControl can be instanciated using this Service. /// /// type of the subcontrol /// existing service collection public static void AddSubControlFactory(this IServiceCollection services) where TSubControl : class { // add to DI: the subcontrol itself services.AddTransient(); // add to DI: Func of that subcontrol, that will create the subcontrol services.AddSingleton>(x => () => x.GetService()!); // add to DI: Factory for the subcontrol services.AddSingleton, AbstractFactory>(); } }