From c3748c80d5187e39f3193996eef1fbd4f5dfefa7 Mon Sep 17 00:00:00 2001 From: Sebastian Walter Date: Tue, 3 Dec 2024 08:28:04 +0100 Subject: [PATCH] Issue #907: Provide event naming strategies by DI container instead of EventFlowConfiguration --- .../IntegrationTests/UnicodeTests.cs | 4 ++- .../NamespaceAndClassNameStrategyTest.cs | 5 ++- .../NamespaceAndNameStrategyTest.cs | 5 ++- .../EventNamingStrategy/VoidStrategyTest.cs | 5 ++- .../ConcurrentFilesEventPersistanceTests.cs | 3 +- ...ConcurrentInMemoryEventPersistanceTests.cs | 3 +- .../EventDefinitionServiceTests.cs | 4 +-- .../Configuration/EventFlowConfiguration.cs | 1 - .../BuiltInEventNamingStrategies.cs | 32 ------------------- .../IEventNamingStrategy.cs | 3 +- .../NamespaceAndClassNameStrategy.cs | 3 +- .../NamespaceAndNameStrategy.cs | 3 +- .../EventNamingStrategy/VoidStrategy.cs | 3 +- Source/EventFlow/EventFlowOptions.cs | 3 ++ .../EventStores/EventDefinitionService.cs | 11 ++++--- 15 files changed, 28 insertions(+), 60 deletions(-) delete mode 100644 Source/EventFlow/Configuration/EventNamingStrategy/BuiltInEventNamingStrategies.cs diff --git a/Source/EventFlow.Tests/IntegrationTests/UnicodeTests.cs b/Source/EventFlow.Tests/IntegrationTests/UnicodeTests.cs index 8332efe0c..9d5f91fc6 100644 --- a/Source/EventFlow.Tests/IntegrationTests/UnicodeTests.cs +++ b/Source/EventFlow.Tests/IntegrationTests/UnicodeTests.cs @@ -26,6 +26,7 @@ using EventFlow.Aggregates; using EventFlow.Commands; using EventFlow.Configuration; +using EventFlow.Configuration.EventNamingStrategy; using EventFlow.Core; using EventFlow.EventStores; using EventFlow.Extensions; @@ -95,7 +96,7 @@ public void UnicodeEvents() var eventDefinitionService = new EventDefinitionService( Mock>(), Mock(), - new EventFlowConfiguration()); + new VoidStrategy()); // Act Action action = () => eventDefinitionService.Load(typeof(Püng1Event)); @@ -111,6 +112,7 @@ public async Task UnicodeIntegration() .AddEvents(typeof(Püng1Event)) .AddCommands(typeof(Cömmand)) .AddCommandHandlers(typeof(CömmandHändler)) + .RegisterServices(s => s.AddScoped()) .ServiceCollection.BuildServiceProvider(); var bus = resolver.GetRequiredService(); diff --git a/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategyTest.cs b/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategyTest.cs index 4fd84245d..8102bdb21 100644 --- a/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategyTest.cs +++ b/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategyTest.cs @@ -1,7 +1,6 @@ // The MIT License (MIT) // -// Copyright (c) 2015-2022 Rasmus Mikkelsen -// Copyright (c) 2015-2021 eBay Software Foundation +// Copyright (c) 2015-2024 Rasmus Mikkelsen // https://github.com/eventflow/EventFlow // // Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -37,7 +36,7 @@ private class Any {} public void EventNameShouldBeNamespaceAndClassName() { // Arrange - var strategy = BuiltInEventNamingStrategies.NamespaceAndClassName; + var strategy = new NamespaceAndClassNameStrategy(); // Act var name = strategy.CreateEventName(1, typeof(Any), "OriginalName"); diff --git a/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndNameStrategyTest.cs b/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndNameStrategyTest.cs index 2e19a49ad..4d188ca98 100644 --- a/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndNameStrategyTest.cs +++ b/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndNameStrategyTest.cs @@ -1,7 +1,6 @@ // The MIT License (MIT) // -// Copyright (c) 2015-2022 Rasmus Mikkelsen -// Copyright (c) 2015-2021 eBay Software Foundation +// Copyright (c) 2015-2024 Rasmus Mikkelsen // https://github.com/eventflow/EventFlow // // Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -36,7 +35,7 @@ private class Any {} public void EventNameShouldBeNamespaceAndClassName() { // Arrange - var strategy = BuiltInEventNamingStrategies.NamespaceAndName; + var strategy = new NamespaceAndNameStrategy(); // Act var name = strategy.CreateEventName(1, typeof(Any), "NameFromAttribute"); diff --git a/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/VoidStrategyTest.cs b/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/VoidStrategyTest.cs index aa284fdbf..f9ae0bfca 100644 --- a/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/VoidStrategyTest.cs +++ b/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/VoidStrategyTest.cs @@ -1,7 +1,6 @@ // The MIT License (MIT) // -// Copyright (c) 2015-2022 Rasmus Mikkelsen -// Copyright (c) 2015-2021 eBay Software Foundation +// Copyright (c) 2015-2024 Rasmus Mikkelsen // https://github.com/eventflow/EventFlow // // Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -37,7 +36,7 @@ private class Any {} public void EventNameShouldBeUnchanged() { // Arrange - var strategy = BuiltInEventNamingStrategies.Void; + var strategy = new VoidStrategy(); // Act var name = strategy.CreateEventName(1, typeof(Any), "OriginalName"); diff --git a/Source/EventFlow.Tests/UnitTests/EventStores/ConcurrentFilesEventPersistanceTests.cs b/Source/EventFlow.Tests/UnitTests/EventStores/ConcurrentFilesEventPersistanceTests.cs index cd3679d48..324e6900f 100644 --- a/Source/EventFlow.Tests/UnitTests/EventStores/ConcurrentFilesEventPersistanceTests.cs +++ b/Source/EventFlow.Tests/UnitTests/EventStores/ConcurrentFilesEventPersistanceTests.cs @@ -29,6 +29,7 @@ using System.Threading.Tasks; using EventFlow.Aggregates; using EventFlow.Configuration; +using EventFlow.Configuration.EventNamingStrategy; using EventFlow.Core; using EventFlow.EventStores; using EventFlow.EventStores.Files; @@ -64,7 +65,7 @@ public void SetUp() var definitionService = new EventDefinitionService( Mock>(), Mock(), - new EventFlowConfiguration()); + new NamespaceAndNameStrategy()); definitionService.Load(typeof(ThingyPingEvent)); _serializer = new EventJsonSerializer(new JsonSerializer(), definitionService, factory); diff --git a/Source/EventFlow.Tests/UnitTests/EventStores/ConcurrentInMemoryEventPersistanceTests.cs b/Source/EventFlow.Tests/UnitTests/EventStores/ConcurrentInMemoryEventPersistanceTests.cs index e091fa680..4ab37d183 100644 --- a/Source/EventFlow.Tests/UnitTests/EventStores/ConcurrentInMemoryEventPersistanceTests.cs +++ b/Source/EventFlow.Tests/UnitTests/EventStores/ConcurrentInMemoryEventPersistanceTests.cs @@ -28,6 +28,7 @@ using System.Threading.Tasks; using EventFlow.Aggregates; using EventFlow.Configuration; +using EventFlow.Configuration.EventNamingStrategy; using EventFlow.Core; using EventFlow.EventStores; using EventFlow.EventStores.InMemory; @@ -86,7 +87,7 @@ private EventStoreBase CreateStore() var factory = new DomainEventFactory(); var persistence = new InMemoryEventPersistence(Logger()); var upgradeManager = new EventUpgradeManager(Logger(), serviceProvider, new EventUpgradeContextFactory()); - var definitionService = new EventDefinitionService(Logger(), Mock(), new EventFlowConfiguration()); + var definitionService = new EventDefinitionService(Logger(), Mock(), new NamespaceAndClassNameStrategy()); definitionService.Load(typeof(ThingyPingEvent)); var serializer = new EventJsonSerializer(new JsonSerializer(), definitionService, factory); diff --git a/Source/EventFlow.Tests/UnitTests/EventStores/EventDefinitionServiceTests.cs b/Source/EventFlow.Tests/UnitTests/EventStores/EventDefinitionServiceTests.cs index 7e1b6b281..897f71759 100644 --- a/Source/EventFlow.Tests/UnitTests/EventStores/EventDefinitionServiceTests.cs +++ b/Source/EventFlow.Tests/UnitTests/EventStores/EventDefinitionServiceTests.cs @@ -41,7 +41,7 @@ public class EventDefinitionServiceTests : VersionedTypeDefinitionServiceTestSui [SetUp] public void SetUp() { - Inject(new EventFlowConfiguration()); + Inject(new VoidStrategy()); } [Test] @@ -75,7 +75,7 @@ public void GetDefinitions_OnEventWithMultipleDefinitions_ReturnsThemAll() public void GetDefinitions_OnEventWithMultipleDefinitionsAndNonDefaultNamingStrategy_ReturnsThemAll() { // Arrange - Inject(new EventFlowConfiguration {EventNamingStrategy = BuiltInEventNamingStrategies.NamespaceAndClassName}); + Inject(new NamespaceAndClassNameStrategy()); Sut.Load(typeof(MultiNamesEvent)); // Act diff --git a/Source/EventFlow/Configuration/EventFlowConfiguration.cs b/Source/EventFlow/Configuration/EventFlowConfiguration.cs index d96d33100..22ef46da3 100644 --- a/Source/EventFlow/Configuration/EventFlowConfiguration.cs +++ b/Source/EventFlow/Configuration/EventFlowConfiguration.cs @@ -100,7 +100,6 @@ internal EventFlowConfiguration() IsAsynchronousSubscribersEnabled = false; CancellationBoundary = CancellationBoundary.BeforeCommittingEvents; ForwardOptimisticConcurrencyExceptions = false; - EventNamingStrategy = BuiltInEventNamingStrategies.Void; } } } \ No newline at end of file diff --git a/Source/EventFlow/Configuration/EventNamingStrategy/BuiltInEventNamingStrategies.cs b/Source/EventFlow/Configuration/EventNamingStrategy/BuiltInEventNamingStrategies.cs deleted file mode 100644 index 45991bde7..000000000 --- a/Source/EventFlow/Configuration/EventNamingStrategy/BuiltInEventNamingStrategies.cs +++ /dev/null @@ -1,32 +0,0 @@ -// The MIT License (MIT) -// -// Copyright (c) 2015-2022 Rasmus Mikkelsen -// Copyright (c) 2015-2021 eBay Software Foundation -// https://github.com/eventflow/EventFlow -// -// Permission is hereby granted, free of charge, to any person obtaining a copy of -// this software and associated documentation files (the "Software"), to deal in -// the Software without restriction, including without limitation the rights to -// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -// the Software, and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -namespace EventFlow.Configuration.EventNamingStrategy -{ - public static class BuiltInEventNamingStrategies - { - public static IEventNamingStrategy Void => new VoidStrategy(); - public static IEventNamingStrategy NamespaceAndClassName => new NamespaceAndClassNameStrategy(); - public static IEventNamingStrategy NamespaceAndName => new NamespaceAndNameStrategy(); - } -} \ No newline at end of file diff --git a/Source/EventFlow/Configuration/EventNamingStrategy/IEventNamingStrategy.cs b/Source/EventFlow/Configuration/EventNamingStrategy/IEventNamingStrategy.cs index cbb53bb3e..15d75afe6 100644 --- a/Source/EventFlow/Configuration/EventNamingStrategy/IEventNamingStrategy.cs +++ b/Source/EventFlow/Configuration/EventNamingStrategy/IEventNamingStrategy.cs @@ -1,7 +1,6 @@ // The MIT License (MIT) // -// Copyright (c) 2015-2022 Rasmus Mikkelsen -// Copyright (c) 2015-2021 eBay Software Foundation +// Copyright (c) 2015-2024 Rasmus Mikkelsen // https://github.com/eventflow/EventFlow // // Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategy.cs b/Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategy.cs index d210554b8..daa01f129 100644 --- a/Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategy.cs +++ b/Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategy.cs @@ -1,7 +1,6 @@ // The MIT License (MIT) // -// Copyright (c) 2015-2022 Rasmus Mikkelsen -// Copyright (c) 2015-2021 eBay Software Foundation +// Copyright (c) 2015-2024 Rasmus Mikkelsen // https://github.com/eventflow/EventFlow // // Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndNameStrategy.cs b/Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndNameStrategy.cs index 0c5c65851..43692cfaf 100644 --- a/Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndNameStrategy.cs +++ b/Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndNameStrategy.cs @@ -1,7 +1,6 @@ // The MIT License (MIT) // -// Copyright (c) 2015-2022 Rasmus Mikkelsen -// Copyright (c) 2015-2021 eBay Software Foundation +// Copyright (c) 2015-2024 Rasmus Mikkelsen // https://github.com/eventflow/EventFlow // // Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/Source/EventFlow/Configuration/EventNamingStrategy/VoidStrategy.cs b/Source/EventFlow/Configuration/EventNamingStrategy/VoidStrategy.cs index 2fd1af1a1..53f9e3fca 100644 --- a/Source/EventFlow/Configuration/EventNamingStrategy/VoidStrategy.cs +++ b/Source/EventFlow/Configuration/EventNamingStrategy/VoidStrategy.cs @@ -1,7 +1,6 @@ // The MIT License (MIT) // -// Copyright (c) 2015-2022 Rasmus Mikkelsen -// Copyright (c) 2015-2021 eBay Software Foundation +// Copyright (c) 2015-2024 Rasmus Mikkelsen // https://github.com/eventflow/EventFlow // // Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/Source/EventFlow/EventFlowOptions.cs b/Source/EventFlow/EventFlowOptions.cs index ff8906868..37ae3141c 100644 --- a/Source/EventFlow/EventFlowOptions.cs +++ b/Source/EventFlow/EventFlowOptions.cs @@ -28,6 +28,7 @@ using EventFlow.Commands; using EventFlow.Configuration; using EventFlow.Configuration.Cancellation; +using EventFlow.Configuration.EventNamingStrategy; using EventFlow.Configuration.Serialization; using EventFlow.Core; using EventFlow.Core.RetryStrategies; @@ -227,6 +228,8 @@ private void RegisterDefaults(IServiceCollection serviceCollection) _aggregateEventTypes, _sagaTypes, _snapshotTypes)); + + serviceCollection.TryAddTransient(); } private void RegisterObsoleteDefaults(IServiceCollection serviceCollection) diff --git a/Source/EventFlow/EventStores/EventDefinitionService.cs b/Source/EventFlow/EventStores/EventDefinitionService.cs index e231ae13b..7d3bd85dc 100644 --- a/Source/EventFlow/EventStores/EventDefinitionService.cs +++ b/Source/EventFlow/EventStores/EventDefinitionService.cs @@ -23,6 +23,7 @@ using System; using EventFlow.Aggregates; using EventFlow.Configuration; +using EventFlow.Configuration.EventNamingStrategy; using EventFlow.Core.VersionedTypes; using Microsoft.Extensions.Logging; @@ -32,22 +33,22 @@ public class EventDefinitionService : VersionedTypeDefinitionService, IEventDefinitionService { - private readonly IEventFlowConfiguration _eventFlowConfiguration; + private readonly IEventNamingStrategy _eventNamingStrategy; public EventDefinitionService( ILogger logger, ILoadedVersionedTypes loadedVersionedTypes, - IEventFlowConfiguration eventFlowConfiguration) + IEventNamingStrategy eventNamingStrategy) : base(logger) { - _eventFlowConfiguration = eventFlowConfiguration; + _eventNamingStrategy = eventNamingStrategy; + Load(loadedVersionedTypes.Events); } protected override EventDefinition CreateDefinition(int version, Type type, string name) { - var strategyAppliedName = _eventFlowConfiguration.EventNamingStrategy - .CreateEventName(version, type, name); + var strategyAppliedName = _eventNamingStrategy.CreateEventName(version, type, name); return new EventDefinition(version, type, strategyAppliedName); }