-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommandChainProcessorBuilder.cs
More file actions
69 lines (53 loc) · 2.38 KB
/
CommandChainProcessorBuilder.cs
File metadata and controls
69 lines (53 loc) · 2.38 KB
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
using Botticelli.Framework.Extensions.Processors;
using Microsoft.Extensions.DependencyInjection;
namespace Botticelli.Framework.Commands.Processors;
public class CommandChainProcessorBuilder<TInputCommand> where TInputCommand : class, ICommand
{
private readonly IServiceCollection _services;
private readonly List<Type> _typesChain = new(3);
private ICommandChainProcessor<TInputCommand>? _chainProcessor;
public CommandChainProcessorBuilder(IServiceCollection services)
{
_services = services;
_typesChain.Add(typeof(CommandChainFirstElementProcessor<TInputCommand>));
_services.AddSingleton<CommandChainFirstElementProcessor<TInputCommand>>();
ProcessorFactoryBuilder.AddProcessor<CommandChainFirstElementProcessor<TInputCommand>>(_services);
}
public CommandChainProcessorBuilder<TInputCommand> AddNext<TNextProcessor>(ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
where TNextProcessor : class, ICommandChainProcessor<TInputCommand>
{
_typesChain.Add(typeof(TNextProcessor));
switch (serviceLifetime)
{
case ServiceLifetime.Singleton:
_services.AddSingleton<TNextProcessor>();
break;
case ServiceLifetime.Scoped:
_services.AddScoped<TNextProcessor>();
break;
case ServiceLifetime.Transient:
_services.AddTransient<TNextProcessor>();
break;
default:
throw new ArgumentOutOfRangeException(nameof(serviceLifetime), serviceLifetime, null);
}
ProcessorFactoryBuilder.AddProcessor<TNextProcessor>(_services);
return this;
}
public ICommandChainProcessor<TInputCommand>? Build(IServiceProvider sp)
{
if (_typesChain.Count == 0) return null;
var scope = sp.CreateScope();
// initializing chain processors...
_chainProcessor ??= sp.GetRequiredService(_typesChain.First()) as ICommandChainProcessor<TInputCommand>;
// making a chain...
var prev = _chainProcessor;
foreach (var type in _typesChain.Skip(1))
{
var proc = scope.ServiceProvider.GetRequiredService(type) as ICommandChainProcessor<TInputCommand>;
if (prev != null) prev.Next = proc;
prev = proc;
}
return _chainProcessor;
}
}