namespace TheXamlGuy.Framework.Core { public class NamedDataTemplateFactory : INamedDataTemplateFactory { private readonly Dictionary dataTracking = new(); private readonly IReadOnlyCollection descriptors; private readonly IServiceFactory serviceFactory; public NamedDataTemplateFactory(IReadOnlyCollection descriptors, IServiceFactory serviceFactory) { this.descriptors = descriptors; this.serviceFactory = serviceFactory; } public virtual object? Create(string name, params object[] parameters) { if (dataTracking.TryGetValue(name, out object? data)) { return data; } if (descriptors.FirstOrDefault(x => x.Name == name) is ITemplateDescriptor descriptor) { data = parameters is { Length: > 0 } ? serviceFactory.Create(descriptor.DataType, parameters) : serviceFactory.Get(descriptor.DataType); if (data is ICachable cachable) { dataTracking[name] = cachable; } } return data; } } }