using System.Collections.Concurrent; namespace Connected.Middleware; internal class ComponentMiddlewareService : IComponentMiddlewareService { public ComponentMiddlewareService() { Middleware = new(); } private ConcurrentDictionary> Middleware { get; } public void Add() { var componentName = typeof(TComponent).FullName; if (string.IsNullOrEmpty(componentName)) throw new ArgumentException(null, nameof(TMiddleware)); if (Middleware.TryGetValue(componentName, out List? items)) { //TODO: sort by priority so multiple inheritance would work items.Add(typeof(TMiddleware)); } else Middleware.TryAdd(componentName, new List { typeof(TMiddleware) }); } public Type? Select() { var typeName = typeof(TComponent).FullName; if (string.IsNullOrEmpty(typeName)) throw new ArgumentException(null, nameof(TComponent)); if (Middleware.TryGetValue(typeName, out List? items) && items is not null && items.Any()) return items[0]; return null; } }