namespace Toolkit.Framework.Foundation; public class NamedContentFactory : INamedContentFactory { private readonly Dictionary cache = new(); private readonly IReadOnlyCollection descriptors; private readonly IServiceFactory serviceFactory; public NamedContentFactory(IReadOnlyCollection 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 IContentTemplateDescriptor descriptor) { data = parameters is { Length: > 0 } ? serviceFactory.Create(descriptor.ContentType, parameters) : serviceFactory.Create(descriptor.ContentType); if (data is ICache cache) { this.cache[name] = cache; } } return data; } }