This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceCollectionExtensions.cs
94 lines (73 loc) · 3.49 KB
/
ServiceCollectionExtensions.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Reflection;
using FluentValidation;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Netcorext.Grpc.Mediator;
using Netcorext.Grpc.Mediator.Abstractions;
using Netcorext.Grpc.Mediator.Internal;
using Netcorext.Grpc.Mediator.Pipelines;
namespace Microsoft.Extensions.DependencyInjection;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddMediator(this IServiceCollection services, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
{
services.AddTransient<IDispatcher, Dispatcher>();
// Add Handlers
var namespacePattern = typeof(IMessageHandler).Namespace ?? string.Empty;
var types = Assembly.GetEntryAssembly()
.GetTypes()
.Cast<TypeInfo>()
.Where(t => t.GetConstructors(BindingFlags.Instance | BindingFlags.Public).Length == 1
&& t.ImplementedInterfaces.Any(f => f.IsGenericType && !string.IsNullOrWhiteSpace(f.Namespace) && f.Namespace.StartsWith(namespacePattern, StringComparison.OrdinalIgnoreCase)))
.ToArray();
return AddMediator(services, types, serviceLifetime);
}
public static IServiceCollection AddMediator(this IServiceCollection services, Type[] types, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
{
if (types == null)
throw new ArgumentNullException(nameof(types));
var serviceMaps = FindServices(types.Cast<TypeInfo>().ToArray());
foreach (var map in serviceMaps)
{
services.TryAdd(new ServiceDescriptor(map.Interface, map.Implementation, serviceLifetime));
}
return services;
}
public static IServiceCollection AddPerformancePipeline(this IServiceCollection services, Action<IServiceProvider, PerformanceOptions> configure = default)
{
services.TryAddSingleton(provider =>
{
var opt = new PerformanceOptions();
configure?.Invoke(provider, opt);
return opt;
});
services.AddPipeline<PerformancePipeline>();
return services;
}
public static IServiceCollection AddValidatorPipeline(this IServiceCollection services)
{
services.AddValidatorsFromAssembly(Assembly.GetEntryAssembly());
services.AddPipeline<ValidatorPipeline>();
return services;
}
public static IServiceCollection AddPipeline<TPipeline>(this IServiceCollection services) where TPipeline : class, IPipeline
{
services.AddTransient<IPipeline, TPipeline>();
return services;
}
private static IEnumerable<ServiceMap> FindServices(params TypeInfo[] types)
{
foreach (var type in types)
{
var req = type.ImplementedInterfaces.FirstOrDefault(t => t.IsGenericType &&
t.GetInterfaces()
.Any(t2 => t2 == typeof(IMessageHandler)));
if (req == null) continue;
yield return new ServiceMap
{
Service = req.GenericTypeArguments.First(),
Interface = req,
Implementation = type
};
}
}
}