Files
mixtape/zero.Core/Options/InterceptorOptions.cs
T
2021-09-08 15:06:56 +02:00

69 lines
1.7 KiB
C#

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<InterceptorRegistration>, IZeroCollectionOptions
{
public void Add<T>(int gravity = 0, Func<Type, bool> 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<T, TBoxed>(int gravity = 0, Func<Type, bool> canHandle = null)
where T : ICollectionInterceptor<TBoxed>
where TBoxed : ZeroEntity
{
Type type = typeof(T);
Type boxedType = typeof(TBoxed);
Func<Type, bool> 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<Type, bool> CanHandle { get; set; }
public bool IsInterceptorBoxed { get; set; }
}
}