Make vault storages work

This commit is contained in:
TheXamlGuy
2024-04-28 16:04:05 +01:00
parent bc55c4649b
commit 7e980dbfce
6 changed files with 168 additions and 65 deletions
+30
View File
@@ -0,0 +1,30 @@
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public class Component :
IComponent
{
private readonly IComponentBuilder builder;
protected Component(IComponentBuilder builder)
{
this.builder = builder;
}
public static TComponent? Create<TComponent>(IServiceProvider provider,
Action<IComponentBuilder> builderDelegate)
where TComponent : class, IComponent
{
if (provider.GetRequiredService<IServiceFactory>() is IServiceFactory factory)
{
IComponentBuilder builder = ComponentBuilder.Create();
builderDelegate(builder);
return factory.Create<TComponent>(builder);
}
return default ;
}
public virtual IComponentBuilder Create() => builder;
}
+57
View File
@@ -0,0 +1,57 @@
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public class ComponentFactory(IServiceProvider provider,
IProxyServiceCollection<IComponentBuilder> proxy,
IComponentScopeCollection scopes) :
IComponentFactory
{
public IComponentHost? Create<TComponent>(string name,
ComponentConfiguration configuration, Action<IServiceCollection>? servicesDelegate = null)
where TComponent : IComponent
{
if (provider.GetRequiredService<TComponent>() is TComponent component)
{
IComponentBuilder builder = component.Create();
builder.AddServices(services =>
{
services.AddTransient(_ =>
provider.GetRequiredService<IProxyService<IPublisher>>());
services.AddTransient(_ =>
provider.GetRequiredService<IProxyService<IComponentHostCollection>>());
services.AddScoped(_ =>
provider.GetRequiredService<INavigationContextCollection>());
services.AddScoped(_ =>
provider.GetRequiredService<INavigationContextProvider>());
services.AddScoped(_ =>
provider.GetRequiredService<IComponentScopeCollection>());
services.AddTransient(_ =>
provider.GetRequiredService<IComponentScopeProvider>());
services.AddRange(proxy.Services);
services.AddSingleton(new ComponentScope(name));
if (servicesDelegate is not null)
{
servicesDelegate(services);
}
});
builder.AddConfiguration(name, configuration);
IComponentHost host = builder.Build();
scopes.Add(new ComponentScopeDescriptor(name,
host.Services.GetRequiredService<IServiceProvider>()));
return host;
}
return default;
}
}
+69
View File
@@ -0,0 +1,69 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Toolkit.Foundation;
public class DefaultHostBuilder :
HostBuilder
{
public static IHostBuilder Create()
{
return new HostBuilder()
.UseContentRoot("Local", true)
.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile("Settings.json", true, true);
})
.ConfigureServices((context, services) =>
{
services.AddScoped<IServiceFactory>(provider =>
new ServiceFactory((type, parameters) => ActivatorUtilities.CreateInstance(provider, type, parameters!)));
services.AddSingleton<IComponentHostCollection,
ComponentHostCollection>();
services.AddScoped<SubscriptionCollection>();
services.AddScoped<ISubscriptionManager, SubscriptionManager>();
services.AddTransient<ISubscriber, Subscriber>();
services.AddTransient<IPublisher, Publisher>();
services.AddScoped<IMediator, Mediator>();
services.AddScoped<IProxyService<IPublisher>>(provider =>
new ProxyService<IPublisher>(provider.GetRequiredService<IPublisher>()));
services.AddScoped<IProxyService<INavigationContextProvider>>(provider =>
new ProxyService<INavigationContextProvider>(provider.GetRequiredService<INavigationContextProvider>()));
services.AddScoped<IProxyService<IComponentHostCollection>>(provider =>
new ProxyService<IComponentHostCollection>(provider.GetRequiredService<IComponentHostCollection>()));
services.AddScoped<IDisposer, Disposer>();
services.AddTransient<IContentTemplateDescriptorProvider, ContentTemplateDescriptorProvider>();
services.AddTransient<INavigationProvider, NavigationProvider>();
services.AddScoped<INavigationContextCollection, NavigationContextCollection>();
services.AddTransient<INavigationContextProvider, NavigationContextProvider>();
services.AddTransient<INavigationScope, NavigationScope>();
services.AddSingleton(new ComponentScope("Root"));
services.AddScoped<IComponentScopeCollection, ComponentScopeCollection>(provider => new ComponentScopeCollection
{
new ComponentScopeDescriptor("Root", provider.GetRequiredService<IServiceProvider>())
});
services.AddTransient<IComponentFactory, ComponentFactory>();
services.AddTransient<IComponentScopeProvider, ComponentScopeProvider>();
services.AddHandler<NavigateHandler>();
services.AddHandler<NavigateBackHandler>();
services.AddInitializer<ComponentInitializer>();
services.AddHostedService<AppService>();
});
}
}
+10
View File
@@ -0,0 +1,10 @@
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public interface IComponentFactory
{
IComponentHost? Create<TComponent>(string name,
ComponentConfiguration configuration, Action<IServiceCollection>? servicesDelegate = null)
where TComponent : IComponent;
}
@@ -8,7 +8,7 @@ using System.Text.Json;
namespace Toolkit.Foundation;
public static class Test
public static class IHostBuilderExtension
{
public static IHostBuilder AddConfiguration<TConfiguration>(this IHostBuilder builder,
string section)
@@ -149,66 +149,3 @@ public static class Test
return builder;
}
}
public class DefaultBuilder :
HostBuilder
{
public static IHostBuilder Create()
{
return new HostBuilder()
.UseContentRoot("Local", true)
.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile("Settings.json", true, true);
})
.ConfigureServices((context, services) =>
{
services.AddScoped<IServiceFactory>(provider =>
new ServiceFactory((type, parameters) => ActivatorUtilities.CreateInstance(provider, type, parameters!)));
services.AddSingleton<IComponentHostCollection,
ComponentHostCollection>();
services.AddScoped<SubscriptionCollection>();
services.AddScoped<ISubscriptionManager, SubscriptionManager>();
services.AddTransient<ISubscriber, Subscriber>();
services.AddTransient<IPublisher, Publisher>();
services.AddScoped<IMediator, Mediator>();
services.AddScoped<IProxyService<IPublisher>>(provider =>
new ProxyService<IPublisher>(provider.GetRequiredService<IPublisher>()));
services.AddScoped<IProxyService<INavigationContextProvider>>(provider =>
new ProxyService<INavigationContextProvider>(provider.GetRequiredService<INavigationContextProvider>()));
services.AddScoped<IProxyService<IComponentHostCollection>>(provider =>
new ProxyService<IComponentHostCollection>(provider.GetRequiredService<IComponentHostCollection>()));
services.AddScoped<IDisposer, Disposer>();
services.AddTransient<IContentTemplateDescriptorProvider, ContentTemplateDescriptorProvider>();
services.AddTransient<INavigationProvider, NavigationProvider>();
services.AddScoped<INavigationContextCollection, NavigationContextCollection>();
services.AddTransient<INavigationContextProvider, NavigationContextProvider>();
services.AddTransient<INavigationScope, NavigationScope>();
services.AddSingleton(new ComponentScope("Root"));
services.AddScoped<IComponentScopeCollection, ComponentScopeCollection>(provider => new ComponentScopeCollection
{
new ComponentScopeDescriptor("Root", provider.GetRequiredService<IServiceProvider>())
});
services.AddTransient<IComponentScopeProvider, ComponentScopeProvider>();
services.AddHandler<NavigateHandler>();
services.AddHandler<NavigateBackHandler>();
services.AddInitializer<ComponentInitializer>();
services.AddHostedService<AppService>();
});
}
}