code sweep

This commit is contained in:
Daniel Clark
2022-12-10 16:11:07 +00:00
parent 4f243eba2e
commit 0da4a37173
84 changed files with 2040 additions and 2104 deletions
@@ -1,3 +1,3 @@
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public record ConfigurationChanged<TConfiguration>(TConfiguration Configuration) where TConfiguration : class;
public record ConfigurationChanged<TConfiguration>(TConfiguration Configuration) where TConfiguration : class;
@@ -1,6 +1,6 @@
using Mediator;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public class ConfigurationInitializer<TConfiguration> : IInitializable where TConfiguration : class, new()
{
@@ -19,4 +19,4 @@ public class ConfigurationInitializer<TConfiguration> : IInitializable where TCo
await mediator.Send(configuration);
await Task.CompletedTask;
}
}
}
@@ -2,7 +2,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public class ConfigurationWriter<TConfiguration> : IConfigurationWriter<TConfiguration> where TConfiguration : class, new()
{
@@ -1,6 +1,6 @@
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public interface IConfigurationWriter<TConfiguration> where TConfiguration : class
{
void Write(string section, TConfiguration args);
}
}
@@ -1,6 +1,6 @@
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public interface IWritableConfigurationProvider
{
void Write<TValue>(string section, TValue value) where TValue : class, new();
}
}
@@ -1,4 +1,4 @@
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public interface IWritableJsonConfigurationBuilder
{
@@ -9,4 +9,4 @@ public interface IWritableJsonConfigurationBuilder
IWritableJsonConfigurationBuilder AddDefaultFileStream(Stream stream);
void Build(string path);
}
}
@@ -1,8 +1,8 @@
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public interface IWritableJsonConfigurationDescriptor
{
Type ConfigurationType { get; }
string Key { get; }
}
}
@@ -1,16 +1,13 @@
using Json.Patch;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public class WritableJsonConfigurationBuilder : IWritableJsonConfigurationBuilder
{
@@ -105,4 +102,4 @@ public class WritableJsonConfigurationBuilder : IWritableJsonConfigurationBuilde
content = null;
return false;
}
}
}
@@ -1,3 +1,3 @@
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public record WritableJsonConfigurationDescriptor(Type ConfigurationType, string Key) : IWritableJsonConfigurationDescriptor;
public record WritableJsonConfigurationDescriptor(Type ConfigurationType, string Key) : IWritableJsonConfigurationDescriptor;
@@ -1,7 +1,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public static class WritableJsonConfigurationExtensions
{
@@ -71,4 +71,4 @@ public static class WritableJsonConfigurationExtensions
}
public static IConfigurationBuilder AddWritableJsonFile(this IConfigurationBuilder builder, Action<WritableJsonConfigurationSource> configureSource) => builder.Add(configureSource);
}
}
@@ -3,7 +3,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
internal class WritableJsonConfigurationFile
{
@@ -145,4 +145,4 @@ internal class WritableJsonConfigurationFile
throw new FormatException();
}
}
}
}
@@ -3,13 +3,12 @@ using Microsoft.Extensions.FileProviders;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public class WritableJsonConfigurationProvider : JsonConfigurationProvider, IWritableConfigurationProvider
{
public WritableJsonConfigurationProvider(JsonConfigurationSource source) : base(source)
{
}
public void Write<TValue>(string section, TValue value) where TValue : class, new()
@@ -43,4 +42,4 @@ public class WritableJsonConfigurationProvider : JsonConfigurationProvider, IWri
document.WriteTo(writer);
}
}
}
}
@@ -1,8 +1,8 @@
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.FileProviders;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public class WritableJsonConfigurationSource : JsonConfigurationSource
{
@@ -24,4 +24,4 @@ public class WritableJsonConfigurationSource : JsonConfigurationSource
Factory?.Build(outputFile);
}
}
}
}
+2 -2
View File
@@ -1,5 +1,5 @@
using Mediator;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public abstract record Write<TConfiguration>(string Section, Action<TConfiguration> UpdateDelegate) : IRequest where TConfiguration : class;
public abstract record Write<TConfiguration>(string Section, Action<TConfiguration> UpdateDelegate) : IRequest where TConfiguration : class;
@@ -1,6 +1,6 @@
using Mediator;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public class WriteHandler<TConfiguration> : IRequestHandler<Write<TConfiguration>> where TConfiguration : class
{
@@ -26,4 +26,4 @@ public class WriteHandler<TConfiguration> : IRequestHandler<Write<TConfiguration
return default;
}
}
}
@@ -1,6 +1,6 @@
using Microsoft.Extensions.Hosting;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public static class IHostBuilderExtensions
{
@@ -13,4 +13,4 @@ public static class IHostBuilderExtensions
return hostBuilder.UseContentRoot(contentRoot);
}
}
}
@@ -1,7 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public static class IServiceCollectionExtensions
{
@@ -11,7 +11,6 @@ public static class IServiceCollectionExtensions
return serviceCollection;
}
public static IServiceCollection AddFoundation(this IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<IServiceFactory>(provider => new ServiceFactory(provider.GetService, (instanceType, parameters) => ActivatorUtilities.CreateInstance(provider, instanceType, parameters!)))
@@ -27,4 +26,4 @@ public static class IServiceCollectionExtensions
return serviceCollection;
}
}
}
@@ -1,4 +1,4 @@
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public static class IServiceFactoryExtensions
{
@@ -12,4 +12,4 @@ public static class IServiceFactoryExtensions
ServiceFactoryDescriptor? descriptor = new(serviceFactory);
return descriptor.Create(type);
}
}
}
@@ -1,7 +1,7 @@
using Mediator;
using Microsoft.Extensions.Hosting;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public class AppService : IHostedService
{
@@ -22,4 +22,4 @@ public class AppService : IHostedService
{
return Task.CompletedTask;
}
}
}
+2 -3
View File
@@ -1,6 +1,5 @@
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public interface ICache
{
}
}
@@ -1,6 +1,6 @@
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public interface IInitializable
{
Task InitializeAsync();
}
}
@@ -1,6 +1,6 @@
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public interface IInitialization
{
Task InitializeAsync();
}
}
@@ -1,6 +1,6 @@
using System.Diagnostics;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public class Initialization : IInitialization
{
@@ -23,4 +23,4 @@ public class Initialization : IInitialization
}
}
}
}
}
@@ -1,5 +1,5 @@
using Mediator;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public record Initialize : IRequest;
public record Initialize : IRequest;
@@ -1,6 +1,6 @@
using Mediator;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public class InitializeHandler : IRequestHandler<Initialize>
{
@@ -16,4 +16,4 @@ public class InitializeHandler : IRequestHandler<Initialize>
await initialization.InitializeAsync();
return default;
}
}
}
@@ -1,5 +1,5 @@
using Mediator;
namespace Toolkit.Foundation;
namespace Toolkit.Framework.Foundation;
public record class Initialized : IRequest;
public record class Initialized : IRequest;
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
namespace Toolkit.Foundation;
namespace Toolkit.Foundation
public interface IEventParameter
{
public interface IEventParameter
{
List<object> GetValues(EventArgs args);
}
List<object> GetValues(EventArgs args);
}
@@ -1,13 +1,11 @@
namespace Toolkit.Foundation
{
public interface INavigationConfirmation
{
ValueTask<bool> CanConfirm();
}
namespace Toolkit.Foundation;
public interface INavigated
{
ValueTask Navigated();
}
public interface INavigationConfirmation
{
ValueTask<bool> CanConfirm();
}
public interface INavigated
{
ValueTask Navigated();
}
@@ -1,9 +1,8 @@
namespace Toolkit.Foundation
{
public interface INavigationRouteDescriptor
{
object Route { get; }
namespace Toolkit.Framework.Foundation;
string? Name { get; }
}
public interface INavigationRouteDescriptor
{
object Route { get; }
string? Name { get; }
}
@@ -1,8 +1,5 @@
using System.Collections.Generic;
namespace Toolkit.Framework.Foundation;
namespace Toolkit.Foundation
public interface INavigationRouteDescriptorCollection : IList<INavigationRouteDescriptor>
{
public interface INavigationRouteDescriptorCollection : IList<INavigationRouteDescriptor>
{
}
}
@@ -1,9 +1,8 @@
namespace Toolkit.Foundation
{
public interface IParameter
{
string? Key { get; }
namespace Toolkit.Framework.Foundation;
KeyValuePair<string, object>? GetValue(object target);
}
public interface IParameter
{
string? Key { get; }
KeyValuePair<string, object>? GetValue(object target);
}
+22 -23
View File
@@ -1,29 +1,28 @@
using Mediator;
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public record Navigate : IRequest
{
public record Navigate : IRequest
public Navigate(string name, params object?[] parameters)
{
public Navigate(string name, params object?[] parameters)
{
Name = name;
Parameters = parameters;
}
public Navigate(Type type, params object?[] parameters)
{
Type = type;
Parameters = parameters;
}
public Type? Type { get; }
public object? Route { get; init; }
public string? Name { get; }
public string? FriendlyName { get; init; }
public object?[] Parameters { get; }
Name = name;
Parameters = parameters;
}
public Navigate(Type type, params object?[] parameters)
{
Type = type;
Parameters = parameters;
}
public Type? Type { get; }
public object? Route { get; init; }
public string? Name { get; }
public string? FriendlyName { get; init; }
public object?[] Parameters { get; }
}
@@ -1,7 +1,5 @@
using Mediator;
namespace Toolkit.Foundation
{
public record NavigateBack(object Route) : IRequest;
namespace Toolkit.Framework.Foundation;
}
public record NavigateBack(object Route) : IRequest;
@@ -1,6 +1,5 @@
using Mediator;
namespace Toolkit.Foundation
{
public record NavigationRoute(string Name, object Route) : IRequest;
}
namespace Toolkit.Framework.Foundation;
public record NavigationRoute(string Name, object Route) : IRequest;
@@ -1,15 +1,14 @@
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public record NavigationRouteDescriptor : INavigationRouteDescriptor
{
public record NavigationRouteDescriptor : INavigationRouteDescriptor
public NavigationRouteDescriptor(string name, object route)
{
public NavigationRouteDescriptor(string name, object route)
{
Name = name;
Route = route;
}
public string Name { get; }
public object Route { get; }
Name = name;
Route = route;
}
public string Name { get; }
public object Route { get; }
}
@@ -1,9 +1,8 @@
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public class NavigationRouteDescriptorCollection : List<INavigationRouteDescriptor>, INavigationRouteDescriptorCollection
{
public class NavigationRouteDescriptorCollection : List<INavigationRouteDescriptor>, INavigationRouteDescriptorCollection
public NavigationRouteDescriptorCollection(IEnumerable<INavigationRouteDescriptor> collection) : base(collection)
{
public NavigationRouteDescriptorCollection(IEnumerable<INavigationRouteDescriptor> collection) : base(collection)
{
}
}
}
@@ -1,7 +1,6 @@
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public interface IServiceCreator<T>
{
public interface IServiceCreator<T>
{
object Create(Func<Type, object[], object> creator, params object[] parameters);
}
}
object Create(Func<Type, object[], object> creator, params object[] parameters);
}
@@ -1,9 +1,8 @@
namespace Toolkit.Foundation
{
public interface IServiceFactory
{
object? Create(Type type, params object?[] parameters);
namespace Toolkit.Framework.Foundation;
T? Create<T>(Type type, params object?[] parameters);
}
}
public interface IServiceFactory
{
object? Create(Type type, params object?[] parameters);
T? Create<T>(Type type, params object?[] parameters);
}
@@ -1,10 +1,9 @@
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public class ServiceCreator<I, T> : IServiceCreator<I>
{
public class ServiceCreator<I, T> : IServiceCreator<I>
public virtual object Create(Func<Type, object[], object> creator, params object[] parameters)
{
public virtual object Create(Func<Type, object[], object> creator, params object[] parameters)
{
return creator(typeof(T), parameters);
}
return creator(typeof(T), parameters);
}
}
}
+22 -23
View File
@@ -1,26 +1,25 @@
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public class ServiceFactory : IServiceFactory
{
public class ServiceFactory : IServiceFactory
private readonly Func<Type, object?> factory;
private readonly Func<Type, object?[], object> creator;
public ServiceFactory(Func<Type, object?> factory, Func<Type, object?[], object> creator)
{
private readonly Func<Type, object?> factory;
private readonly Func<Type, object?[], object> creator;
public ServiceFactory(Func<Type, object?> factory, Func<Type, object?[], object> creator)
{
this.factory = factory;
this.creator = creator;
}
public object? Create(Type type, params object?[] parameters)
{
dynamic? lookup = factory(typeof(IServiceCreator<>).MakeGenericType(type));
return lookup is not null ? lookup.Create(creator, parameters) : creator(type, parameters);
}
public T? Create<T>(Type type, params object?[] parameters)
{
dynamic? lookup = factory(typeof(IServiceCreator<>).MakeGenericType(type));
return lookup is not null ? (T)lookup.Create(creator, parameters) : (T)creator(type, parameters);
}
this.factory = factory;
this.creator = creator;
}
}
public object? Create(Type type, params object?[] parameters)
{
dynamic? lookup = factory(typeof(IServiceCreator<>).MakeGenericType(type));
return lookup is not null ? lookup.Create(creator, parameters) : creator(type, parameters);
}
public T? Create<T>(Type type, params object?[] parameters)
{
dynamic? lookup = factory(typeof(IServiceCreator<>).MakeGenericType(type));
return lookup is not null ? (T)lookup.Create(creator, parameters) : (T)creator(type, parameters);
}
}
@@ -1,25 +1,24 @@
using System.Reflection;
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
internal class ServiceFactoryDescriptor
{
internal class ServiceFactoryDescriptor
private readonly IServiceFactory serviceFactory;
public ServiceFactoryDescriptor(IServiceFactory serviceFactory)
{
private readonly IServiceFactory serviceFactory;
public ServiceFactoryDescriptor(IServiceFactory serviceFactory)
{
this.serviceFactory = serviceFactory;
}
public object? Create(Type type)
{
MethodInfo? methodInfo = typeof(ServiceFactoryDescriptor).GetMethod(nameof(Create), BindingFlags.NonPublic | BindingFlags.Instance);
return methodInfo?.MakeGenericMethod(type).Invoke(this, new object[] { type });
}
private T Create<T>(Type type)
{
return serviceFactory.Create<T>(type);
}
this.serviceFactory = serviceFactory;
}
}
public object? Create(Type type)
{
MethodInfo? methodInfo = typeof(ServiceFactoryDescriptor).GetMethod(nameof(Create), BindingFlags.NonPublic | BindingFlags.Instance);
return methodInfo?.MakeGenericMethod(type).Invoke(this, new object[] { type });
}
private T? Create<T>(Type type)
{
return serviceFactory.Create<T>(type);
}
}
@@ -1,7 +1,6 @@
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public interface INamedDataTemplateFactory
{
public interface INamedDataTemplateFactory
{
object? Create(string name, params object[] parameters);
}
}
object? Create(string name, params object[] parameters);
}
@@ -1,7 +1,6 @@
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public interface INamedTemplateFactory
{
public interface INamedTemplateFactory
{
object? Create(string name);
}
}
object? Create(string name);
}
@@ -1,13 +1,12 @@
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public interface ITemplateBuilder
{
public interface ITemplateBuilder
{
IReadOnlyCollection<ITemplateDescriptor> Descriptors { get; }
IReadOnlyCollection<ITemplateDescriptor> Descriptors { get; }
ITemplateBuilder Add<TViewModel, TView>(string name, ServiceLifetime lifetime = ServiceLifetime.Transient);
ITemplateBuilder Add<TViewModel, TView>(string name, ServiceLifetime lifetime = ServiceLifetime.Transient);
ITemplateBuilder Add<TViewModel, TView>(ServiceLifetime lifetime = ServiceLifetime.Transient);
}
}
ITemplateBuilder Add<TViewModel, TView>(ServiceLifetime lifetime = ServiceLifetime.Transient);
}
@@ -1,15 +1,14 @@
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public interface ITemplateDescriptor
{
public interface ITemplateDescriptor
{
Type ContentType { get; }
Type ContentType { get; }
ServiceLifetime Lifetime { get; }
ServiceLifetime Lifetime { get; }
string? Name { get; }
string? Name { get; }
Type TemplateType { get; }
}
}
Type TemplateType { get; }
}
@@ -1,12 +1,10 @@
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public interface ITemplateDescriptorProvider
{
ITemplateDescriptor? Get(string name);
public interface ITemplateDescriptorProvider
{
ITemplateDescriptor? Get(string name);
ITemplateDescriptor? Get(Type type);
ITemplateDescriptor? Get(Type type);
ITemplateDescriptor? Get<T>();
}
}
ITemplateDescriptor? Get<T>();
}
@@ -1,9 +1,8 @@
using System.Diagnostics.CodeAnalysis;
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public interface ITemplateFactory
{
public interface ITemplateFactory
{
object? Create([MaybeNull] object? data);
}
}
object? Create([MaybeNull] object? data);
}
@@ -1,6 +1,5 @@
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public interface ITemplateSelector
{
public interface ITemplateSelector
{
}
}
}
@@ -1,7 +1,6 @@
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public interface ITypedDataTemplateFactory
{
public interface ITypedDataTemplateFactory
{
object? Create(Type type, params object[] parameters);
}
}
object? Create(Type type, params object[] parameters);
}
@@ -1,36 +1,35 @@
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public class NamedDataTemplateFactory : INamedDataTemplateFactory
{
public class NamedDataTemplateFactory : INamedDataTemplateFactory
private readonly Dictionary<string, object> cache = new();
private readonly IReadOnlyCollection<ITemplateDescriptor> descriptors;
private readonly IServiceFactory serviceFactory;
public NamedDataTemplateFactory(IReadOnlyCollection<ITemplateDescriptor> descriptors,
IServiceFactory serviceFactory)
{
private readonly Dictionary<string, object> cache = new();
this.descriptors = descriptors;
this.serviceFactory = serviceFactory;
}
private readonly IReadOnlyCollection<ITemplateDescriptor> descriptors;
private readonly IServiceFactory serviceFactory;
public NamedDataTemplateFactory(IReadOnlyCollection<ITemplateDescriptor> descriptors,
IServiceFactory serviceFactory)
public virtual object? Create(string name, params object[] parameters)
{
if (cache.TryGetValue(name, out object? data))
{
this.descriptors = descriptors;
this.serviceFactory = serviceFactory;
}
public virtual object? Create(string name, params object[] parameters)
{
if (cache.TryGetValue(name, out object? data))
{
return data;
}
if (descriptors.FirstOrDefault(x => x.Name == name) is ITemplateDescriptor descriptor)
{
data = parameters is { Length: > 0 } ? serviceFactory.Create<object>(descriptor.ContentType, parameters) : serviceFactory.Create(descriptor.ContentType);
if (data is ICache cache)
{
this.cache[name] = cache;
}
}
return data;
}
if (descriptors.FirstOrDefault(x => x.Name == name) is ITemplateDescriptor descriptor)
{
data = parameters is { Length: > 0 } ? serviceFactory.Create<object>(descriptor.ContentType, parameters) : serviceFactory.Create(descriptor.ContentType);
if (data is ICache cache)
{
this.cache[name] = cache;
}
}
return data;
}
}
}
@@ -1,41 +1,40 @@
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public class NamedTemplateFactory : INamedTemplateFactory
{
public class NamedTemplateFactory : INamedTemplateFactory
private readonly Dictionary<string, object> cache = new();
private readonly ITemplateDescriptorProvider provider;
private readonly IServiceFactory serviceFactory;
public NamedTemplateFactory(ITemplateDescriptorProvider provider,
IServiceFactory serviceFactory)
{
private readonly Dictionary<string, object> cache = new();
this.provider = provider;
this.serviceFactory = serviceFactory;
}
private readonly ITemplateDescriptorProvider provider;
private readonly IServiceFactory serviceFactory;
public NamedTemplateFactory(ITemplateDescriptorProvider provider,
IServiceFactory serviceFactory)
public virtual object? Create(string name)
{
if (cache.TryGetValue(name, out object? view))
{
this.provider = provider;
this.serviceFactory = serviceFactory;
}
public virtual object? Create(string name)
{
if (cache.TryGetValue(name, out object? view))
{
return view;
}
if (provider.Get(name) is ITemplateDescriptor descriptor)
{
view = serviceFactory.Create(descriptor.TemplateType);
if (view is ICache cache)
{
this.cache[name] = cache;
}
if (descriptor.GetType().GenericTypeArguments is { Length: 2 })
{
(descriptor as dynamic).ViewInvoker?.Invoke(view);
}
}
return view;
}
if (provider.Get(name) is ITemplateDescriptor descriptor)
{
view = serviceFactory.Create(descriptor.TemplateType);
if (view is ICache cache)
{
this.cache[name] = cache;
}
if (descriptor.GetType().GenericTypeArguments is { Length: 2 })
{
(descriptor as dynamic).ViewInvoker?.Invoke(view);
}
}
return view;
}
}
}
@@ -1,24 +1,23 @@
using Microsoft.Extensions.DependencyInjection;
using System.Collections.ObjectModel;
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public class TemplateBuilder : ITemplateBuilder
{
public class TemplateBuilder : ITemplateBuilder
private readonly List<ITemplateDescriptor> descriptors = new();
public IReadOnlyCollection<ITemplateDescriptor> Descriptors => new ReadOnlyCollection<ITemplateDescriptor>(descriptors);
public ITemplateBuilder Add<TViewModel, TView>(string name, ServiceLifetime lifetime = ServiceLifetime.Transient)
{
private readonly List<ITemplateDescriptor> descriptors = new();
public IReadOnlyCollection<ITemplateDescriptor> Descriptors => new ReadOnlyCollection<ITemplateDescriptor>(descriptors);
public ITemplateBuilder Add<TViewModel, TView>(string name, ServiceLifetime lifetime = ServiceLifetime.Transient)
{
descriptors.Add(new TemplateDescriptor(typeof(TViewModel), typeof(TView), name, lifetime));
return this;
}
public ITemplateBuilder Add<TViewModel, TView>(ServiceLifetime lifetime = ServiceLifetime.Transient)
{
descriptors.Add(new TemplateDescriptor(typeof(TViewModel), typeof(TView), null, lifetime));
return this;
}
descriptors.Add(new TemplateDescriptor(typeof(TViewModel), typeof(TView), name, lifetime));
return this;
}
}
public ITemplateBuilder Add<TViewModel, TView>(ServiceLifetime lifetime = ServiceLifetime.Transient)
{
descriptors.Add(new TemplateDescriptor(typeof(TViewModel), typeof(TView), null, lifetime));
return this;
}
}
@@ -1,26 +1,25 @@
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public class TemplateDescriptor : ITemplateDescriptor
{
public class TemplateDescriptor : ITemplateDescriptor
public TemplateDescriptor(Type dataType,
Type templateType,
string? name = null,
ServiceLifetime lifetime = ServiceLifetime.Transient)
{
public TemplateDescriptor(Type dataType,
Type templateType,
string? name = null,
ServiceLifetime lifetime = ServiceLifetime.Transient)
{
TemplateType = templateType;
ContentType = dataType;
Name = name;
Lifetime = lifetime;
}
public ServiceLifetime Lifetime { get; }
public Type TemplateType { get; }
public Type ContentType { get; }
public string? Name { get; }
TemplateType = templateType;
ContentType = dataType;
Name = name;
Lifetime = lifetime;
}
}
public ServiceLifetime Lifetime { get; }
public Type TemplateType { get; }
public Type ContentType { get; }
public string? Name { get; }
}
@@ -1,42 +1,41 @@
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public class TemplateDescriptorProvider : ITemplateDescriptorProvider
{
public class TemplateDescriptorProvider : ITemplateDescriptorProvider
private readonly IReadOnlyCollection<ITemplateDescriptor> descriptors;
public TemplateDescriptorProvider(IReadOnlyCollection<ITemplateDescriptor> descriptors)
{
private readonly IReadOnlyCollection<ITemplateDescriptor> descriptors;
public TemplateDescriptorProvider(IReadOnlyCollection<ITemplateDescriptor> descriptors)
{
this.descriptors = descriptors;
}
public ITemplateDescriptor? Get(string name)
{
if (descriptors.FirstOrDefault(x => x.Name == name) is ITemplateDescriptor descriptor)
{
return descriptor;
}
return null;
}
public ITemplateDescriptor? Get(Type type)
{
if (descriptors.FirstOrDefault(x => x.ContentType == type) is ITemplateDescriptor descriptor)
{
return descriptor;
}
return null;
}
public ITemplateDescriptor? Get<T>()
{
if (descriptors.FirstOrDefault(x => x.ContentType == typeof(T)) is ITemplateDescriptor descriptor)
{
return descriptor;
}
return null;
}
this.descriptors = descriptors;
}
}
public ITemplateDescriptor? Get(string name)
{
if (descriptors.FirstOrDefault(x => x.Name == name) is ITemplateDescriptor descriptor)
{
return descriptor;
}
return null;
}
public ITemplateDescriptor? Get(Type type)
{
if (descriptors.FirstOrDefault(x => x.ContentType == type) is ITemplateDescriptor descriptor)
{
return descriptor;
}
return null;
}
public ITemplateDescriptor? Get<T>()
{
if (descriptors.FirstOrDefault(x => x.ContentType == typeof(T)) is ITemplateDescriptor descriptor)
{
return descriptor;
}
return null;
}
}
@@ -1,43 +1,42 @@
using System.Diagnostics.CodeAnalysis;
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public class TemplateFactory : ITemplateFactory
{
public class TemplateFactory : ITemplateFactory
private readonly Dictionary<object, object> cache = new();
private readonly ITemplateDescriptorProvider provider;
private readonly IServiceFactory serviceFactory;
public TemplateFactory(ITemplateDescriptorProvider provider,
IServiceFactory serviceFactory)
{
private readonly Dictionary<object, object> cache = new();
this.provider = provider;
this.serviceFactory = serviceFactory;
}
private readonly ITemplateDescriptorProvider provider;
private readonly IServiceFactory serviceFactory;
public TemplateFactory(ITemplateDescriptorProvider provider,
IServiceFactory serviceFactory)
public virtual object? Create([MaybeNull] object? data)
{
if (data is null)
{
this.provider = provider;
this.serviceFactory = serviceFactory;
return null;
}
public virtual object? Create([MaybeNull] object? data)
if (cache.TryGetValue(data, out object? template))
{
if (data is null)
{
return null;
}
if (cache.TryGetValue(data, out object? template))
{
return template;
}
if (provider.Get(data.GetType()) is ITemplateDescriptor descriptor)
{
template = serviceFactory.Create(descriptor.TemplateType);
if (template is ICache cache)
{
this.cache[data] = cache;
}
}
return template;
}
if (provider.Get(data.GetType()) is ITemplateDescriptor descriptor)
{
template = serviceFactory.Create(descriptor.TemplateType);
if (template is ICache cache)
{
this.cache[data] = cache;
}
}
return template;
}
}
}
@@ -1,36 +1,35 @@
namespace Toolkit.Foundation
namespace Toolkit.Framework.Foundation;
public class TypedDataTemplateFactory : ITypedDataTemplateFactory
{
public class TypedDataTemplateFactory : ITypedDataTemplateFactory
private readonly Dictionary<Type, object> cache = new();
private readonly IReadOnlyCollection<ITemplateDescriptor> descriptors;
private readonly IServiceFactory serviceFactory;
public TypedDataTemplateFactory(IReadOnlyCollection<ITemplateDescriptor> descriptors,
IServiceFactory serviceFactory)
{
private readonly Dictionary<Type, object> cache = new();
this.descriptors = descriptors;
this.serviceFactory = serviceFactory;
}
private readonly IReadOnlyCollection<ITemplateDescriptor> descriptors;
private readonly IServiceFactory serviceFactory;
public TypedDataTemplateFactory(IReadOnlyCollection<ITemplateDescriptor> descriptors,
IServiceFactory serviceFactory)
public virtual object? Create(Type type, params object[] parameters)
{
if (cache.TryGetValue(type, out object? data))
{
this.descriptors = descriptors;
this.serviceFactory = serviceFactory;
}
public virtual object? Create(Type type, params object[] parameters)
{
if (cache.TryGetValue(type, out object? data))
{
return data;
}
if (descriptors.FirstOrDefault(x => x.ContentType == type) is ITemplateDescriptor descriptor)
{
data = parameters is { Length: > 0 } ? serviceFactory.Create<object>(descriptor.ContentType, parameters) : serviceFactory.Create(descriptor.ContentType);
if (data is ICache cache)
{
this.cache[type] = cache;
}
}
return data;
}
if (descriptors.FirstOrDefault(x => x.ContentType == type) is ITemplateDescriptor descriptor)
{
data = parameters is { Length: > 0 } ? serviceFactory.Create<object>(descriptor.ContentType, parameters) : serviceFactory.Create(descriptor.ContentType);
if (data is ICache cache)
{
this.cache[type] = cache;
}
}
return data;
}
}
}