diff --git a/src/Mediator.DependencyInjection/Builder/MediatorBuilder.Default.cs b/src/Mediator.DependencyInjection/Builder/MediatorBuilder.Default.cs index 55e2749..e155c33 100644 --- a/src/Mediator.DependencyInjection/Builder/MediatorBuilder.Default.cs +++ b/src/Mediator.DependencyInjection/Builder/MediatorBuilder.Default.cs @@ -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))); @@ -18,6 +24,12 @@ public IMediatorBuilder AddDefaultRequestHandler(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))); @@ -30,6 +42,12 @@ public IMediatorBuilder AddDefaultNotificationHandler(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))); diff --git a/src/Mediator/Exceptions/HandlerNotFoundException.cs b/src/Mediator/Exceptions/HandlerNotFoundException.cs index fae9af6..bdf4099 100644 --- a/src/Mediator/Exceptions/HandlerNotFoundException.cs +++ b/src/Mediator/Exceptions/HandlerNotFoundException.cs @@ -3,7 +3,7 @@ namespace Zapto.Mediator; -public class HandlerNotFoundException : Exception +public class HandlerNotFoundException : InvalidOperationException { public HandlerNotFoundException() { diff --git a/src/Mediator/IMediatorBuilder.cs b/src/Mediator/IMediatorBuilder.cs index 529bd74..f20b5d8 100644 --- a/src/Mediator/IMediatorBuilder.cs +++ b/src/Mediator/IMediatorBuilder.cs @@ -119,14 +119,20 @@ IMediatorBuilder AddStreamRequestHandler(IStreamRequestHand public IMediatorBuilder AddStreamRequestHandler(Func> handler) where TRequest : IStreamRequest; + IMediatorBuilder AddDefaultRequestHandler(IDefaultRequestHandler handler); + IMediatorBuilder AddDefaultRequestHandler(Type handlerType, RegistrationScope scope = RegistrationScope.Transient); IMediatorBuilder AddDefaultRequestHandler(RegistrationScope scope = RegistrationScope.Transient) where THandler : class, IDefaultRequestHandler; + IMediatorBuilder AddDefaultNotificationHandler(IDefaultNotificationHandler handler); + IMediatorBuilder AddDefaultNotificationHandler(Type handlerType, RegistrationScope scope = RegistrationScope.Transient); IMediatorBuilder AddDefaultNotificationHandler(RegistrationScope scope = RegistrationScope.Transient) where THandler : class, IDefaultNotificationHandler; + IMediatorBuilder AddDefaultStreamRequestHandler(IDefaultStreamRequestHandler handler); + IMediatorBuilder AddDefaultStreamRequestHandler(Type handlerType, RegistrationScope scope = RegistrationScope.Transient); IMediatorBuilder AddDefaultStreamRequestHandler(RegistrationScope scope = RegistrationScope.Transient) where THandler : class, IDefaultStreamRequestHandler; diff --git a/tests/Mediator.DependencyInjection.Tests/DefaultHandler/NotificationDefaultHandlerTest.cs b/tests/Mediator.DependencyInjection.Tests/DefaultHandler/NotificationDefaultHandlerTest.cs new file mode 100644 index 0000000..c793965 --- /dev/null +++ b/tests/Mediator.DependencyInjection.Tests/DefaultHandler/NotificationDefaultHandlerTest.cs @@ -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(); + + var serviceProvider = new ServiceCollection() + .AddMediator(b => b.AddDefaultNotificationHandler(handler)) + .BuildServiceProvider(); + + var mediator = serviceProvider.GetRequiredService(); + + await mediator.Publish(new Notification()); + + _ = handler.Received() + .Handle(Arg.Any(), Arg.Any(), Arg.Any()); + } +} diff --git a/tests/Mediator.DependencyInjection.Tests/DefaultHandler/RequestDefaultHandlerTest.cs b/tests/Mediator.DependencyInjection.Tests/DefaultHandler/RequestDefaultHandlerTest.cs new file mode 100644 index 0000000..e5a11c7 --- /dev/null +++ b/tests/Mediator.DependencyInjection.Tests/DefaultHandler/RequestDefaultHandlerTest.cs @@ -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(); + + var serviceProvider = new ServiceCollection() + .AddMediator(b => b.AddDefaultRequestHandler(handler)) + .BuildServiceProvider(); + + var mediator = serviceProvider.GetRequiredService(); + + await mediator.Send(new Request()); + + _ = handler.Received() + .Handle(Arg.Any(), Arg.Any(), Arg.Any()); + } +} diff --git a/tests/Mediator.DependencyInjection.Tests/DefaultHandler/StreamRequestDefaultHandlerTest.cs b/tests/Mediator.DependencyInjection.Tests/DefaultHandler/StreamRequestDefaultHandlerTest.cs new file mode 100644 index 0000000..fa244d7 --- /dev/null +++ b/tests/Mediator.DependencyInjection.Tests/DefaultHandler/StreamRequestDefaultHandlerTest.cs @@ -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(); + + handler.Handle(Arg.Any(), Arg.Any(), Arg.Any()) + .Returns(Array.Empty().ToAsyncEnumerable()); + + var serviceProvider = new ServiceCollection() + .AddMediator(b => b.AddDefaultStreamRequestHandler(handler)) + .BuildServiceProvider(); + + var mediator = serviceProvider.GetRequiredService(); + + await mediator + .CreateStream(new StreamRequest()) + .ToListAsync(); + + _ = handler.Received() + .Handle(Arg.Any(), Arg.Any(), Arg.Any()); + } +} diff --git a/tests/Mediator.DependencyInjection.Tests/RequestTest.cs b/tests/Mediator.DependencyInjection.Tests/RequestTest.cs index fe0dc98..30b59af 100644 --- a/tests/Mediator.DependencyInjection.Tests/RequestTest.cs +++ b/tests/Mediator.DependencyInjection.Tests/RequestTest.cs @@ -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(); + + await Assert.ThrowsAsync(() => mediator.Send(new Request()).AsTask()); + } + + [Fact] + public async Task TestNoHandlerNamespace() + { + var ns = new MediatorNamespace("test"); + + var serviceProvider = new ServiceCollection() + .AddMediator(_ => {}) + .BuildServiceProvider(); + + var mediator = serviceProvider.GetRequiredService(); + + await Assert.ThrowsAsync(() => mediator.Send(ns, new Request()).AsTask()); + } } diff --git a/tests/Mediator.DependencyInjection.Tests/StreamRequestTest.cs b/tests/Mediator.DependencyInjection.Tests/StreamRequestTest.cs index cfe2aa6..a6b9193 100644 --- a/tests/Mediator.DependencyInjection.Tests/StreamRequestTest.cs +++ b/tests/Mediator.DependencyInjection.Tests/StreamRequestTest.cs @@ -111,4 +111,33 @@ await mediator _ = handler.Received(requiredNumberOfCalls: 2) .Handle(Arg.Any(), Arg.Any(), Arg.Any()); } + + [Fact] + public async Task TestNoHandler() + { + var serviceProvider = new ServiceCollection() + .AddMediator(_ => { }) + .BuildServiceProvider(); + + var mediator = serviceProvider.GetRequiredService(); + + await Assert.ThrowsAsync(() => mediator + .CreateStream(new StreamRequest()) + .ToListAsync().AsTask()); + } + + [Fact] + public async Task TestNoHandlerNamespace() + { + var ns = new MediatorNamespace("test"); + var serviceProvider = new ServiceCollection() + .AddMediator(_ => { }) + .BuildServiceProvider(); + + var mediator = serviceProvider.GetRequiredService(); + + await Assert.ThrowsAsync(() => mediator + .CreateStream(ns, new StreamRequest()) + .ToListAsync().AsTask()); + } }