-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventDispatcher.cs
45 lines (36 loc) · 1.43 KB
/
EventDispatcher.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
using CarRentalService.Data.Events;
using CarRentalService.Data.Handlers;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace CarRentalService
{
public class EventDispatcher
{
public static EventDispatcher Instance { get { return _instance; } }
public IServiceProvider ServiceProvider { get; private set; }
private static EventDispatcher _instance = new EventDispatcher();
internal void Configure(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}
public void DispatchEvents<E>(IEnumerable<E> integrationEvents) where E : IIntegrationEvent
{
foreach (var integrationEvent in integrationEvents)
{
var eventHandlers = ServiceProvider.GetServices<IEventHandler>();
eventHandlers = eventHandlers
.Where(handler => ((TypeInfo)handler.GetType()).ImplementedInterfaces
.Any(i => i.GenericTypeArguments.FirstOrDefault() == integrationEvent.GetType()))
.ToList();
foreach (var eventHandler in eventHandlers)
{
eventHandler.Handle(integrationEvent);
}
}
}
}
}