diff --git a/changelog.md b/changelog.md index e9ebdf2..e114d0a 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # Change Log +## 2020-02-04 - 0.4.7 + +- #48 Changed `PipelineNotFoundException` to internal. +- #48 Removed sealed modified from Mediator to allow derived implementations. +- #48 Added `OnPipelineNotFound` event. + ## 2019-10-20 - 0.4.6 * #25 Cancellation Token fixed. diff --git a/src/FluentMediator.Microsoft.Extensions.DependencyInjection/FluentMediator.Microsoft.Extensions.DependencyInjection.csproj b/src/FluentMediator.Microsoft.Extensions.DependencyInjection/FluentMediator.Microsoft.Extensions.DependencyInjection.csproj index 8328356..de5ceeb 100644 --- a/src/FluentMediator.Microsoft.Extensions.DependencyInjection/FluentMediator.Microsoft.Extensions.DependencyInjection.csproj +++ b/src/FluentMediator.Microsoft.Extensions.DependencyInjection/FluentMediator.Microsoft.Extensions.DependencyInjection.csproj @@ -7,7 +7,7 @@ FluentMediator.Microsoft.Extensions.DependencyInjection - 0.4.6 + 0.4.7 Ivan Paulovich Ivan Paulovich Microsoft Extensions for FluentMediator. diff --git a/src/FluentMediator.Microsoft.Extensions.DependencyInjection/FluentMediatorExtensions.cs b/src/FluentMediator.Microsoft.Extensions.DependencyInjection/FluentMediatorExtensions.cs index b90061a..0934013 100644 --- a/src/FluentMediator.Microsoft.Extensions.DependencyInjection/FluentMediatorExtensions.cs +++ b/src/FluentMediator.Microsoft.Extensions.DependencyInjection/FluentMediatorExtensions.cs @@ -8,6 +8,28 @@ namespace FluentMediator /// public static class FluentMediatorExtensions { + /// + /// Adds the FluentMediator + /// + /// The ServiceCollection + /// Builder + /// The changed ServiceCollection + public static IServiceCollection AddFluentMediator( + this IServiceCollection services, + Action setupAction) + where TMediator : class, IMediator + { + var pipelineProviderBuilder = new PipelineProviderBuilder(); + setupAction(pipelineProviderBuilder); + var pipelineProvider = pipelineProviderBuilder.Build(); + + services.AddTransient(c => c.GetService); + services.AddTransient(c => pipelineProvider); + services.AddTransient(); + + return services; + } + /// /// Adds the FluentMediator /// diff --git a/src/FluentMediator/FluentMediator.csproj b/src/FluentMediator/FluentMediator.csproj index a45f8df..50cdf0a 100644 --- a/src/FluentMediator/FluentMediator.csproj +++ b/src/FluentMediator/FluentMediator.csproj @@ -11,7 +11,7 @@ FluentMediator - 0.4.6 + 0.4.7 Ivan Paulovich Ivan Paulovich FluentMediator is an unobtrusive library that allows developers to build custom pipelines for Commands, Queries and Events. diff --git a/src/FluentMediator/Pipelines/PipelineNotFoundException.cs b/src/FluentMediator/Pipelines/PipelineNotFoundException.cs index f9e423f..bde4a55 100644 --- a/src/FluentMediator/Pipelines/PipelineNotFoundException.cs +++ b/src/FluentMediator/Pipelines/PipelineNotFoundException.cs @@ -3,7 +3,7 @@ namespace FluentMediator.Pipelines /// /// Occurs when a pipeline for a message was not found /// - public sealed class PipelineNotFoundException : MediatorException + internal sealed class PipelineNotFoundException : MediatorException { /// /// Instantiate a PipelineNotFoundException diff --git a/test/UnitTests/MyCustomMediator.cs b/test/UnitTests/MyCustomMediator.cs new file mode 100644 index 0000000..238c6f2 --- /dev/null +++ b/test/UnitTests/MyCustomMediator.cs @@ -0,0 +1,22 @@ +using FluentMediator; + +namespace UnitTests +{ + public class MyCustomMediator : Mediator + { + public bool MyCustomPipelineNotFoundHandlerWasCalled = false; + + public MyCustomMediator(GetService getService, IPipelineProvider pipelines) : base(getService, pipelines) + { + } + + protected override void OnPipelineNotFound(PipelineNotFoundEventArgs e) + { + MyCustomPipelineNotFoundHandlerWasCalled = true; + + //Do something before raising the event + base.OnPipelineNotFound(e); + //Do something after raising the event + } + } +} \ No newline at end of file diff --git a/test/UnitTests/PublishingRequestsTests.cs b/test/UnitTests/PublishingRequestsTests.cs index 0c818a5..84b8930 100644 --- a/test/UnitTests/PublishingRequestsTests.cs +++ b/test/UnitTests/PublishingRequestsTests.cs @@ -1,7 +1,6 @@ using System.Threading; using System.Threading.Tasks; using FluentMediator; -using FluentMediator.Pipelines; using Microsoft.Extensions.DependencyInjection; using Moq; using UnitTests.PingPong; @@ -122,55 +121,42 @@ public async Task PublishAsync_Calls_CancellablePipeline_Handlers2() } [Fact] - public async Task PublishAsync_ThrowsPipelineNotFoundException_WhenHandlerIsNotSetup() + public void Publish_CallsPipelineNotFound_WhenHandlerIsNotSetup() { var services = new ServiceCollection(); - // Mediator Without the Handler for PingRequest + // Mediator Without the Handler for PingRequest. services.AddFluentMediator(builder => { }); var provider = services.BuildServiceProvider(); var mediator = provider.GetRequiredService(); - var cts = new CancellationTokenSource(); - var ping = new PingRequest("Cancellable Async Ping"); - - var actualEx = await Record.ExceptionAsync(async () => await mediator.PublishAsync(ping, cts.Token)); - Assert.IsType(actualEx); - } - - [Fact] - public void Publish_ThrowsPipelineNotFoundException_WhenHandlerIsNotSetup() - { - var services = new ServiceCollection(); - // Mediator Without the Handler for PingRequest - services.AddFluentMediator(builder => { }); + bool myCustomPipelineNotFoundHandlerWasCalled = false; - var provider = services.BuildServiceProvider(); - var mediator = provider.GetRequiredService(); + // This is handler is called for every message without a destination pipeline. + mediator.PipelineNotFound += (object sender, PipelineNotFoundEventArgs e) => { + myCustomPipelineNotFoundHandlerWasCalled = true; + }; var cts = new CancellationTokenSource(); var ping = new PingRequest("Cancellable Async Ping"); - var actualEx = Record.Exception(() => mediator.Publish(ping)); - Assert.IsType(actualEx); + // Should run without throwing exceptions + mediator.Publish(ping); + + // The method was called :) + Assert.True(myCustomPipelineNotFoundHandlerWasCalled); } [Fact] - public void Publish_CallsOnPipelineNotFound_WhenHandlerIsNotSetup() + public void Publish_CallsCustomPipelineNotFound_WhenHandlerIsNotSetup() { var services = new ServiceCollection(); + // Mediator Without the Handler for PingRequest. - services.AddFluentMediator(builder => { }); + services.AddFluentMediator(builder => { }); var provider = services.BuildServiceProvider(); - var mediator = provider.GetRequiredService(); - - bool myCustomPipelineNotFoundHandlerWasCalled = false; - - // This is handler is called for every message without a destination pipeline. - mediator.PipelineNotFound += (object sender, PipelineNotFoundEventArgs e) => { - myCustomPipelineNotFoundHandlerWasCalled = true; - }; + var mediator = (MyCustomMediator)provider.GetRequiredService(); var cts = new CancellationTokenSource(); var ping = new PingRequest("Cancellable Async Ping"); @@ -179,7 +165,7 @@ public void Publish_CallsOnPipelineNotFound_WhenHandlerIsNotSetup() mediator.Publish(ping); // The method was called :) - Assert.True(myCustomPipelineNotFoundHandlerWasCalled); + Assert.True(mediator.MyCustomPipelineNotFoundHandlerWasCalled); } } } \ No newline at end of file diff --git a/test/UnitTests/SendingRequestTests.cs b/test/UnitTests/SendingRequestTests.cs index 506bd88..ad0dbc4 100644 --- a/test/UnitTests/SendingRequestTests.cs +++ b/test/UnitTests/SendingRequestTests.cs @@ -228,24 +228,6 @@ public async Task SendCancellable_Named_PipelineReturns_Response() Assert.NotNull(response); } - [Fact] - public void Send_Not_Configured_Throws_PipelineNotFoundException() - { - var services = new ServiceCollection(); - services.AddFluentMediator(m => - { }); - - services.AddScoped(); - var provider = services.BuildServiceProvider(); - var mediator = provider.GetRequiredService(); - - var ping = new PingRequest("Ping"); - var actualEx = Record.Exception(() => mediator.Send(ping)); - - Assert.NotNull(actualEx); - Assert.IsType(actualEx); - } - [Fact] public void Send_Throws_Exception_Null_Requests() {