forked from oskardudycz/EventSourcing.NetCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.cs
60 lines (50 loc) · 2.13 KB
/
Config.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
using Core.Commands;
using Core.Events;
using Core.Ids;
using Core.Queries;
using Core.Requests;
using Core.Tracing;
using Core.Tracing.Causation;
using Core.Tracing.Correlation;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Core;
public static class Config
{
public static IServiceCollection AddCoreServices(this IServiceCollection services)
{
services.AddMediatR()
.AddScoped<ICommandBus, CommandBus>()
.AddScoped<IQueryBus, QueryBus>()
.AddTracing()
.AddEventBus();
services.TryAddScoped<IExternalCommandBus, ExternalCommandBus>();
services.TryAddScoped<IIdGenerator, NulloIdGenerator>();
return services;
}
public static IServiceCollection AddTracing(this IServiceCollection services)
{
services.TryAddSingleton<ICorrelationIdFactory, GuidCorrelationIdFactory>();
services.TryAddSingleton<ICausationIdFactory, GuidCausationIdFactory>();
services.TryAddScoped<ICorrelationIdProvider, CorrelationIdProvider>();
services.TryAddScoped<ICausationIdProvider, CausationIdProvider>();
services.TryAddScoped<ITraceMetadataProvider, TraceMetadataProvider>();
services.TryAddSingleton<ITracingScopeFactory, TracingScopeFactory>();
services.TryAddScoped<Func<IServiceProvider, TraceMetadata?, TracingScope>>(sp =>
(scopedServiceProvider, traceMetadata) =>
sp.GetRequiredService<ITracingScopeFactory>().CreateTraceScope(scopedServiceProvider, traceMetadata)
);
services.TryAddScoped<Func<IServiceProvider, EventEnvelope?, TracingScope>>(sp =>
(scopedServiceProvider, eventEnvelope) => sp.GetRequiredService<ITracingScopeFactory>()
.CreateTraceScope(scopedServiceProvider, eventEnvelope)
);
return services;
}
private static IServiceCollection AddMediatR(this IServiceCollection services)
{
return services
.AddScoped<IMediator, Mediator>()
.AddTransient<ServiceFactory>(sp => sp.GetRequiredService!);
}
}