Skip to content

Commit 573748d

Browse files
Andreas Gehrkechristianhelle
Andreas Gehrke
authored andcommitted
Use TryAdd when registering types in UseCQRS() to prevent duplicate
registrations. Similarly to AddEventStore().
1 parent 4e1543d commit 573748d

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/Atc.Cosmos.EventStore.Cqrs/DependencyInjection/EventStoreOptionsBuilderExtensions.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ public static EventStoreOptionsBuilder UseCQRS(
1818

1919
configure?.Invoke(cqrsBuilder);
2020

21-
builder.Services.AddSingleton(typeof(IStateProjector<>), typeof(StateProjector<>));
22-
builder.Services.AddSingleton(typeof(IStateWriter<>), typeof(StateWriter<>));
23-
builder.Services.AddTransient(typeof(ICommandProcessor<>), typeof(CommandProcessor<>));
24-
builder.Services.AddTransient<ICommandProcessorFactory, CommandProcessorFactory>();
25-
builder.Services.AddTransient<ICommandHandlerFactory, CommandHandlerFactory>();
26-
builder.Services.AddSingleton<ICommandTelemetry, CommandTelemetry>();
21+
builder.Services.TryAddSingleton(typeof(IStateProjector<>), typeof(StateProjector<>));
22+
builder.Services.TryAddSingleton(typeof(IStateWriter<>), typeof(StateWriter<>));
23+
builder.Services.TryAddTransient(typeof(ICommandProcessor<>), typeof(CommandProcessor<>));
24+
builder.Services.TryAddTransient<ICommandProcessorFactory, CommandProcessorFactory>();
25+
builder.Services.TryAddTransient<ICommandHandlerFactory, CommandHandlerFactory>();
26+
builder.Services.TryAddSingleton<ICommandTelemetry, CommandTelemetry>();
2727

28-
builder.Services.AddSingleton<IProjectionOptionsFactory, ProjectionOptionsFactory>();
28+
builder.Services.TryAddSingleton<IProjectionOptionsFactory, ProjectionOptionsFactory>();
2929

30-
builder.Services.AddSingleton(typeof(ProjectionMetadata<>), typeof(ProjectionMetadata<>));
31-
builder.Services.AddTransient<IProjectionFactory, DefaultProjectionFactory>();
30+
builder.Services.TryAddSingleton(typeof(ProjectionMetadata<>), typeof(ProjectionMetadata<>));
31+
builder.Services.TryAddTransient<IProjectionFactory, DefaultProjectionFactory>();
3232

3333
builder.Services.TryAddSingleton<IProjectionTelemetry, ProjectionTelemetry>();
3434

0 commit comments

Comments
 (0)