using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; namespace Hyperbar.Widget; public static class IServiceCollectionExtensions { public static IServiceCollection AddWidget(this IServiceCollection services) { services.AddTransient(); services.AddTransient, WidgetFactory>(); services.AddHandler(); services.AddHandler(); services.AddHandler(); services.AddHandler(); return services; } public static IServiceCollection AddWidgetTemplate(this IServiceCollection services) where TWidgetContent : IWidgetViewModel { Type contentType = typeof(TWidgetContent); Type templateType = typeof(IWidgetView); string key = contentType.Name; services.AddTransient(typeof(IWidgetViewModel), contentType); services.TryAddTransient(templateType, provider => provider.GetService()!); services.AddKeyedTransient(typeof(IWidgetViewModel), key, contentType); services.TryAddKeyedTransient(key, (provider, key) => provider.GetService()!); services.AddTransient(provider => new ContentTemplateDescriptor { ContentType = contentType, TemplateType = templateType, Key = key }); return services; } public static IServiceCollection AddWidgetTemplate(this IServiceCollection services) where TWidgetContent : IWidgetViewModel { Type contentType = typeof(TWidgetContent); Type templateType = typeof(TWidgetTemplate); string key = contentType.Name; services.AddTransient(typeof(IWidgetViewModel), contentType); services.TryAddTransient(templateType); services.AddKeyedTransient(typeof(IWidgetViewModel), key, contentType); services.TryAddKeyedTransient(templateType, key); services.AddTransient(provider => new ContentTemplateDescriptor { ContentType = contentType, TemplateType = templateType, Key = key }); return services; } }