-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pipeline.cs
36 lines (30 loc) · 929 Bytes
/
Pipeline.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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace CQRS.Common
{
public sealed class Pipeline<T>
{
private IServiceProvider _currentServiceProvider;
readonly List<IHandler<T>> handlers = new List<IHandler<T>>();
public Pipeline(IServiceProvider currentServiceProvider)
{
_currentServiceProvider = currentServiceProvider;
}
public Pipeline<T> UseHandler<TH>() where TH : IHandler<T>
{
var handler = _currentServiceProvider.GetRequiredService<TH>();
handlers.Add(handler);
return this;
}
public async Task<T> ProcessAsync(T input)
{
foreach (var filter in handlers)
{
input = await filter.Handle(input);
}
return input;
}
}
}