Skip to content

Commit

Permalink
Issue eventflow#907: Provide event naming strategies by DI container …
Browse files Browse the repository at this point in the history
…instead of EventFlowConfiguration
  • Loading branch information
SeWaS committed Dec 3, 2024
1 parent 28fad0b commit c3748c8
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 60 deletions.
4 changes: 3 additions & 1 deletion Source/EventFlow.Tests/IntegrationTests/UnicodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,7 +96,7 @@ public void UnicodeEvents()
var eventDefinitionService = new EventDefinitionService(
Mock<ILogger<EventDefinitionService>>(),
Mock<ILoadedVersionedTypes>(),
new EventFlowConfiguration());
new VoidStrategy());

// Act
Action action = () => eventDefinitionService.Load(typeof(Püng1Event));
Expand All @@ -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<IEventNamingStrategy, NamespaceAndClassNameStrategy>())
.ServiceCollection.BuildServiceProvider();

var bus = resolver.GetRequiredService<ICommandBus>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +65,7 @@ public void SetUp()
var definitionService = new EventDefinitionService(
Mock<ILogger<EventDefinitionService>>(),
Mock<ILoadedVersionedTypes>(),
new EventFlowConfiguration());
new NamespaceAndNameStrategy());
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 @@ -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;
Expand Down Expand Up @@ -86,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>(), new EventFlowConfiguration());
var definitionService = new EventDefinitionService(Logger<EventDefinitionService>(), Mock<ILoadedVersionedTypes>(), new NamespaceAndClassNameStrategy());
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 @@ -41,7 +41,7 @@ public class EventDefinitionServiceTests : VersionedTypeDefinitionServiceTestSui
[SetUp]
public void SetUp()
{
Inject<IEventFlowConfiguration>(new EventFlowConfiguration());
Inject<IEventNamingStrategy>(new VoidStrategy());
}

[Test]
Expand Down Expand Up @@ -75,7 +75,7 @@ public void GetDefinitions_OnEventWithMultipleDefinitions_ReturnsThemAll()
public void GetDefinitions_OnEventWithMultipleDefinitionsAndNonDefaultNamingStrategy_ReturnsThemAll()
{
// Arrange
Inject<IEventFlowConfiguration>(new EventFlowConfiguration {EventNamingStrategy = BuiltInEventNamingStrategies.NamespaceAndClassName});
Inject<IEventNamingStrategy>(new NamespaceAndClassNameStrategy());
Sut.Load(typeof(MultiNamesEvent));

// Act
Expand Down
1 change: 0 additions & 1 deletion Source/EventFlow/Configuration/EventFlowConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ internal EventFlowConfiguration()
IsAsynchronousSubscribersEnabled = false;
CancellationBoundary = CancellationBoundary.BeforeCommittingEvents;
ForwardOptimisticConcurrencyExceptions = false;
EventNamingStrategy = BuiltInEventNamingStrategies.Void;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions Source/EventFlow/EventFlowOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -227,6 +228,8 @@ private void RegisterDefaults(IServiceCollection serviceCollection)
_aggregateEventTypes,
_sagaTypes,
_snapshotTypes));

serviceCollection.TryAddTransient<IEventNamingStrategy, VoidStrategy>();
}

private void RegisterObsoleteDefaults(IServiceCollection serviceCollection)
Expand Down
11 changes: 6 additions & 5 deletions Source/EventFlow/EventStores/EventDefinitionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System;
using EventFlow.Aggregates;
using EventFlow.Configuration;
using EventFlow.Configuration.EventNamingStrategy;
using EventFlow.Core.VersionedTypes;
using Microsoft.Extensions.Logging;

Expand All @@ -32,22 +33,22 @@ public class EventDefinitionService :
VersionedTypeDefinitionService<IAggregateEvent, EventVersionAttribute, EventDefinition>,
IEventDefinitionService
{
private readonly IEventFlowConfiguration _eventFlowConfiguration;
private readonly IEventNamingStrategy _eventNamingStrategy;

public EventDefinitionService(
ILogger<EventDefinitionService> 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);
}
Expand Down

0 comments on commit c3748c8

Please sign in to comment.