/
IInterceptionConfiguration.cs
55 lines (46 loc) · 1.99 KB
/
IInterceptionConfiguration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Diagnostics.CodeAnalysis;
using WeihanLi.Common.Helpers;
namespace WeihanLi.Common.Aspect;
public interface IInterceptionConfiguration
{
ICollection<IInterceptor> Interceptors { get; }
}
internal class InterceptionConfiguration : IInterceptionConfiguration
{
public ICollection<IInterceptor> Interceptors { get; }
public InterceptionConfiguration()
{
Interceptors = new List<IInterceptor>();
}
}
public static class InterceptionConfigurationExtensions
{
public static IInterceptionConfiguration With(this IInterceptionConfiguration interceptionConfiguration, Func<IInvocation, Func<Task>, Task> interceptFunc)
{
Guard.NotNull(interceptionConfiguration, nameof(interceptionConfiguration))
.Interceptors.Add(new DelegateInterceptor(interceptFunc));
return interceptionConfiguration;
}
public static IInterceptionConfiguration With(this IInterceptionConfiguration interceptionConfiguration, IInterceptor interceptor)
{
interceptionConfiguration.Interceptors.Add(interceptor);
return interceptionConfiguration;
}
public static IInterceptionConfiguration With<TInterceptor>(this IInterceptionConfiguration interceptionConfiguration) where TInterceptor : IInterceptor, new()
{
interceptionConfiguration.With(new TInterceptor());
return interceptionConfiguration;
}
public static IInterceptionConfiguration With<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TInterceptor>(this IInterceptionConfiguration interceptionConfiguration, params object?[] parameters) where TInterceptor : IInterceptor
{
if (Guard.NotNull(parameters, nameof(parameters)).Length == 0)
{
interceptionConfiguration.With(NewFuncHelper<TInterceptor>.Instance());
}
else
{
interceptionConfiguration.With(ActivatorHelper.CreateInstance<TInterceptor>(parameters));
}
return interceptionConfiguration;
}
}