using Microsoft.Extensions.DependencyInjection;
namespace Common.StartupHelpers;
public static class ServiceExtensions
{
///
/// Service Extension to add an to Dependency Injection.
///
/// Type of interface
/// Type of implementation
/// existing service collection
public static void AddAbstractFactory(this IServiceCollection services)
where TInterface : class
where TImplementation : class, TInterface
{
// add implementation to services collection
services.AddTransient();
// add factory function to services collection
services.AddSingleton>(x => () => x.GetService()!);
// add factory implementation to services collection
services.AddSingleton, AbstractFactory>();
}
///
/// 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 constructor of that subcontrol
services.AddSingleton>(x => () => x.GetService()!);
// add to DI: Factory for the subcontrol
services.AddSingleton, AbstractFactory>();
}
}