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 Nov 2, 2023
1 parent cf4287b commit a0b56dc
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 @@ -95,7 +95,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 @@ -64,7 +64,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 @@ -87,7 +87,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 @@ -25,6 +25,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 @@ -37,6 +39,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 @@ -63,6 +71,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 @@ -23,6 +23,7 @@

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

namespace EventFlow.Configuration
{
Expand All @@ -36,6 +37,8 @@ public class EventFlowConfiguration : IEventFlowConfiguration, ICancellationConf
public bool IsAsynchronousSubscribersEnabled { get; set; }
public CancellationBoundary CancellationBoundary { get; set; }
public bool ForwardOptimisticConcurrencyExceptions { get; set; }

public IEventNamingStrategy EventNamingStrategy { get; set; }

internal EventFlowConfiguration()
{
Expand All @@ -47,6 +50,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 a0b56dc

Please sign in to comment.