Multiprocotol Terminalprogram (BAT)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MultiTerm/Common/StartupHelpers/ServiceExtensions.cs

22 lines
965 B

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