From 28fad0b14ebeabee085604f013c0e8f077ea0b09 Mon Sep 17 00:00:00 2001 From: Sebastian Walter Date: Mon, 30 Oct 2023 16:34:50 +0100 Subject: [PATCH] Issue #907: provides mechanism to define a global event-naming strategy --- .../IntegrationTests/UnicodeTests.cs | 3 +- .../NamespaceAndClassNameStrategyTest.cs | 49 +++++++++++++++++++ .../NamespaceAndNameStrategyTest.cs | 48 ++++++++++++++++++ .../EventNamingStrategy/VoidStrategyTest.cs | 49 +++++++++++++++++++ .../ConcurrentFilesEventPersistanceTests.cs | 3 +- ...ConcurrentInMemoryEventPersistanceTests.cs | 2 +- .../EventDefinitionServiceTests.cs | 28 +++++++++++ .../Configuration/EventFlowConfiguration.cs | 4 ++ .../BuiltInEventNamingStrategies.cs | 32 ++++++++++++ .../IEventNamingStrategy.cs | 32 ++++++++++++ .../NamespaceAndClassNameStrategy.cs | 33 +++++++++++++ .../NamespaceAndNameStrategy.cs | 33 +++++++++++++ .../EventNamingStrategy/VoidStrategy.cs | 32 ++++++++++++ .../Configuration/IEventFlowConfiguration.cs | 13 +++++ .../EventStores/EventDefinitionService.cs | 11 ++++- 15 files changed, 367 insertions(+), 5 deletions(-) create mode 100644 Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategyTest.cs create mode 100644 Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndNameStrategyTest.cs create mode 100644 Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/VoidStrategyTest.cs create mode 100644 Source/EventFlow/Configuration/EventNamingStrategy/BuiltInEventNamingStrategies.cs create mode 100644 Source/EventFlow/Configuration/EventNamingStrategy/IEventNamingStrategy.cs create mode 100644 Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategy.cs create mode 100644 Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndNameStrategy.cs create mode 100644 Source/EventFlow/Configuration/EventNamingStrategy/VoidStrategy.cs diff --git a/Source/EventFlow.Tests/IntegrationTests/UnicodeTests.cs b/Source/EventFlow.Tests/IntegrationTests/UnicodeTests.cs index 3f8a32c89..8332efe0c 100644 --- a/Source/EventFlow.Tests/IntegrationTests/UnicodeTests.cs +++ b/Source/EventFlow.Tests/IntegrationTests/UnicodeTests.cs @@ -94,7 +94,8 @@ public void UnicodeEvents() // Arrange var eventDefinitionService = new EventDefinitionService( Mock>(), - Mock()); + Mock(), + new EventFlowConfiguration()); // Act Action action = () => eventDefinitionService.Load(typeof(Püng1Event)); diff --git a/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategyTest.cs b/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategyTest.cs new file mode 100644 index 000000000..4fd84245d --- /dev/null +++ b/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategyTest.cs @@ -0,0 +1,49 @@ +// 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. + +using EventFlow.Configuration.EventNamingStrategy; +using EventFlow.TestHelpers; +using FluentAssertions; +using NUnit.Framework; + +namespace EventFlow.Tests.UnitTests.Configuration.EventNamingStrategy +{ + [Category(Categories.Unit)] + public class NamespaceAndClassNameStrategyTest + { + private class Any {} + + [Test] + public void EventNameShouldBeNamespaceAndClassName() + { + // Arrange + var strategy = BuiltInEventNamingStrategies.NamespaceAndClassName; + + // Act + var name = strategy.CreateEventName(1, typeof(Any), "OriginalName"); + + // Assert + name.Should().Be(GetType().Namespace + ".Any"); + } + } +} \ No newline at end of file diff --git a/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndNameStrategyTest.cs b/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndNameStrategyTest.cs new file mode 100644 index 000000000..2e19a49ad --- /dev/null +++ b/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/NamespaceAndNameStrategyTest.cs @@ -0,0 +1,48 @@ +// 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. + +using EventFlow.Configuration.EventNamingStrategy; +using EventFlow.EventStores; +using FluentAssertions; +using NUnit.Framework; + +namespace EventFlow.Tests.UnitTests.Configuration.EventNamingStrategy +{ + public class NamespaceAndNameStrategyTest + { + private class Any {} + + [Test] + public void EventNameShouldBeNamespaceAndClassName() + { + // Arrange + var strategy = BuiltInEventNamingStrategies.NamespaceAndName; + + // Act + var name = strategy.CreateEventName(1, typeof(Any), "NameFromAttribute"); + + // Assert + name.Should().Be(GetType().Namespace + ".NameFromAttribute"); + } + } +} \ No newline at end of file diff --git a/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/VoidStrategyTest.cs b/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/VoidStrategyTest.cs new file mode 100644 index 000000000..aa284fdbf --- /dev/null +++ b/Source/EventFlow.Tests/UnitTests/Configuration/EventNamingStrategy/VoidStrategyTest.cs @@ -0,0 +1,49 @@ +// 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. + +using EventFlow.Configuration.EventNamingStrategy; +using EventFlow.TestHelpers; +using FluentAssertions; +using NUnit.Framework; + +namespace EventFlow.Tests.UnitTests.Configuration.EventNamingStrategy +{ + [Category(Categories.Unit)] + public class VoidStrategyTest + { + private class Any {} + + [Test] + public void EventNameShouldBeUnchanged() + { + // Arrange + var strategy = BuiltInEventNamingStrategies.Void; + + // Act + var name = strategy.CreateEventName(1, typeof(Any), "OriginalName"); + + // Assert + name.Should().Be("OriginalName"); + } + } +} \ No newline at end of file diff --git a/Source/EventFlow.Tests/UnitTests/EventStores/ConcurrentFilesEventPersistanceTests.cs b/Source/EventFlow.Tests/UnitTests/EventStores/ConcurrentFilesEventPersistanceTests.cs index 45b2a0588..cd3679d48 100644 --- a/Source/EventFlow.Tests/UnitTests/EventStores/ConcurrentFilesEventPersistanceTests.cs +++ b/Source/EventFlow.Tests/UnitTests/EventStores/ConcurrentFilesEventPersistanceTests.cs @@ -63,7 +63,8 @@ public void SetUp() var factory = new DomainEventFactory(); var definitionService = new EventDefinitionService( Mock>(), - Mock()); + Mock(), + new EventFlowConfiguration()); 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 2ebec4472..e091fa680 100644 --- a/Source/EventFlow.Tests/UnitTests/EventStores/ConcurrentInMemoryEventPersistanceTests.cs +++ b/Source/EventFlow.Tests/UnitTests/EventStores/ConcurrentInMemoryEventPersistanceTests.cs @@ -86,7 +86,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()); + var definitionService = new EventDefinitionService(Logger(), Mock(), new EventFlowConfiguration()); 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 f6c18ea76..7e1b6b281 100644 --- a/Source/EventFlow.Tests/UnitTests/EventStores/EventDefinitionServiceTests.cs +++ b/Source/EventFlow.Tests/UnitTests/EventStores/EventDefinitionServiceTests.cs @@ -24,6 +24,8 @@ using System.Collections.Generic; using System.Linq; using EventFlow.Aggregates; +using EventFlow.Configuration; +using EventFlow.Configuration.EventNamingStrategy; using EventFlow.Core; using EventFlow.EventStores; using EventFlow.TestHelpers; @@ -36,6 +38,12 @@ namespace EventFlow.Tests.UnitTests.EventStores [Category(Categories.Unit)] public class EventDefinitionServiceTests : VersionedTypeDefinitionServiceTestSuite { + [SetUp] + public void SetUp() + { + Inject(new EventFlowConfiguration()); + } + [Test] public void GetDefinition_OnEventWithMultipleDefinitions_ThrowsException() { @@ -62,6 +70,26 @@ public void GetDefinitions_OnEventWithMultipleDefinitions_ReturnsThemAll() .Select(d => $"{d.Name}-V{d.Version}") .Should().BeEquivalentTo(new []{"multi-names-event-V1", "MultiNamesEvent-V1", "MultiNamesEvent-V2"}); } + + [Test] + public void GetDefinitions_OnEventWithMultipleDefinitionsAndNonDefaultNamingStrategy_ReturnsThemAll() + { + // Arrange + Inject(new EventFlowConfiguration {EventNamingStrategy = BuiltInEventNamingStrategies.NamespaceAndClassName}); + Sut.Load(typeof(MultiNamesEvent)); + + // Act + var eventDefinitions = Sut.GetDefinitions(typeof(MultiNamesEvent)); + + // Assert + eventDefinitions.Should().HaveCount(3); + eventDefinitions + .Select(d => $"{d.Name}-V{d.Version}") + .Should().BeEquivalentTo( + "EventFlow.Tests.UnitTests.EventStores.MultiNamesEvent-V1", + "EventFlow.Tests.UnitTests.EventStores.MultiNamesEvent-V1", + "EventFlow.Tests.UnitTests.EventStores.MultiNamesEvent-V2"); + } [EventVersion("Fancy", 42)] public class TestEventWithLongName : AggregateEvent, IIdentity> { } diff --git a/Source/EventFlow/Configuration/EventFlowConfiguration.cs b/Source/EventFlow/Configuration/EventFlowConfiguration.cs index b0f35c4eb..d96d33100 100644 --- a/Source/EventFlow/Configuration/EventFlowConfiguration.cs +++ b/Source/EventFlow/Configuration/EventFlowConfiguration.cs @@ -22,6 +22,7 @@ using System; using EventFlow.Configuration.Cancellation; +using EventFlow.Configuration.EventNamingStrategy; namespace EventFlow.Configuration { @@ -86,6 +87,8 @@ public class EventFlowConfiguration : IEventFlowConfiguration, ICancellationConf public CancellationBoundary CancellationBoundary { get; set; } public bool ForwardOptimisticConcurrencyExceptions { get; set; } + + public IEventNamingStrategy EventNamingStrategy { get; set; } internal EventFlowConfiguration() { @@ -97,6 +100,7 @@ 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 new file mode 100644 index 000000000..45991bde7 --- /dev/null +++ b/Source/EventFlow/Configuration/EventNamingStrategy/BuiltInEventNamingStrategies.cs @@ -0,0 +1,32 @@ +// 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 new file mode 100644 index 000000000..cbb53bb3e --- /dev/null +++ b/Source/EventFlow/Configuration/EventNamingStrategy/IEventNamingStrategy.cs @@ -0,0 +1,32 @@ +// 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. + +using System; + +namespace EventFlow.Configuration.EventNamingStrategy +{ + public interface IEventNamingStrategy + { + public string CreateEventName(int version, Type eventType, string name); + } +} \ No newline at end of file diff --git a/Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategy.cs b/Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategy.cs new file mode 100644 index 000000000..d210554b8 --- /dev/null +++ b/Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndClassNameStrategy.cs @@ -0,0 +1,33 @@ +// 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. + +using System; + +namespace EventFlow.Configuration.EventNamingStrategy +{ + public class NamespaceAndClassNameStrategy : IEventNamingStrategy + { + public string CreateEventName(int version, Type eventType, string name) => + eventType.Namespace + "." + eventType.Name; + } +} \ No newline at end of file diff --git a/Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndNameStrategy.cs b/Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndNameStrategy.cs new file mode 100644 index 000000000..0c5c65851 --- /dev/null +++ b/Source/EventFlow/Configuration/EventNamingStrategy/NamespaceAndNameStrategy.cs @@ -0,0 +1,33 @@ +// 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. + +using System; + +namespace EventFlow.Configuration.EventNamingStrategy +{ + public class NamespaceAndNameStrategy : IEventNamingStrategy + { + public string CreateEventName(int version, Type eventType, string name) + => eventType.Namespace + "." + name; + } +} \ No newline at end of file diff --git a/Source/EventFlow/Configuration/EventNamingStrategy/VoidStrategy.cs b/Source/EventFlow/Configuration/EventNamingStrategy/VoidStrategy.cs new file mode 100644 index 000000000..2fd1af1a1 --- /dev/null +++ b/Source/EventFlow/Configuration/EventNamingStrategy/VoidStrategy.cs @@ -0,0 +1,32 @@ +// 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. + +using System; + +namespace EventFlow.Configuration.EventNamingStrategy +{ + internal class VoidStrategy : IEventNamingStrategy + { + public string CreateEventName(int version, Type eventType, string name) => name; + } +} \ No newline at end of file diff --git a/Source/EventFlow/Configuration/IEventFlowConfiguration.cs b/Source/EventFlow/Configuration/IEventFlowConfiguration.cs index e4ad4611a..5580007a8 100644 --- a/Source/EventFlow/Configuration/IEventFlowConfiguration.cs +++ b/Source/EventFlow/Configuration/IEventFlowConfiguration.cs @@ -22,6 +22,7 @@ using System; using EventFlow.Configuration.Cancellation; +using EventFlow.Configuration.EventNamingStrategy; namespace EventFlow.Configuration { @@ -86,5 +87,17 @@ public interface IEventFlowConfiguration CancellationBoundary CancellationBoundary { get; } bool ForwardOptimisticConcurrencyExceptions { get; set; } + + /// + /// Use EventNamingStrategy to determine the internal naming strategy + /// for events. The strategy is applied whenever an event is persisted + /// and loading from persistence and when serializing/deserializing in that + /// context respectively. + /// + /// If more fine grained control of is needed, a custom implementation of + /// IEventNamingStrategy should be provided. + /// + /// Defaults to SimpleNamingStrategy + IEventNamingStrategy EventNamingStrategy { get; } } } \ No newline at end of file diff --git a/Source/EventFlow/EventStores/EventDefinitionService.cs b/Source/EventFlow/EventStores/EventDefinitionService.cs index d79329a7f..e231ae13b 100644 --- a/Source/EventFlow/EventStores/EventDefinitionService.cs +++ b/Source/EventFlow/EventStores/EventDefinitionService.cs @@ -32,17 +32,24 @@ public class EventDefinitionService : VersionedTypeDefinitionService, IEventDefinitionService { + private readonly IEventFlowConfiguration _eventFlowConfiguration; + public EventDefinitionService( ILogger logger, - ILoadedVersionedTypes loadedVersionedTypes) + ILoadedVersionedTypes loadedVersionedTypes, + IEventFlowConfiguration eventFlowConfiguration) : base(logger) { + _eventFlowConfiguration = eventFlowConfiguration; Load(loadedVersionedTypes.Events); } protected override EventDefinition CreateDefinition(int version, Type type, string name) { - return new EventDefinition(version, type, name); + var strategyAppliedName = _eventFlowConfiguration.EventNamingStrategy + .CreateEventName(version, type, name); + + return new EventDefinition(version, type, strategyAppliedName); } } }