Skip to content

Commit

Permalink
feat: singleton default handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardSmit committed Dec 21, 2023
1 parent 0e94449 commit ccb7e0f
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ namespace Zapto.Mediator;

public partial class MediatorBuilder : IMediatorBuilder
{
public IMediatorBuilder AddDefaultRequestHandler(IDefaultRequestHandler handler)
{
_services.AddSingleton(handler);
return this;
}

public IMediatorBuilder AddDefaultRequestHandler(Type handlerType, RegistrationScope scope = RegistrationScope.Transient)
{
_services.Add(new ServiceDescriptor(typeof(IDefaultRequestHandler), handlerType, GetLifetime(scope)));
Expand All @@ -18,6 +24,12 @@ public IMediatorBuilder AddDefaultRequestHandler<THandler>(RegistrationScope sco
return this;
}

public IMediatorBuilder AddDefaultNotificationHandler(IDefaultNotificationHandler handler)
{
_services.AddSingleton(handler);
return this;
}

public IMediatorBuilder AddDefaultNotificationHandler(Type handlerType, RegistrationScope scope = RegistrationScope.Transient)
{
_services.Add(new ServiceDescriptor(typeof(IDefaultNotificationHandler), handlerType, GetLifetime(scope)));
Expand All @@ -30,6 +42,12 @@ public IMediatorBuilder AddDefaultNotificationHandler<THandler>(RegistrationScop
return this;
}

public IMediatorBuilder AddDefaultStreamRequestHandler(IDefaultStreamRequestHandler handler)
{
_services.AddSingleton(handler);
return this;
}

public IMediatorBuilder AddDefaultStreamRequestHandler(Type handlerType, RegistrationScope scope = RegistrationScope.Transient)
{
_services.Add(new ServiceDescriptor(typeof(IDefaultStreamRequestHandler), handlerType, GetLifetime(scope)));
Expand Down
2 changes: 1 addition & 1 deletion src/Mediator/Exceptions/HandlerNotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Zapto.Mediator;

public class HandlerNotFoundException : Exception
public class HandlerNotFoundException : InvalidOperationException
{
public HandlerNotFoundException()
{
Expand Down
6 changes: 6 additions & 0 deletions src/Mediator/IMediatorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,20 @@ IMediatorBuilder AddStreamRequestHandler<TRequest, TResponse>(IStreamRequestHand
public IMediatorBuilder AddStreamRequestHandler<TRequest, TResponse>(Func<IServiceProvider, TRequest, IAsyncEnumerable<TResponse>> handler)
where TRequest : IStreamRequest<TResponse>;

IMediatorBuilder AddDefaultRequestHandler(IDefaultRequestHandler handler);

IMediatorBuilder AddDefaultRequestHandler(Type handlerType, RegistrationScope scope = RegistrationScope.Transient);

IMediatorBuilder AddDefaultRequestHandler<THandler>(RegistrationScope scope = RegistrationScope.Transient) where THandler : class, IDefaultRequestHandler;

IMediatorBuilder AddDefaultNotificationHandler(IDefaultNotificationHandler handler);

IMediatorBuilder AddDefaultNotificationHandler(Type handlerType, RegistrationScope scope = RegistrationScope.Transient);

IMediatorBuilder AddDefaultNotificationHandler<THandler>(RegistrationScope scope = RegistrationScope.Transient) where THandler : class, IDefaultNotificationHandler;

IMediatorBuilder AddDefaultStreamRequestHandler(IDefaultStreamRequestHandler handler);

IMediatorBuilder AddDefaultStreamRequestHandler(Type handlerType, RegistrationScope scope = RegistrationScope.Transient);

IMediatorBuilder AddDefaultStreamRequestHandler<THandler>(RegistrationScope scope = RegistrationScope.Transient) where THandler : class, IDefaultStreamRequestHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Xunit;
using Zapto.Mediator;

namespace Mediator.DependencyInjection.Tests.DefaultHandler;

public class NotificationDefaultHandlerTest
{
[Fact]
public async Task TestNotification()
{
var handler = Substitute.For<IDefaultNotificationHandler>();

var serviceProvider = new ServiceCollection()
.AddMediator(b => b.AddDefaultNotificationHandler(handler))
.BuildServiceProvider();

var mediator = serviceProvider.GetRequiredService<IMediator>();

await mediator.Publish(new Notification());

_ = handler.Received()
.Handle(Arg.Any<IServiceProvider>(), Arg.Any<Notification>(), Arg.Any<CancellationToken>());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Xunit;
using Zapto.Mediator;

namespace Mediator.DependencyInjection.Tests.DefaultHandler;

public class RequestDefaultHandlerTest
{
[Fact]
public async Task TestRequest()
{
var handler = Substitute.For<IDefaultRequestHandler>();

var serviceProvider = new ServiceCollection()
.AddMediator(b => b.AddDefaultRequestHandler(handler))
.BuildServiceProvider();

var mediator = serviceProvider.GetRequiredService<IMediator>();

await mediator.Send<Request, int>(new Request());

_ = handler.Received()
.Handle<Request, int>(Arg.Any<IServiceProvider>(), Arg.Any<Request>(), Arg.Any<CancellationToken>());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Xunit;
using Zapto.Mediator;

namespace Mediator.DependencyInjection.Tests.DefaultHandler;

public class StreamRequestDefaultHandlerTest
{
[Fact]
public async Task TestStream()
{
var handler = Substitute.For<IDefaultStreamRequestHandler>();

handler.Handle<StreamRequest, int>(Arg.Any<IServiceProvider>(), Arg.Any<StreamRequest>(), Arg.Any<CancellationToken>())
.Returns(Array.Empty<int>().ToAsyncEnumerable());

var serviceProvider = new ServiceCollection()
.AddMediator(b => b.AddDefaultStreamRequestHandler(handler))
.BuildServiceProvider();

var mediator = serviceProvider.GetRequiredService<IMediator>();

await mediator
.CreateStream<StreamRequest, int>(new StreamRequest())
.ToListAsync();

_ = handler.Received()
.Handle<StreamRequest, int>(Arg.Any<IServiceProvider>(), Arg.Any<StreamRequest>(), Arg.Any<CancellationToken>());
}
}
26 changes: 26 additions & 0 deletions tests/Mediator.DependencyInjection.Tests/RequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,30 @@ public async Task TestCollection()

Assert.Equal(0, result.Count);
}

[Fact]
public async Task TestNoHandler()
{
var serviceProvider = new ServiceCollection()
.AddMediator(_ => {})
.BuildServiceProvider();

var mediator = serviceProvider.GetRequiredService<IMediator>();

await Assert.ThrowsAsync<HandlerNotFoundException>(() => mediator.Send<Request, int>(new Request()).AsTask());
}

[Fact]
public async Task TestNoHandlerNamespace()
{
var ns = new MediatorNamespace("test");

var serviceProvider = new ServiceCollection()
.AddMediator(_ => {})
.BuildServiceProvider();

var mediator = serviceProvider.GetRequiredService<IMediator>();

await Assert.ThrowsAsync<NamespaceHandlerNotFoundException>(() => mediator.Send<Request, int>(ns, new Request()).AsTask());
}
}
29 changes: 29 additions & 0 deletions tests/Mediator.DependencyInjection.Tests/StreamRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,33 @@ await mediator
_ = handler.Received(requiredNumberOfCalls: 2)
.Handle(Arg.Any<IServiceProvider>(), Arg.Any<StreamRequest>(), Arg.Any<CancellationToken>());
}

[Fact]
public async Task TestNoHandler()
{
var serviceProvider = new ServiceCollection()
.AddMediator(_ => { })
.BuildServiceProvider();

var mediator = serviceProvider.GetRequiredService<IMediator>();

await Assert.ThrowsAsync<HandlerNotFoundException>(() => mediator
.CreateStream<StreamRequest, int>(new StreamRequest())
.ToListAsync().AsTask());
}

[Fact]
public async Task TestNoHandlerNamespace()
{
var ns = new MediatorNamespace("test");
var serviceProvider = new ServiceCollection()
.AddMediator(_ => { })
.BuildServiceProvider();

var mediator = serviceProvider.GetRequiredService<IMediator>();

await Assert.ThrowsAsync<NamespaceHandlerNotFoundException>(() => mediator
.CreateStream<StreamRequest, int>(ns, new StreamRequest())
.ToListAsync().AsTask());
}
}

0 comments on commit ccb7e0f

Please sign in to comment.