Wrap FluentAvalonia controls within same named classes allowing us to declare the xmlns namespace in our assembly
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface INamedDataTemplateFactory
|
||||
{
|
||||
object? Create(string name, params object[] parameters);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface INamedTemplateFactory
|
||||
{
|
||||
object? Create(string name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface ITemplateBuilder
|
||||
{
|
||||
IReadOnlyCollection<ITemplateDescriptor> Descriptors { get; }
|
||||
|
||||
ITemplateBuilder Add<TViewModel, TView>(string name, ServiceLifetime lifetime = ServiceLifetime.Transient);
|
||||
|
||||
ITemplateBuilder Add<TViewModel, TView>(ServiceLifetime lifetime = ServiceLifetime.Transient);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface ITemplateDescriptor
|
||||
{
|
||||
Type ContentType { get; }
|
||||
|
||||
ServiceLifetime Lifetime { get; }
|
||||
|
||||
string? Name { get; }
|
||||
|
||||
Type TemplateType { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
|
||||
public interface ITemplateDescriptorProvider
|
||||
{
|
||||
ITemplateDescriptor? Get(string name);
|
||||
|
||||
ITemplateDescriptor? Get(Type type);
|
||||
|
||||
ITemplateDescriptor? Get<T>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface ITemplateFactory
|
||||
{
|
||||
object? Create([MaybeNull] object? data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface ITemplateSelector
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface ITypedDataTemplateFactory
|
||||
{
|
||||
object? Create(Type type, params object[] parameters);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public class TemplateDescriptor : ITemplateDescriptor
|
||||
{
|
||||
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; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public class TemplateDescriptorProvider : ITemplateDescriptorProvider
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
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)
|
||||
{
|
||||
this.provider = provider;
|
||||
this.serviceFactory = serviceFactory;
|
||||
}
|
||||
|
||||
public virtual object? Create([MaybeNull] object? data)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user