Add Content lookup markupextension
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public record Content : IRequest<object?>
|
||||
{
|
||||
public Content(string name, params object?[] parameters)
|
||||
{
|
||||
Name = name;
|
||||
Parameters = parameters;
|
||||
}
|
||||
|
||||
public Content(Type type, params object?[] parameters)
|
||||
{
|
||||
Type = type;
|
||||
Parameters = parameters;
|
||||
}
|
||||
|
||||
public Type? Type { get; }
|
||||
|
||||
public string? Name { get; }
|
||||
|
||||
public object?[] Parameters { get; }
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public class ContentTemplateBuilder : IContentTemplateBuilder
|
||||
{
|
||||
private readonly List<IContentTemplateDescriptor> descriptors = new();
|
||||
|
||||
public IReadOnlyCollection<IContentTemplateDescriptor> Descriptors => new ReadOnlyCollection<IContentTemplateDescriptor>(descriptors);
|
||||
|
||||
public IContentTemplateBuilder Add<TViewModel, TView>(string name, ServiceLifetime lifetime = ServiceLifetime.Transient)
|
||||
{
|
||||
descriptors.Add(new ContentTemplateDescriptor(typeof(TViewModel), typeof(TView), name, lifetime));
|
||||
return this;
|
||||
}
|
||||
|
||||
public IContentTemplateBuilder Add<TViewModel, TView>(ServiceLifetime lifetime = ServiceLifetime.Transient)
|
||||
{
|
||||
descriptors.Add(new ContentTemplateDescriptor(typeof(TViewModel), typeof(TView), null, lifetime));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -2,9 +2,9 @@
|
||||
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public class TemplateDescriptor : ITemplateDescriptor
|
||||
public class ContentTemplateDescriptor : IContentTemplateDescriptor
|
||||
{
|
||||
public TemplateDescriptor(Type dataType,
|
||||
public ContentTemplateDescriptor(Type dataType,
|
||||
Type templateType,
|
||||
string? name = null,
|
||||
ServiceLifetime lifetime = ServiceLifetime.Transient)
|
||||
@@ -0,0 +1,41 @@
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public class ContentTemplateDescriptorProvider : IContentTemplateDescriptorProvider
|
||||
{
|
||||
private readonly IReadOnlyCollection<IContentTemplateDescriptor> descriptors;
|
||||
|
||||
public ContentTemplateDescriptorProvider(IReadOnlyCollection<IContentTemplateDescriptor> descriptors)
|
||||
{
|
||||
this.descriptors = descriptors;
|
||||
}
|
||||
|
||||
public IContentTemplateDescriptor? Get(string name)
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => x.Name == name) is IContentTemplateDescriptor descriptor)
|
||||
{
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public IContentTemplateDescriptor? Get(Type type)
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => x.ContentType == type) is IContentTemplateDescriptor descriptor)
|
||||
{
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public IContentTemplateDescriptor? Get<T>()
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => x.ContentType == typeof(T)) is IContentTemplateDescriptor descriptor)
|
||||
{
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -2,14 +2,14 @@
|
||||
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public class TemplateFactory : ITemplateFactory
|
||||
public class ContentTemplateFactory : IContentTemplateFactory
|
||||
{
|
||||
private readonly Dictionary<object, object> cache = new();
|
||||
|
||||
private readonly ITemplateDescriptorProvider provider;
|
||||
private readonly IContentTemplateDescriptorProvider provider;
|
||||
private readonly IServiceFactory serviceFactory;
|
||||
|
||||
public TemplateFactory(ITemplateDescriptorProvider provider,
|
||||
public ContentTemplateFactory(IContentTemplateDescriptorProvider provider,
|
||||
IServiceFactory serviceFactory)
|
||||
{
|
||||
this.provider = provider;
|
||||
@@ -28,7 +28,7 @@ public class TemplateFactory : ITemplateFactory
|
||||
return template;
|
||||
}
|
||||
|
||||
if (provider.Get(data.GetType()) is ITemplateDescriptor descriptor)
|
||||
if (provider.Get(data.GetType()) is IContentTemplateDescriptor descriptor)
|
||||
{
|
||||
template = serviceFactory.Create(descriptor.TemplateType);
|
||||
if (template is ICache cache)
|
||||
@@ -0,0 +1,12 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public interface IContentTemplateBuilder
|
||||
{
|
||||
IReadOnlyCollection<IContentTemplateDescriptor> Descriptors { get; }
|
||||
|
||||
IContentTemplateBuilder Add<TViewModel, TView>(string name, ServiceLifetime lifetime = ServiceLifetime.Transient);
|
||||
|
||||
IContentTemplateBuilder Add<TViewModel, TView>(ServiceLifetime lifetime = ServiceLifetime.Transient);
|
||||
}
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public interface ITemplateDescriptor
|
||||
public interface IContentTemplateDescriptor
|
||||
{
|
||||
Type ContentType { get; }
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public interface IContentTemplateDescriptorProvider
|
||||
{
|
||||
IContentTemplateDescriptor? Get(string name);
|
||||
|
||||
IContentTemplateDescriptor? Get(Type type);
|
||||
|
||||
IContentTemplateDescriptor? Get<T>();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public interface IContentTemplateFactory
|
||||
{
|
||||
object? Create([MaybeNull] object? content);
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public interface ITemplateSelector
|
||||
public interface IContentTemplateSelector
|
||||
{
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public interface INamedTemplateFactory
|
||||
public interface INamedContentTemplateFactory
|
||||
{
|
||||
object? Create(string name);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public interface INamedContentFactory
|
||||
{
|
||||
object? Create(string name, params object?[] parameters);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public interface ITypedContentFactory
|
||||
{
|
||||
object? Create(Type type, params object?[] parameters);
|
||||
}
|
||||
+4
-4
@@ -1,13 +1,13 @@
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public class NamedDataTemplateFactory : INamedDataTemplateFactory
|
||||
public class NamedContentFactory : INamedContentFactory
|
||||
{
|
||||
private readonly Dictionary<string, object> cache = new();
|
||||
|
||||
private readonly IReadOnlyCollection<ITemplateDescriptor> descriptors;
|
||||
private readonly IReadOnlyCollection<IContentTemplateDescriptor> descriptors;
|
||||
private readonly IServiceFactory serviceFactory;
|
||||
|
||||
public NamedDataTemplateFactory(IReadOnlyCollection<ITemplateDescriptor> descriptors,
|
||||
public NamedContentFactory(IReadOnlyCollection<IContentTemplateDescriptor> descriptors,
|
||||
IServiceFactory serviceFactory)
|
||||
{
|
||||
this.descriptors = descriptors;
|
||||
@@ -21,7 +21,7 @@ public class NamedDataTemplateFactory : INamedDataTemplateFactory
|
||||
return data;
|
||||
}
|
||||
|
||||
if (descriptors.FirstOrDefault(x => x.Name == name) is ITemplateDescriptor descriptor)
|
||||
if (descriptors.FirstOrDefault(x => x.Name == name) is IContentTemplateDescriptor descriptor)
|
||||
{
|
||||
data = parameters is { Length: > 0 } ? serviceFactory.Create<object>(descriptor.ContentType, parameters) : serviceFactory.Create(descriptor.ContentType);
|
||||
if (data is ICache cache)
|
||||
+4
-4
@@ -1,13 +1,13 @@
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public class NamedTemplateFactory : INamedTemplateFactory
|
||||
public class NamedContentTemplateFactory : INamedContentTemplateFactory
|
||||
{
|
||||
private readonly Dictionary<string, object> cache = new();
|
||||
|
||||
private readonly ITemplateDescriptorProvider provider;
|
||||
private readonly IContentTemplateDescriptorProvider provider;
|
||||
private readonly IServiceFactory serviceFactory;
|
||||
|
||||
public NamedTemplateFactory(ITemplateDescriptorProvider provider,
|
||||
public NamedContentTemplateFactory(IContentTemplateDescriptorProvider provider,
|
||||
IServiceFactory serviceFactory)
|
||||
{
|
||||
this.provider = provider;
|
||||
@@ -21,7 +21,7 @@ public class NamedTemplateFactory : INamedTemplateFactory
|
||||
return view;
|
||||
}
|
||||
|
||||
if (provider.Get(name) is ITemplateDescriptor descriptor)
|
||||
if (provider.Get(name) is IContentTemplateDescriptor descriptor)
|
||||
{
|
||||
view = serviceFactory.Create(descriptor.TemplateType);
|
||||
if (view is ICache cache)
|
||||
+4
-4
@@ -1,13 +1,13 @@
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public class TypedDataTemplateFactory : ITypedDataTemplateFactory
|
||||
public class TypedContentFactory : ITypedContentFactory
|
||||
{
|
||||
private readonly Dictionary<Type, object> cache = new();
|
||||
|
||||
private readonly IReadOnlyCollection<ITemplateDescriptor> descriptors;
|
||||
private readonly IReadOnlyCollection<IContentTemplateDescriptor> descriptors;
|
||||
private readonly IServiceFactory serviceFactory;
|
||||
|
||||
public TypedDataTemplateFactory(IReadOnlyCollection<ITemplateDescriptor> descriptors,
|
||||
public TypedContentFactory(IReadOnlyCollection<IContentTemplateDescriptor> descriptors,
|
||||
IServiceFactory serviceFactory)
|
||||
{
|
||||
this.descriptors = descriptors;
|
||||
@@ -21,7 +21,7 @@ public class TypedDataTemplateFactory : ITypedDataTemplateFactory
|
||||
return data;
|
||||
}
|
||||
|
||||
if (descriptors.FirstOrDefault(x => x.ContentType == type) is ITemplateDescriptor descriptor)
|
||||
if (descriptors.FirstOrDefault(x => x.ContentType == type) is IContentTemplateDescriptor descriptor)
|
||||
{
|
||||
data = parameters is { Length: > 0 } ? serviceFactory.Create<object>(descriptor.ContentType, parameters) : serviceFactory.Create(descriptor.ContentType);
|
||||
if (data is ICache cache)
|
||||
@@ -22,7 +22,5 @@ public record Navigate : IRequest
|
||||
|
||||
public string? Name { get; }
|
||||
|
||||
public string? FriendlyName { get; init; }
|
||||
|
||||
public object?[] Parameters { get; }
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public interface INamedDataTemplateFactory
|
||||
{
|
||||
object? Create(string name, params object[] parameters);
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Toolkit.Framework.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);
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public interface ITemplateDescriptorProvider
|
||||
{
|
||||
ITemplateDescriptor? Get(string name);
|
||||
|
||||
ITemplateDescriptor? Get(Type type);
|
||||
|
||||
ITemplateDescriptor? Get<T>();
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public interface ITemplateFactory
|
||||
{
|
||||
object? Create([MaybeNull] object? data);
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public interface ITypedDataTemplateFactory
|
||||
{
|
||||
object? Create(Type type, params object[] parameters);
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Toolkit.Framework.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;
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
namespace Toolkit.Framework.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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user