Skip to content

Commit af874f0

Browse files
committed
more extensions
1 parent b72cd47 commit af874f0

10 files changed

+64
-16
lines changed

Eocron.DependencyInjection.Interceptors/MemoryCacheAsyncInterceptor.cs renamed to Eocron.DependencyInjection.Interceptors/Caching/MemoryCacheAsyncInterceptor.cs

File renamed without changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

Eocron.DependencyInjection.Interceptors/Eocron.DependencyInjection.Interceptors.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@
1717
<PackageReference Include="Polly" />
1818
</ItemGroup>
1919

20+
<ItemGroup>
21+
<ProjectReference Include="..\Eocron.DependencyInjection\Eocron.DependencyInjection.csproj" />
22+
</ItemGroup>
23+
2024

2125
</Project>

Eocron.DependencyInjection.Interceptors/RetryUntilConditionAsyncInterceptor.cs renamed to Eocron.DependencyInjection.Interceptors/Retry/RetryUntilConditionAsyncInterceptor.cs

File renamed without changes.

Eocron.DependencyInjection.Interceptors/ServiceCollectionExtensions.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

Eocron.DependencyInjection.Interceptors/TimeoutAsyncInterceptor.cs renamed to Eocron.DependencyInjection.Interceptors/Timeout/TimeoutAsyncInterceptor.cs

File renamed without changes.

Eocron.DependencyInjection/DecoratorChain.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
namespace Eocron.DependencyInjection
45
{
56
public sealed class DecoratorChain
67
{
78
private readonly List<DecoratorDelegate> _items = new();
89
public IReadOnlyList<DecoratorDelegate> Items => _items;
10+
public Type ServiceType { get; }
911

12+
public DecoratorChain(Type serviceType)
13+
{
14+
ServiceType = serviceType;
15+
}
1016
public DecoratorChain Add(DecoratorDelegate decorator)
1117
{
1218
_items.Add(decorator);

Eocron.DependencyInjection/DecoratorExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ private static ServiceDescriptor CloneWithNewKey(ServiceDescriptor descriptor)
9292
return new ServiceDescriptor(descriptor.ServiceType, newKey, descriptor.ImplementationType, descriptor.Lifetime);
9393
}
9494

95-
private static DecoratorChain ToChain(Action<DecoratorChain> chainBuilder)
95+
private static DecoratorChain ToChain(Type serviceType, Action<DecoratorChain> chainBuilder)
9696
{
97-
var chain = new DecoratorChain();
97+
var chain = new DecoratorChain(serviceType);
9898
chainBuilder(chain);
9999
return chain;
100100
}

Eocron.DependencyInjection/KeyedServiceCollectionExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public static IServiceCollection AddKeyedSingleton(
476476
Action<DecoratorChain> chainBuilder)
477477
{
478478
var serviceDescriptor = new ServiceDescriptor(serviceType, serviceKey, implementationInstance);
479-
services.Add(serviceDescriptor, ToChain(chainBuilder));
479+
services.Add(serviceDescriptor, ToChain(serviceType, chainBuilder));
480480
return services;
481481
}
482482

@@ -510,7 +510,7 @@ private static IServiceCollection AddKeyed(
510510
Action<DecoratorChain> chainBuilder)
511511
{
512512
var descriptor = new ServiceDescriptor(serviceType, serviceKey, implementationType, lifetime);
513-
collection.Add(descriptor, ToChain(chainBuilder));
513+
collection.Add(descriptor, ToChain(serviceType, chainBuilder));
514514
return collection;
515515
}
516516

@@ -523,7 +523,7 @@ private static IServiceCollection AddKeyed(
523523
Action<DecoratorChain> chainBuilder)
524524
{
525525
var descriptor = new ServiceDescriptor(serviceType, serviceKey, implementationFactory, lifetime);
526-
collection.Add(descriptor, ToChain(chainBuilder));
526+
collection.Add(descriptor, ToChain(serviceType, chainBuilder));
527527
return collection;
528528
}
529529
}

Eocron.DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ public static IServiceCollection AddSingleton(
407407
object implementationInstance, Action<DecoratorChain> chainBuilder)
408408
{
409409
var serviceDescriptor = new ServiceDescriptor(serviceType, implementationInstance);
410-
services.Add(serviceDescriptor, ToChain(chainBuilder));
410+
services.Add(serviceDescriptor, ToChain(serviceType, chainBuilder));
411411
return services;
412412
}
413413

@@ -436,7 +436,7 @@ private static IServiceCollection Add(
436436
ServiceLifetime lifetime, Action<DecoratorChain> chainBuilder)
437437
{
438438
var descriptor = new ServiceDescriptor(serviceType, implementationType, lifetime);
439-
collection.Add(descriptor, ToChain(chainBuilder));
439+
collection.Add(descriptor, ToChain(serviceType, chainBuilder));
440440
return collection;
441441
}
442442

@@ -447,7 +447,7 @@ private static IServiceCollection Add(
447447
ServiceLifetime lifetime, Action<DecoratorChain> chainBuilder)
448448
{
449449
var descriptor = new ServiceDescriptor(serviceType, implementationFactory, lifetime);
450-
collection.Add(descriptor, ToChain(chainBuilder));
450+
collection.Add(descriptor, ToChain(serviceType, chainBuilder));
451451
return collection;
452452
}
453453

0 commit comments

Comments
 (0)