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 7e175aa commit c3c58aa
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 9 deletions.
9 changes: 8 additions & 1 deletion src/FluentMediator/IMediator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using FluentMediator.Pipelines.CancellablePipelineAsync;
using FluentMediator.Pipelines.Pipeline;
using FluentMediator.Pipelines.PipelineAsync;
Expand All @@ -10,5 +11,11 @@ namespace FluentMediator
public interface IMediator:
ISyncMediator,
IAsyncMediator,
ICancellableMediator { }
ICancellableMediator
{
/// <summary>
/// On Pipeline Not Found Event Handler.
/// </summary>
event EventHandler<PipelineNotFoundEventArgs>? PipelineNotFound;
}
}
43 changes: 35 additions & 8 deletions src/FluentMediator/Mediator.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using FluentMediator.Pipelines;

namespace FluentMediator
{
/// <summary>
/// Publishes/Sends messages through the Pipelines
/// </summary>
public sealed class Mediator : IMediator
public class Mediator : IMediator
{
/// <summary>
/// Returns a service from the Container
/// </summary>
/// <value></value>
public GetService GetService { get; }

private IPipelineProvider _pipelines;

/// <summary>
/// On Pipeline Not Found Handler.
/// </summary>
public event EventHandler<PipelineNotFoundEventArgs>? PipelineNotFound;

/// <summary>
/// Instantiate a Mediator
/// </summary>
Expand All @@ -40,16 +48,23 @@ public void Publish(object request, string? pipelineName = null)
throw new NullRequestException("The request is null.");
}

if (pipelineName is string)
try
{
var pipeline = _pipelines.GetPipeline(pipelineName);
pipeline.Publish(GetService, request!);

if (pipelineName is string)
{
var pipeline = _pipelines.GetPipeline(pipelineName);
pipeline.Publish(GetService, request!);
}
else
{
var pipeline = _pipelines.GetPipeline(request.GetType());
pipeline.Publish(GetService, request!);
}
}
else
catch (PipelineNotFoundException)
{
var pipeline = _pipelines.GetPipeline(request.GetType());
pipeline.Publish(GetService, request!);
var e = new PipelineNotFoundEventArgs(request);
OnPipelineNotFound(e);
}
}

Expand Down Expand Up @@ -182,5 +197,17 @@ public async Task<TResult> SendAsync<TResult>(object request, CancellationToken
return await pipeline.SendAsync<TResult>(GetService, request, cancellationToken);
}
}

/// <summary>
/// On Pipeline Not Found.
/// </summary>
/// <param name="e">OnErrorEventArgs.</param>
protected virtual void OnPipelineNotFound(PipelineNotFoundEventArgs e)
{
if (this.PipelineNotFound is EventHandler<PipelineNotFoundEventArgs>)
{
this.PipelineNotFound(this, e);
}
}
}
}
25 changes: 25 additions & 0 deletions src/FluentMediator/PipelineNotFoundEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace FluentMediator
{
/// <summary>
/// On Error Event.
/// </summary>
public class PipelineNotFoundEventArgs : EventArgs
{
/// <summary>
///
/// </summary>
/// <value></value>
public object Message { get; }

/// <summary>
///
/// </summary>
/// <param name="message"></param>
public PipelineNotFoundEventArgs(object message)
{
this.Message = message;
}
}
}
27 changes: 27 additions & 0 deletions test/UnitTests/PublishingRequestsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,32 @@ public void Publish_ThrowsPipelineNotFoundException_WhenHandlerIsNotSetup()
var actualEx = Record.Exception(() => mediator.Publish(ping));
Assert.IsType<PipelineNotFoundException>(actualEx);
}

[Fact]
public void Publish_CallsOnPipelineNotFound_WhenHandlerIsNotSetup()
{
var services = new ServiceCollection();
// Mediator Without the Handler for PingRequest.
services.AddFluentMediator(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 cts = new CancellationTokenSource();
var ping = new PingRequest("Cancellable Async Ping");

// Should run without throwing exceptions
mediator.Publish(ping);

// The method was called :)
Assert.True(myCustomPipelineNotFoundHandlerWasCalled);
}
}
}

0 comments on commit c3c58aa

Please sign in to comment.