Add a factory for populating view models

This commit is contained in:
TheXamlGuy
2024-01-06 19:21:38 +00:00
parent 66f65185c8
commit d5cc92e6ba
17 changed files with 86 additions and 905 deletions
@@ -101,10 +101,11 @@ public static class IServiceCollectionExtensions
where TConfiguration :
class, new()
{
_ = services.AddOptions();
_ = services.AddSingleton<IConfigureOptions<TConfiguration>>(new ConfigureNamedOptions<TConfiguration>("", args => { }));
services.AddOptions();
services.AddSingleton<IConfigureOptions<TConfiguration>>(new ConfigureNamedOptions<TConfiguration>("", args => { }));
services.AddTransient(provider => provider.GetService<IOptionsMonitor<TConfiguration>>()!.CurrentValue);
_ = services.AddTransient<IConfigurationWriter<TConfiguration>>(provider =>
services.AddTransient<IConfigurationWriter<TConfiguration>>(provider =>
{
string? jsonFilePath = null;
if (provider.GetService<IHostEnvironment>() is IHostEnvironment hostEnvironment)
@@ -127,7 +128,7 @@ public static class IServiceCollectionExtensions
return new ConfigurationWriter<TConfiguration>(jsonFilePath, section, defaultSerializerOptions);
});
_ = services.AddTransient<IWritableConfiguration<TConfiguration>, WritableConfiguration<TConfiguration>>();
services.AddTransient<IWritableConfiguration<TConfiguration>, WritableConfiguration<TConfiguration>>();
return services;
}
@@ -140,18 +141,13 @@ public static class IServiceCollectionExtensions
string key = contentType.Name;
_ = services.AddTransient(typeof(IWidgetViewModel), contentType);
services.AddTransient(typeof(IWidgetViewModel), contentType);
services.TryAddTransient(templateType, provider => provider.GetService<IWidgetView>()!);
_ = services.AddKeyedTransient(typeof(IWidgetViewModel), key, contentType);
services.AddKeyedTransient(typeof(IWidgetViewModel), key, contentType);
services.TryAddKeyedTransient(key, (provider, key) => provider.GetService<IWidgetView>()!);
_ = services.AddTransient<IContentTemplateDescriptor>(provider => new ContentTemplateDescriptor
{
ContentType = contentType,
TemplateType = templateType,
Key = key
});
services.AddTransient<IContentTemplateDescriptor>(provider => new ContentTemplateDescriptor { ContentType = contentType, TemplateType = templateType, Key = key });
return services;
}
@@ -165,18 +161,13 @@ public static class IServiceCollectionExtensions
string key = contentType.Name;
_ = services.AddTransient(typeof(IWidgetViewModel), contentType);
services.AddTransient(typeof(IWidgetViewModel), contentType);
services.TryAddTransient(templateType);
_ = services.AddKeyedTransient(typeof(IWidgetViewModel), key, contentType);
services.AddKeyedTransient(typeof(IWidgetViewModel), key, contentType);
services.TryAddKeyedTransient(templateType, key);
_ = services.AddTransient<IContentTemplateDescriptor>(provider => new ContentTemplateDescriptor
{
ContentType = contentType,
TemplateType = templateType,
Key = key
});
services.AddTransient<IContentTemplateDescriptor>(provider => new ContentTemplateDescriptor { ContentType = contentType, TemplateType = templateType, Key = key });
return services;
}
@@ -189,18 +180,13 @@ public static class IServiceCollectionExtensions
key ??= contentType.Name;
_ = services.AddTransient(contentType);
services.AddTransient(contentType);
services.TryAddTransient(templateType);
_ = services.AddKeyedTransient(contentType, key);
_ = services.AddKeyedTransient(templateType, key);
services.AddKeyedTransient(contentType, key);
services.AddKeyedTransient(templateType, key);
_ = services.AddTransient<IContentTemplateDescriptor>(provider => new ContentTemplateDescriptor
{
ContentType = contentType,
TemplateType = templateType,
Key = key
});
services.AddTransient<IContentTemplateDescriptor>(provider => new ContentTemplateDescriptor { ContentType = contentType, TemplateType = templateType, Key = key });
return services;
}
+6
View File
@@ -0,0 +1,6 @@
namespace Hyperbar;
public interface IViewModelFactory<TFrom, TTo>
{
TTo Create();
}
@@ -2,9 +2,23 @@
namespace Hyperbar;
public class ObservableCollectionViewModel<TItem>(IServiceFactory serviceFactory) :
public class ObservableCollectionViewModel<TItem> :
ObservableCollection<TItem>
{
private readonly IServiceFactory serviceFactory;
public ObservableCollectionViewModel(IServiceFactory serviceFactory)
{
this.serviceFactory = serviceFactory;
}
public ObservableCollectionViewModel(IServiceFactory serviceFactory,
IEnumerable<TItem> items)
{
this.serviceFactory = serviceFactory;
AddRange(items);
}
public TItem Add()
{
TItem? item = serviceFactory.Create<TItem>();
-10
View File
@@ -1,10 +0,0 @@
namespace Hyperbar;
public class WidgetViewModelBase(ITemplateFactory templateFactory,
IServiceFactory serviceFactory) :
ObservableCollectionViewModel<IWidgetComponentViewModel>(serviceFactory),
IWidgetViewModel,
ITemplatedViewModel
{
public ITemplateFactory TemplateFactory => templateFactory;
}