using CommunityToolkit.Mvvm.Messaging; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Toolkit.Foundation; public class ComponentInitializer(IEnumerable components, IProxyServiceCollection typedServices, IComponentHostCollection hosts, IComponentScopeCollection scopes, IServiceProvider provider) : IInitialization { public void Initialize() { foreach (IComponent component in components) { IComponentBuilder builder = component.Configure(); builder.AddServices(services => { services.AddTransient(_ => provider.GetRequiredService>()); services.AddTransient(_ => provider.GetRequiredService>()); services.AddScoped(_ => provider.GetRequiredService()); services.AddScoped(_ => provider.GetRequiredService()); services.AddScoped(_ => provider.GetRequiredService()); services.AddTransient(_ => provider.GetRequiredService()); services.AddRange(typedServices.Services); services.AddSingleton(new Scoped(component.GetType().Name)); }); IComponentHost host = builder.Build(); scopes.Add(new ComponentScopeDescriptor(component.GetType().Name, provider.GetRequiredService())); hosts.Add(host); host.Start(); } } }