You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Components/Middleware/ComponentMiddlewareService.cs

43 lines
1.0 KiB

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