Skip to content

Commit

Permalink
Issue eventflow#907: provides mechanism to define a global event-nami…
Browse files Browse the repository at this point in the history
…ng strategy
  • Loading branch information
SeWaS committed Dec 2, 2024
1 parent c27cb47 commit 28fad0b
Show file tree
Hide file tree
Showing 15 changed files with 367 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Source/EventFlow.Tests/IntegrationTests/UnicodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public void UnicodeEvents()
// Arrange
var eventDefinitionService = new EventDefinitionService(
Mock<ILogger<EventDefinitionService>>(),
Mock<ILoadedVersionedTypes>());
Mock<ILoadedVersionedTypes>(),
new EventFlowConfiguration());

// Act
Action action = () => eventDefinitionService.Load(typeof(Püng1Event));
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public void SetUp()
var factory = new DomainEventFactory();
var definitionService = new EventDefinitionService(
Mock<ILogger<EventDefinitionService>>(),
Mock<ILoadedVersionedTypes>());
Mock<ILoadedVersionedTypes>(),
new EventFlowConfiguration());
definitionService.Load(typeof(ThingyPingEvent));

_serializer = new EventJsonSerializer(new JsonSerializer(), definitionService, factory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private EventStoreBase CreateStore()
var factory = new DomainEventFactory();
var persistence = new InMemoryEventPersistence(Logger<InMemoryEventPersistence>());
var upgradeManager = new EventUpgradeManager(Logger<EventUpgradeManager>(), serviceProvider, new EventUpgradeContextFactory());
var definitionService = new EventDefinitionService(Logger<EventDefinitionService>(), Mock<ILoadedVersionedTypes>());
var definitionService = new EventDefinitionService(Logger<EventDefinitionService>(), Mock<ILoadedVersionedTypes>(), new EventFlowConfiguration());
definitionService.Load(typeof(ThingyPingEvent));
var serializer = new EventJsonSerializer(new JsonSerializer(), definitionService, factory);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +38,12 @@ namespace EventFlow.Tests.UnitTests.EventStores
[Category(Categories.Unit)]
public class EventDefinitionServiceTests : VersionedTypeDefinitionServiceTestSuite<EventDefinitionService, IAggregateEvent, EventVersionAttribute, EventDefinition>
{
[SetUp]
public void SetUp()
{
Inject<IEventFlowConfiguration>(new EventFlowConfiguration());
}

[Test]
public void GetDefinition_OnEventWithMultipleDefinitions_ThrowsException()
{
Expand All @@ -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<IEventFlowConfiguration>(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<IAggregateRoot<IIdentity>, IIdentity> { }
Expand Down
4 changes: 4 additions & 0 deletions Source/EventFlow/Configuration/EventFlowConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

using System;
using EventFlow.Configuration.Cancellation;
using EventFlow.Configuration.EventNamingStrategy;

namespace EventFlow.Configuration
{
Expand Down Expand Up @@ -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()
{
Expand All @@ -97,6 +100,7 @@ internal EventFlowConfiguration()
IsAsynchronousSubscribersEnabled = false;
CancellationBoundary = CancellationBoundary.BeforeCommittingEvents;
ForwardOptimisticConcurrencyExceptions = false;
EventNamingStrategy = BuiltInEventNamingStrategies.Void;
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 28fad0b

Please sign in to comment.