Add TViewModelImplementation to add AddTemplate extension
This commit is contained in:
@@ -113,38 +113,47 @@ public static class IServiceCollectionExtensions
|
|||||||
object? key = null,
|
object? key = null,
|
||||||
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
|
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
|
||||||
params object[]? parameters)
|
params object[]? parameters)
|
||||||
|
{
|
||||||
|
return AddTemplate<TViewModel, TViewModel, TView>(services, key, serviceLifetime, parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IServiceCollection AddTemplate<TViewModel, TViewModelImplementation, TView>(this IServiceCollection services,
|
||||||
|
object? key = null,
|
||||||
|
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
|
||||||
|
params object[]? parameters)
|
||||||
{
|
{
|
||||||
Type viewModelType = typeof(TViewModel);
|
Type viewModelType = typeof(TViewModel);
|
||||||
|
Type viewModelImplementationType = typeof(TViewModelImplementation);
|
||||||
Type viewType = typeof(TView);
|
Type viewType = typeof(TView);
|
||||||
|
|
||||||
key ??= viewModelType.Name.Replace("ViewModel", "");
|
key ??= viewModelImplementationType.Name.Replace("ViewModel", "");
|
||||||
|
|
||||||
if (parameters is { Length: 0 })
|
if (parameters is { Length: 0 })
|
||||||
{
|
{
|
||||||
services.Add(new ServiceDescriptor(viewModelType, viewModelType, serviceLifetime));
|
services.Add(new ServiceDescriptor(viewModelType, viewModelImplementationType, serviceLifetime));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
services.Add(new ServiceDescriptor(viewModelType, provider =>
|
services.Add(new ServiceDescriptor(viewModelImplementationType, provider =>
|
||||||
provider.GetRequiredService<IServiceFactory>().Create<TViewModel>(parameters)!, serviceLifetime));
|
provider.GetRequiredService<IServiceFactory>().Create<TViewModelImplementation>(parameters)!, serviceLifetime));
|
||||||
}
|
}
|
||||||
|
|
||||||
services.Add(new ServiceDescriptor(viewType, viewType, serviceLifetime));
|
services.Add(new ServiceDescriptor(viewType, viewType, serviceLifetime));
|
||||||
|
|
||||||
if (parameters is { Length: 0 })
|
if (parameters is { Length: 0 })
|
||||||
{
|
{
|
||||||
services.Add(new ServiceDescriptor(viewModelType, key, viewModelType, serviceLifetime));
|
services.Add(new ServiceDescriptor(viewModelType, key, viewModelImplementationType, serviceLifetime));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
services.Add(new ServiceDescriptor(viewModelType, key, (provider, key) =>
|
services.Add(new ServiceDescriptor(viewModelImplementationType, key, (provider, key) =>
|
||||||
provider.GetRequiredService<IServiceFactory>().Create<TViewModel>(parameters)!, serviceLifetime));
|
provider.GetRequiredService<IServiceFactory>().Create<TViewModelImplementation>(parameters)!, serviceLifetime));
|
||||||
}
|
}
|
||||||
|
|
||||||
services.Add(new ServiceDescriptor(viewType, key, viewType, serviceLifetime));
|
services.Add(new ServiceDescriptor(viewType, key, viewType, serviceLifetime));
|
||||||
|
|
||||||
services.AddKeyedTransient<IContentTemplateDescriptor>(key, (provider, _) =>
|
services.AddKeyedTransient<IContentTemplateDescriptor>(key, (provider, _) =>
|
||||||
new ContentTemplateDescriptor(key, viewModelType, viewType, parameters));
|
new ContentTemplateDescriptor(key, viewModelImplementationType, viewType, parameters));
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user