-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServiceCollectionExtensions.cs
78 lines (67 loc) · 2.99 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
using Microsoft.Extensions.DependencyInjection;
using ServiceAnt.Handler.Request;
using ServiceAnt.Subscription;
using System;
using System.Reflection;
using System.Linq;
using ServiceAnt.Base;
using ServiceAnt.Request.Handler;
using ServiceAnt.Infrastructure.Dependency;
using ServiceAnt.IocInstaller.DotNetCore;
using ServiceAnt;
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
private static System.Reflection.Assembly[] _handlerAssemblies;
public static void AddServiceAnt(this IServiceCollection @this, params Assembly[] handlerAssemblies)
{
_handlerAssemblies = handlerAssemblies;
var subcriptionsManager = new InMemorySubscriptionsManager();
var requestManager = new InMemoryRequestHandlerManager();
RegisterHandlers(subcriptionsManager, requestManager);
@this.AddSingleton<ISubscriptionManager>(subcriptionsManager);
@this.AddSingleton<IRequestHandlerManager>(requestManager);
@this.AddTransient<IIocResolver>(serviceProvider =>
{
return new IocResolver(serviceProvider);
});
@this.AddSingleton<IServiceBus, InProcessServiceBus>();
var allHandlerTypes = _handlerAssemblies.SelectMany(p => p.ExportedTypes).Where(p=>typeof(IHandler).IsAssignableFrom(p));
foreach (var aHandlerType in allHandlerTypes)
{
@this.AddTransient(aHandlerType);
}
}
private static void RegisterHandlers(ISubscriptionManager subcriptionsManager, IRequestHandlerManager requestManager)
{
foreach (var aHandlerAssembly in _handlerAssemblies)
{
var handlerTypes = aHandlerAssembly.GetTypes().Where(p => typeof(IHandler).IsAssignableFrom(p) && !p.IsInterface);
foreach (var aHandler in handlerTypes)
{
RegisterHandlerType(subcriptionsManager, requestManager, aHandler);
}
}
}
private static void RegisterHandlerType(ISubscriptionManager subcriptionsManager, IRequestHandlerManager requestManager, Type aHandlerType)
{
var interfaces = aHandlerType.GetInterfaces();
foreach (var aInterface in interfaces)
{
if (!typeof(IHandler).IsAssignableFrom(aInterface))
{
continue;
}
var genericArgs = aInterface.GetGenericArguments();
if (genericArgs.Length == 1)
{
if (typeof(IRequestHandler).IsAssignableFrom(aInterface))
requestManager.AddRequestHandler(genericArgs[0], new IocHandlerFactory(aHandlerType, genericArgs[0]));
else
subcriptionsManager.AddSubscription(genericArgs[0], new IocHandlerFactory(aHandlerType, genericArgs[0]));
}
}
}
}
}