Files
Toolkit2/Framework/Foundation/Contents/ContentTemplateFactory.cs
T
2022-12-14 20:07:45 +00:00

42 lines
1.1 KiB
C#

using System.Diagnostics.CodeAnalysis;
namespace Toolkit.Framework.Foundation;
public class ContentTemplateFactory : IContentTemplateFactory
{
private readonly Dictionary<object, object> cache = new();
private readonly IContentTemplateDescriptorProvider provider;
private readonly IServiceFactory serviceFactory;
public ContentTemplateFactory(IContentTemplateDescriptorProvider 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 IContentTemplateDescriptor descriptor)
{
template = serviceFactory.Create(descriptor.TemplateType);
if (template is ICache cache)
{
this.cache[data] = cache;
}
}
return template;
}
}