diff --git a/src/EventStoreEventTools.Tests/EventStoreEventTools.Tests.csproj b/src/EventStoreEventTools.Tests/EventStoreEventTools.Tests.csproj new file mode 100644 index 0000000..42d3c29 --- /dev/null +++ b/src/EventStoreEventTools.Tests/EventStoreEventTools.Tests.csproj @@ -0,0 +1,31 @@ + + + + net6.0 + enable + enable + + false + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/src/EventStoreEventTools.Tests/EventStoreEventToolsTests.cs b/src/EventStoreEventTools.Tests/EventStoreEventToolsTests.cs new file mode 100644 index 0000000..3464048 --- /dev/null +++ b/src/EventStoreEventTools.Tests/EventStoreEventToolsTests.cs @@ -0,0 +1,37 @@ +namespace EventStoreEventTools.Tests +{ + using NUnit.Framework; + using EventStore.Client; + using CorshamScience.Tools.EventStore; + using Newtonsoft.Json; + using System.Text; + + internal class EventStoreEventToolsTests + { + [Test] + public void ToEventData_Given_Valid_Event_Returns_EventData() + { + var standardEvent = new StandardEvent + { + Id = Uuid.NewUuid(), + Description = "Test", + }; + + var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(standardEvent)); + var eventHeaders = new { ClrType = standardEvent.GetType().AssemblyQualifiedName }; + + var metadata = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(eventHeaders)); + var typeName = standardEvent.GetType().Name; + + var expectedObject = new EventData(standardEvent.Id, typeName, data, metadata); + + var toEventData = EventStoreEventTools.ToEventData(standardEvent.Id, standardEvent); + Assert.That(JsonConvert.SerializeObject(toEventData), Is.EqualTo(JsonConvert.SerializeObject(expectedObject))); + } + private class StandardEvent + { + public Uuid Id { get; set; } + public string? Description { get; set; } + } + } +} diff --git a/src/Tools.EventStore.sln b/src/Tools.EventStore.sln index e6b7efd..d3a0914 100644 --- a/src/Tools.EventStore.sln +++ b/src/Tools.EventStore.sln @@ -1,9 +1,11 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27703.2000 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32929.385 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tools.EventStore", "Tools.EventStore\Tools.EventStore.csproj", "{1FEA1406-472C-471E-96C6-3F075DD50FD3}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tools.EventStore", "Tools.EventStore\Tools.EventStore.csproj", "{1FEA1406-472C-471E-96C6-3F075DD50FD3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventStoreEventTools.Tests", "EventStoreEventTools.Tests\EventStoreEventTools.Tests.csproj", "{DCC4AC2A-9B1D-41FA-B450-818C62D2D158}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {1FEA1406-472C-471E-96C6-3F075DD50FD3}.Debug|Any CPU.Build.0 = Debug|Any CPU {1FEA1406-472C-471E-96C6-3F075DD50FD3}.Release|Any CPU.ActiveCfg = Release|Any CPU {1FEA1406-472C-471E-96C6-3F075DD50FD3}.Release|Any CPU.Build.0 = Release|Any CPU + {DCC4AC2A-9B1D-41FA-B450-818C62D2D158}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DCC4AC2A-9B1D-41FA-B450-818C62D2D158}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DCC4AC2A-9B1D-41FA-B450-818C62D2D158}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DCC4AC2A-9B1D-41FA-B450-818C62D2D158}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Tools.EventStore/EventStoreClientBuilder.cs b/src/Tools.EventStore/EventStoreClientBuilder.cs new file mode 100644 index 0000000..40bf141 --- /dev/null +++ b/src/Tools.EventStore/EventStoreClientBuilder.cs @@ -0,0 +1,26 @@ +// +// Copyright (c) Corsham Science. All rights reserved. +// + +namespace CorshamScience.Tools.EventStore +{ + using System; + using global::EventStore.Client; + + /// + /// A helper class for quickly building an . + /// + public static class EventStoreClientBuilder + { + /// + /// Builds an using the default settings. + /// + /// Put valid connection string. + /// EventStoreClient. + public static EventStoreClient GetEventStoreClient(string connectionString) + { + var settings = EventStoreClientSettings.Create(connectionString); + return new EventStoreClient(settings); + } + } +} diff --git a/src/Tools.EventStore/EventStoreConnectionBuilder.cs b/src/Tools.EventStore/EventStoreConnectionBuilder.cs deleted file mode 100644 index 9370321..0000000 --- a/src/Tools.EventStore/EventStoreConnectionBuilder.cs +++ /dev/null @@ -1,114 +0,0 @@ -// -// Copyright (c) Corsham Science. All rights reserved. -// - -namespace CorshamScience.Tools.EventStore -{ - using System; - using global::EventStore.ClientAPI; - - /// - /// A helper class for quickly building an . - /// - // ReSharper disable once UnusedMember.Global - public static class EventStoreConnectionBuilder - { - /// - /// Build an using the passed in connection and configuration. - /// - /// The URI for the Event Store's external TCP port, including login details. - /// An optional maximum allowed retries for any given operation; this defaults to 10. - /// An optional gossip timeout for the EventStore connection; this defaults to 5 seconds. - /// An optional to use to log information about and issues with the . - /// An optional action to call when the disconnects. If no action is passed in, but a logger is, disconnection will result in an error logging event. - /// An optional action to call when the connects. If no action is passed in, but a logger is, connection will result in an info logging event. - /// An optional action to call when the is reconnecting. If no action is passed in, but a logger is, reconnection will result in an error logging event. - /// An optional action to call when the is closed. If no action is passed in, but a logger is, closure will result in an info logging event. - /// An optional action to call when the encounters an error. If no action is passed in, but a logger is, an error occurring will result in an error error logging event. - /// A configured, connected which uses any passed in configuration variables, or the defaults detailed in the parameters section. - // ReSharper disable once UnusedMember.Global - public static IEventStoreConnection SetUpEventStoreConnection( - Uri connection, - int retryLimit = 10, - TimeSpan? gossipTimeout = null, - ILogger logger = null, - Action onDisconnected = null, - Action onConnected = null, - Action onReconnecting = null, - Action onClosed = null, - Action onError = null) - { - var settings = ConnectionSettings.Create().KeepReconnecting().SetGossipTimeout(gossipTimeout ?? TimeSpan.FromSeconds(5)).LimitRetriesForOperationTo(retryLimit); - if (logger != null) - { - settings.UseCustomLogger(logger); - } - - var eventStoreClient = EventStoreConnection.Create(settings, connection); - - if (onError != null) - { - eventStoreClient.ErrorOccurred += (sender, args) => onError(sender, args); - } - else if (logger != null) - { - void EventStoreClientOnErrorOccurred(object sender, ClientErrorEventArgs clientErrorEventArgs) - { - if (clientErrorEventArgs.Exception != null) - { - logger.Error(clientErrorEventArgs.Exception, "Event Store error occurred"); - } - else - { - logger.Error("Unknown Event Store error"); - } - } - - eventStoreClient.ErrorOccurred += EventStoreClientOnErrorOccurred; - } - - if (onDisconnected != null) - { - eventStoreClient.Disconnected += (sender, args) => onDisconnected(sender, args); - } - else if (logger != null) - { - void EventStoreClientOnDisconnected(object sender, ClientConnectionEventArgs clientConnectionEventArgs) => logger.Error($"Event Store disconnected from endpoint {clientConnectionEventArgs.RemoteEndPoint.Address}:{clientConnectionEventArgs.RemoteEndPoint.Port}"); - eventStoreClient.Disconnected += EventStoreClientOnDisconnected; - } - - if (onReconnecting != null) - { - eventStoreClient.Reconnecting += (sender, args) => onReconnecting(sender, args); - } - else if (logger != null) - { - void EventStoreClientOnReconnecting(object sender, ClientReconnectingEventArgs clientReconnectingEventArgs) => logger.Info("Event Store reconnecting"); - eventStoreClient.Reconnecting += EventStoreClientOnReconnecting; - } - - if (onConnected != null) - { - eventStoreClient.Connected += (sender, args) => onConnected(sender, args); - } - else if (logger != null) - { - void EventStoreClientOnConnected(object sender, ClientConnectionEventArgs clientConnectionEventArgs) => logger.Info($"Event Store connected {clientConnectionEventArgs.RemoteEndPoint.Address}:{clientConnectionEventArgs.RemoteEndPoint.Port}"); - eventStoreClient.Connected += EventStoreClientOnConnected; - } - - if (onClosed != null) - { - eventStoreClient.Closed += (sender, args) => onClosed(sender, args); - } - else if (logger != null) - { - void EventStoreClientOnClosed(object sender, ClientClosedEventArgs clientClosedEventArgs) => logger.Info("Event Store connection closed"); - eventStoreClient.Closed += EventStoreClientOnClosed; - } - - eventStoreClient.ConnectAsync().Wait(); - return eventStoreClient; - } - } -} diff --git a/src/Tools.EventStore/EventStoreEventTools.cs b/src/Tools.EventStore/EventStoreEventTools.cs index 38b0431..a4cb536 100644 --- a/src/Tools.EventStore/EventStoreEventTools.cs +++ b/src/Tools.EventStore/EventStoreEventTools.cs @@ -6,11 +6,11 @@ namespace CorshamScience.Tools.EventStore { using System; using System.Text; - using global::EventStore.ClientAPI; + using global::EventStore.Client; using Newtonsoft.Json; /// - /// A helper class containing methods to handle reading and writing events to/from an . + /// A helper class containing methods to handle reading and writing events to/from an . /// public static class EventStoreEventTools { @@ -22,7 +22,7 @@ public static class EventStoreEventTools /// Optional to use when serializing the provided and the event metadata. /// A new object with the provided ID, and serialized & encoded JSON data, as well as metadata containing the assembly qualified name for the event . // ReSharper disable once UnusedMember.Global - public static EventData ToEventData(Guid eventId, object @event, JsonSerializerSettings serializerSettings = null) + public static EventData ToEventData(Uuid eventId, object @event, JsonSerializerSettings serializerSettings = null) { var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(@event, serializerSettings)); var eventHeaders = new { ClrType = @event.GetType().AssemblyQualifiedName }; @@ -30,7 +30,7 @@ public static EventData ToEventData(Guid eventId, object @event, JsonSerializerS var metadata = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(eventHeaders, serializerSettings)); var typeName = @event.GetType().Name; - return new EventData(eventId, typeName, true, data, metadata); + return new EventData(eventId, typeName, data, metadata); } /// @@ -52,7 +52,7 @@ public static EventData ToEventData(Guid eventId, object @event, JsonSerializerS /// An of the provided build from the JSON data contained in the provided . public static object FromResolvedEvent(ResolvedEvent @event, Type typeToConvertTo, JsonSerializerSettings serializerSettings = null) { - var eventString = Encoding.UTF8.GetString(@event.Event.Data); + var eventString = Encoding.UTF8.GetString(@event.Event.Data.Span); return JsonConvert.DeserializeObject(eventString, typeToConvertTo, serializerSettings); } } diff --git a/src/Tools.EventStore/Tools.EventStore.csproj b/src/Tools.EventStore/Tools.EventStore.csproj index 35dd476..33808bc 100644 --- a/src/Tools.EventStore/Tools.EventStore.csproj +++ b/src/Tools.EventStore/Tools.EventStore.csproj @@ -1,7 +1,7 @@  - netstandard2.0 + net6.0 CorshamScience.Tools.EventStore CorshamScience.Tools.EventStore CorshamScience.Tools @@ -35,7 +35,9 @@ All - + + +