Skip to content

Commit

Permalink
Event Handling for #48
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanpaulovich committed Feb 4, 2020
1 parent c3c58aa commit 2db9083
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 52 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<PropertyGroup>
<PackageId>FluentMediator.Microsoft.Extensions.DependencyInjection</PackageId>
<Version>0.4.6</Version>
<Version>0.4.7</Version>
<Authors>Ivan Paulovich</Authors>
<Copyright>Ivan Paulovich</Copyright>
<Description>Microsoft Extensions for FluentMediator.</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ namespace FluentMediator
/// </summary>
public static class FluentMediatorExtensions
{
/// <summary>
/// Adds the FluentMediator
/// </summary>
/// <param name="services">The ServiceCollection</param>
/// <param name="setupAction">Builder</param>
/// <returns>The changed ServiceCollection</returns>
public static IServiceCollection AddFluentMediator<TMediator>(
this IServiceCollection services,
Action<IPipelineProviderBuilder> setupAction)
where TMediator : class, IMediator
{
var pipelineProviderBuilder = new PipelineProviderBuilder();
setupAction(pipelineProviderBuilder);
var pipelineProvider = pipelineProviderBuilder.Build();

services.AddTransient<GetService>(c => c.GetService);
services.AddTransient(c => pipelineProvider);
services.AddTransient<IMediator, TMediator>();

return services;
}

/// <summary>
/// Adds the FluentMediator
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/FluentMediator/FluentMediator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<PropertyGroup>
<PackageId>FluentMediator</PackageId>
<Version>0.4.6</Version>
<Version>0.4.7</Version>
<Authors>Ivan Paulovich</Authors>
<Copyright>Ivan Paulovich</Copyright>
<Description>FluentMediator is an unobtrusive library that allows developers to build custom pipelines for Commands, Queries and Events.</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/FluentMediator/Pipelines/PipelineNotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace FluentMediator.Pipelines
/// <summary>
/// Occurs when a pipeline for a message was not found
/// </summary>
public sealed class PipelineNotFoundException : MediatorException
internal sealed class PipelineNotFoundException : MediatorException
{
/// <summary>
/// Instantiate a PipelineNotFoundException
Expand Down
22 changes: 22 additions & 0 deletions test/UnitTests/MyCustomMediator.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
}
48 changes: 17 additions & 31 deletions test/UnitTests/PublishingRequestsTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<IMediator>();

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<PipelineNotFoundException>(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<IMediator>();
// 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<PipelineNotFoundException>(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<MyCustomMediator>(builder => { });

var provider = services.BuildServiceProvider();
var mediator = provider.GetRequiredService<IMediator>();

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<IMediator>();

var cts = new CancellationTokenSource();
var ping = new PingRequest("Cancellable Async Ping");
Expand All @@ -179,7 +165,7 @@ public void Publish_CallsOnPipelineNotFound_WhenHandlerIsNotSetup()
mediator.Publish(ping);

// The method was called :)
Assert.True(myCustomPipelineNotFoundHandlerWasCalled);
Assert.True(mediator.MyCustomPipelineNotFoundHandlerWasCalled);
}
}
}
18 changes: 0 additions & 18 deletions test/UnitTests/SendingRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IPingHandler, PingHandler>();
var provider = services.BuildServiceProvider();
var mediator = provider.GetRequiredService<IMediator>();

var ping = new PingRequest("Ping");
var actualEx = Record.Exception(() => mediator.Send<PingResponse>(ping));

Assert.NotNull(actualEx);
Assert.IsType<PipelineNotFoundException>(actualEx);
}

[Fact]
public void Send_Throws_Exception_Null_Requests()
{
Expand Down

0 comments on commit 2db9083

Please sign in to comment.