using System; using System.Linq; using zero.Core.Collections; using zero.Core.Entities; using zero.Core.Utils; namespace zero.Core.Options { public class InterceptorOptions : ZeroBackofficeCollection, IZeroCollectionOptions { 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 => 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; } } }