using System; using zero.Core.Collections; namespace zero.Configuration; public class InterceptorOptions : OptionsEnumerable, IOptionsEnumerable { public void Add(int gravity = 0, Func canHandle = null) where T : ICollectionInterceptor { Type type = typeof(T); if (canHandle == null) { canHandle = _ => true; } Items.Add(new InterceptorRegistration() { Hash = IdGenerator.Create(), Name = type.Name, Gravity = gravity, CanHandle = canHandle, InterceptorType = type }); } public void Add(int gravity = 0, Func canHandle = null) where T : ICollectionInterceptor where TBoxed : ZeroEntity { Type type = typeof(T); Type boxedType = typeof(TBoxed); Func finalCanHandle = requestedType => { return boxedType.IsAssignableFrom(requestedType) && (canHandle == null || canHandle.Invoke(requestedType)); }; Items.Add(new InterceptorRegistration() { Hash = IdGenerator.Create(), Name = type.Name, Gravity = gravity, CanHandle = finalCanHandle, InterceptorType = type, IsInterceptorBoxed = true }); } } public class InterceptorRegistration { public int Gravity { get; set; } public Type InterceptorType { get; set; } public string Hash { get; set; } public string Name { get; set; } public Func CanHandle { get; set; } public bool IsInterceptorBoxed { get; set; } }