Skip to content

Commit

Permalink
added DI utils for transport and persistence serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
mizrael committed Aug 25, 2021
1 parent f7a4397 commit 91b31cf
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/OpenSleigh.Core/DependencyInjection/BusConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ public IBusConfigurator WithOutboxCleanerOptions(OutboxCleanerOptions options)
return this;
}

public IBusConfigurator WithTransportSerializer<T>(T serializer = null) where T : class, ITransportSerializer
{
if(serializer is null)
this.Services.Replace(ServiceDescriptor.Singleton<ITransportSerializer, T>());
else
this.Services.Replace(ServiceDescriptor.Singleton<ITransportSerializer, T>(_ => serializer));

return this;
}

public IBusConfigurator WithPersistenceSerializer<T>(T serializer = null) where T : class, IPersistenceSerializer
{
if (serializer is null)
this.Services.Replace(ServiceDescriptor.Singleton<IPersistenceSerializer, T>());
else
this.Services.Replace(ServiceDescriptor.Singleton<IPersistenceSerializer, T>(_ => serializer));

return this;
}

public IMessageHandlerConfigurator<TM> AddMessageHandlers<TM>(IEnumerable<Assembly> sourceAssemblies)
where TM : IMessage
{
Expand Down
15 changes: 15 additions & 0 deletions src/OpenSleigh.Core/DependencyInjection/IBusConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using OpenSleigh.Core.Messaging;
using OpenSleigh.Core.Utils;

namespace OpenSleigh.Core.DependencyInjection
{
Expand All @@ -28,6 +29,20 @@ ISagaConfigurator<TS, TD> AddSaga<TS, TD>()
where TS : Saga<TD>
where TD : SagaState;

/// <summary>
/// replaces the current registered Transport Serializer.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="serializer">instance of the serializer that will be used. If null, its type will be used to perform the registration.</param>
IBusConfigurator WithTransportSerializer<T>(T serializer = null) where T : class, ITransportSerializer;

/// <summary>
/// replaces the current registered Persistence Serializer.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="serializer">instance of the serializer that will be used. If null, its type will be used to perform the registration.</param>
IBusConfigurator WithPersistenceSerializer<T>(T serializer = null) where T : class, IPersistenceSerializer;

IServiceCollection Services { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public static IServiceCollection AddOpenSleigh(this IServiceCollection services,
.AddSingleton<ISagasRunner, SagasRunner>()
.AddSingleton<ITypesCache, TypesCache>()
.AddSingleton<ITypeResolver>(typeResolver)

.AddSingleton<ITransportSerializer, JsonSerializer>()
.AddSingleton<IPersistenceSerializer, JsonSerializer>()

.AddSingleton<IMessageHandlersResolver, DefaultMessageHandlersResolver>()
.AddSingleton<IMessageHandlersRunner, DefaultMessageHandlersRunner>()
.AddSingleton<IMessageContextFactory, DefaultMessageContextFactory>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Xunit;
using NSubstitute;
using OpenSleigh.Core.Tests.Sagas;
using OpenSleigh.Core.Utils;

namespace OpenSleigh.Core.Tests.Unit.DependencyInjection
{
Expand Down Expand Up @@ -32,5 +33,77 @@ public void AddMessageHandlers_should_add_all_available_message_handlers_from_as
sd.ServiceType == typeof(IHandleMessage<DummyMessage>) &&
sd.ImplementationType.IsAssignableTo(typeof(DummyMessageHandler))));
}

[Fact]
public void WithTransportSerializer_should_replace_existing_registration()
{
var services = new ServiceCollection();
var sagaTypeResolver = NSubstitute.Substitute.For<ISagaTypeResolver>();
var typeResolver = NSubstitute.Substitute.For<ITypeResolver>();
var sysInfo = SystemInfo.New();

var sut = new BusConfigurator(services, sagaTypeResolver, typeResolver, sysInfo);

var newSerializer = new FakeTransportSerializer();
sut.WithTransportSerializer(newSerializer);

var sp = services.BuildServiceProvider();
var serializer = sp.GetService<ITransportSerializer>();
serializer.Should().Be(newSerializer);
}

[Fact]
public void WithTransportSerializer_should_replace_existing_registration_when_input_null()
{
var services = new ServiceCollection();
var sagaTypeResolver = NSubstitute.Substitute.For<ISagaTypeResolver>();
var typeResolver = NSubstitute.Substitute.For<ITypeResolver>();
var sysInfo = SystemInfo.New();

var sut = new BusConfigurator(services, sagaTypeResolver, typeResolver, sysInfo);

sut.WithTransportSerializer<FakeTransportSerializer>();

var sp = services.BuildServiceProvider();
var serializer = sp.GetService<ITransportSerializer>();
serializer.Should().NotBeNull()
.And.BeOfType<FakeTransportSerializer>();
}

[Fact]
public void WithPersistenceSerializer_should_replace_existing_registration()
{
var services = new ServiceCollection();
var sagaTypeResolver = NSubstitute.Substitute.For<ISagaTypeResolver>();
var typeResolver = NSubstitute.Substitute.For<ITypeResolver>();
var sysInfo = SystemInfo.New();

var sut = new BusConfigurator(services, sagaTypeResolver, typeResolver, sysInfo);

var newSerializer = new FakePersistenceSerializer();
sut.WithPersistenceSerializer(newSerializer);

var sp = services.BuildServiceProvider();
var serializer = sp.GetService<IPersistenceSerializer>();
serializer.Should().Be(newSerializer);
}

[Fact]
public void WithPersistenceSerializer_should_replace_existing_registration_when_input_null()
{
var services = new ServiceCollection();
var sagaTypeResolver = NSubstitute.Substitute.For<ISagaTypeResolver>();
var typeResolver = NSubstitute.Substitute.For<ITypeResolver>();
var sysInfo = SystemInfo.New();

var sut = new BusConfigurator(services, sagaTypeResolver, typeResolver, sysInfo);

sut.WithPersistenceSerializer<FakePersistenceSerializer>();

var sp = services.BuildServiceProvider();
var serializer = sp.GetService<IPersistenceSerializer>();
serializer.Should().NotBeNull()
.And.BeOfType<FakePersistenceSerializer>();
}
}
}
20 changes: 20 additions & 0 deletions tests/OpenSleigh.Core.Tests/Unit/FakePersistenceSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using OpenSleigh.Core.Utils;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace OpenSleigh.Core.Tests.Unit
{
public class FakePersistenceSerializer : IPersistenceSerializer
{
public ValueTask<T> DeserializeAsync<T>(ReadOnlyMemory<byte> data, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public ValueTask<byte[]> SerializeAsync<T>(T state, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
}
25 changes: 25 additions & 0 deletions tests/OpenSleigh.Core.Tests/Unit/FakeTransportSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using OpenSleigh.Core.Utils;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace OpenSleigh.Core.Tests.Unit
{
public class FakeTransportSerializer : ITransportSerializer
{
public object Deserialize(ReadOnlyMemory<byte> data, Type type)
{
throw new NotImplementedException();
}

public ValueTask<T> DeserializeAsync<T>(ReadOnlyMemory<byte> data, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public ValueTask<byte[]> SerializeAsync<T>(T state, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
}

0 comments on commit 91b31cf

Please sign in to comment.