1+ using System ;
2+ using Castle . DynamicProxy ;
3+ using Microsoft . Extensions . DependencyInjection ;
4+ using Microsoft . Extensions . Logging ;
5+
6+ namespace Eocron . DependencyInjection . Interceptors
7+ {
8+ public static class DecoratorChainExtensions
9+ {
10+ public static DecoratorChain AddInterceptor ( this DecoratorChain decoratorChain ,
11+ Func < IServiceProvider , IAsyncInterceptor > interceptorFactory )
12+ {
13+ decoratorChain . Add ( ( sp , instance ) => InterceptionHelper . CreateProxy ( instance , interceptorFactory ( sp ) ) ) ;
14+ return decoratorChain ;
15+ }
16+
17+ public static DecoratorChain AddInterceptor ( this DecoratorChain decoratorChain ,
18+ IAsyncInterceptor interceptor )
19+ {
20+ decoratorChain . Add ( ( sp , instance ) => InterceptionHelper . CreateProxy ( instance , interceptor ) ) ;
21+ return decoratorChain ;
22+ }
23+
24+ public static DecoratorChain AddTimeout ( this DecoratorChain decoratorChain , TimeSpan timeout )
25+ {
26+ if ( timeout <= TimeSpan . Zero )
27+ {
28+ return decoratorChain ;
29+ }
30+
31+ decoratorChain . AddInterceptor ( new TimeoutAsyncInterceptor ( timeout ) ) ;
32+ return decoratorChain ;
33+ }
34+
35+ public static DecoratorChain AddRetry ( this DecoratorChain decoratorChain ,
36+ Func < int , Exception , bool > exceptionPredicate ,
37+ Func < int , Exception , TimeSpan > retryIntervalProvider )
38+ {
39+ decoratorChain . AddInterceptor ( ( sp ) => new RetryUntilConditionAsyncInterceptor ( exceptionPredicate ,
40+ retryIntervalProvider ,
41+ sp . GetService < ILoggerFactory > ( ) ? . CreateLogger ( decoratorChain . ServiceType . Name ) ) ) ;
42+ return decoratorChain ;
43+ }
44+ }
45+ }
0 commit comments