Skip to content

Commit

Permalink
Scoped and Transient lifetime added #31
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanpaulovich committed Oct 19, 2019
1 parent 60b2ebf commit 4d83168
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<PropertyGroup>
<PackageId>FluentMediator.Microsoft.Extensions.DependencyInjection</PackageId>
<Version>0.3.2</Version>
<Version>0.3.3</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
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,48 @@ namespace FluentMediator
{
public static class FluentMediatorExtensions
{
public static IServiceCollection AddFluentMediator(this IServiceCollection services, Action<IPipelinesBuilder> setupAction)
public static IServiceCollection AddFluentMediator(
this IServiceCollection services,
Action<IPipelinesBuilder> setupAction)
{
var pipelinesBuilder = new PipelinesBuilder();
setupAction(pipelinesBuilder);
var pipelines = pipelinesBuilder.Build();

services.AddTransient<GetService>(c => c.GetService);
services.AddTransient(c => pipelines);
services.AddTransient<GetService>(c => c.GetService);
services.AddTransient<IMediator, Mediator>();

return services;
}

public static IServiceCollection AddSingletonFluentMediator(
this IServiceCollection services,
Action<IPipelinesBuilder> setupAction)
{
var pipelinesBuilder = new PipelinesBuilder();
setupAction(pipelinesBuilder);
var pipelines = pipelinesBuilder.Build();

services.AddSingleton(c => pipelines);
services.AddSingleton<GetService>(c => c.GetService);
services.AddSingleton<IMediator, Mediator>();

return services;;
}

public static IServiceCollection AddScopedFluentMediator(
this IServiceCollection services,
Action<IPipelinesBuilder> setupAction)
{
var pipelinesBuilder = new PipelinesBuilder();
setupAction(pipelinesBuilder);
var pipelines = pipelinesBuilder.Build();

services.AddScoped(c => pipelines);
services.AddScoped<GetService>(c => c.GetService);
services.AddScoped<IMediator, Mediator>();

return services;
}
}
Expand Down
40 changes: 0 additions & 40 deletions test/UnitTests/BuildPipelineTests.cs

This file was deleted.

31 changes: 31 additions & 0 deletions test/UnitTests/BuildingMediatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using FluentMediator;
using FluentMediator.Pipelines;
using UnitTests.PingPong;
using Xunit;

namespace UnitTests
{
public class BuildingMediatorTests
{
[Fact]
public void Build_DuplicatedRequest_ThrowsPipelineAlreadyExistsException()
{
var m = new PipelinesBuilder();

m.On<PingRequest>().AsyncPipeline()
.Return<PingResponse, IPingHandler>(
(handler, req) => handler.MyCustomFooBarAsync(req)
);
m.On<PingRequest>().AsyncPipeline()
.Return<PingResponse, IPingHandler>(
(handler, req) => handler.MyCustomFooBarAsync(req)
);

var ex = Record.Exception(() => m.Build());

Assert.NotNull(ex);
Assert.IsType<PipelineAlreadyExistsException>(ex);
}
}
}
8 changes: 4 additions & 4 deletions test/UnitTests/PingPong/IPingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace UnitTests.PingPong
{
public interface IPingHandler
{
PingResponse MyMethod(PingRequest request);
PingResponse MyLongMethod(PingRequest request);
Task<PingResponse> MyMethodAsync(PingRequest request);
Task<PingResponse> MyMethodAsync(PingRequest request, CancellationToken cancelationToken);
PingResponse MyCustomFooMethod(PingRequest request);
PingResponse MyCustomBarMethod(PingRequest request);
Task<PingResponse> MyCustomFooBarAsync(PingRequest request);
Task<PingResponse> MyCancellableForAsync(PingRequest request, CancellationToken cancelationToken);
}
}
8 changes: 4 additions & 4 deletions test/UnitTests/PingPong/PingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ namespace UnitTests.PingPong
{
public class PingHandler : IPingHandler
{
public PingResponse MyLongMethod(PingRequest request)
public PingResponse MyCustomBarMethod(PingRequest request)
{
return new PingResponse("Pong");
}

public PingResponse MyMethod(PingRequest request)
public PingResponse MyCustomFooMethod(PingRequest request)
{
return new PingResponse("Pong");
}

public Task<PingResponse> MyMethodAsync(PingRequest request)
public Task<PingResponse> MyCustomFooBarAsync(PingRequest request)
{
return Task.FromResult(new PingResponse("Pong"));
}

public Task<PingResponse> MyMethodAsync(PingRequest request, CancellationToken cancelationToken)
public Task<PingResponse> MyCancellableForAsync(PingRequest request, CancellationToken cancelationToken)
{
return Task.FromResult(new PingResponse("Pong"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using FluentMediator;
Expand All @@ -9,17 +8,17 @@

namespace UnitTests
{
public sealed class PublishTests
public sealed class PublishingRequestsTests
{
[Fact]
public void PipelineBuilder()
public void Publish_Calls_Pipeline_Handlers()
{
var services = new ServiceCollection();
services.AddFluentMediator(m =>
{
m.On<PingRequest>().Pipeline()
.Call<IPingHandler>((handler, req) => handler.MyMethod(req))
.Call<IPingHandler>((handler, req) => handler.MyLongMethod(req));
.Call<IPingHandler>((handler, req) => handler.MyCustomFooMethod(req))
.Call<IPingHandler>((handler, req) => handler.MyCustomBarMethod(req));
});
var pingHandler = new Mock<IPingHandler>();
services.AddScoped(provider => pingHandler.Object);
Expand All @@ -28,20 +27,24 @@ public void PipelineBuilder()
var mediator = provider.GetRequiredService<IMediator>();

var ping = new PingRequest("Ping");

//
// Act
//
mediator.Publish(ping);

pingHandler.Verify(e => e.MyMethod(ping), Times.Once);
pingHandler.Verify(e => e.MyLongMethod(ping), Times.Once);
pingHandler.Verify(e => e.MyCustomFooMethod(ping), Times.Once);
pingHandler.Verify(e => e.MyCustomBarMethod(ping), Times.Once);
}

[Fact]
public async Task BuildAsyncPipeline()
public async Task PublishAsync_Calls_AsyncPipeline_Handlers()
{
var services = new ServiceCollection();
services.AddFluentMediator(m =>
{
m.On<PingRequest>().AsyncPipeline()
.Call<IPingHandler>(async(handler, req) => await handler.MyMethodAsync(req))
.Call<IPingHandler>(async(handler, req) => await handler.MyCustomFooBarAsync(req))
.Build();
});
var pingHandler = new Mock<IPingHandler>();
Expand All @@ -51,20 +54,24 @@ public async Task BuildAsyncPipeline()
var mediator = provider.GetRequiredService<IMediator>();

var ping = new PingRequest("Async Ping");

//
// Act
//
await mediator.PublishAsync(ping);

pingHandler.Verify(e => e.MyMethodAsync(ping), Times.Once);
pingHandler.Verify(e => e.MyCustomFooBarAsync(ping), Times.Once);
}

[Fact]
public async Task BuildCancellableAsyncPipeline()
public async Task PublishAsync_Calls_CancellablePipeline_Handlers()
{

var services = new ServiceCollection();
services.AddFluentMediator(m =>
{
m.On<PingRequest>().CancellablePipeline()
.Call<IPingHandler>(async(handler, req, ct) => await handler.MyMethodAsync(req, ct))
.Call<IPingHandler>(async(handler, req, ct) => await handler.MyCancellableForAsync(req, ct))
.Build();
});
var pingHandler = new Mock<IPingHandler>();
Expand All @@ -75,21 +82,25 @@ public async Task BuildCancellableAsyncPipeline()

var cts = new CancellationTokenSource();
var ping = new PingRequest("Cancellable Async Ping");

//
// Act
//
await mediator.PublishAsync(ping, cts.Token);

pingHandler.Verify(e => e.MyMethodAsync(ping, It.IsAny<CancellationToken>()), Times.Once);
pingHandler.Verify(e => e.MyCancellableForAsync(ping, It.IsAny<CancellationToken>()), Times.Once);
}

[Fact]
public async Task BuildCancellableAsyncPipelineDirect()
public async Task PublishAsync_Calls_CancellablePipeline_Handlers2()
{
var services = new ServiceCollection();
services.AddFluentMediator(m =>
{
m.On<PingRequest>().CancellablePipeline()
.Call<IPingHandler>(async(handler, req, ct) => await handler.MyMethodAsync(req, ct))
.Call<IPingHandler>(async(handler, req, ct) => await handler.MyCancellableForAsync(req, ct))
.Return<PingResponse, IPingHandler>(
async(handler, req, ct) => await handler.MyMethodAsync(req, ct)
async(handler, req, ct) => await handler.MyCancellableForAsync(req, ct)
);
});
var pingHandler = new Mock<IPingHandler>();
Expand All @@ -100,32 +111,15 @@ public async Task BuildCancellableAsyncPipelineDirect()

var cts = new CancellationTokenSource();
var ping = new PingRequest("Cancellable Async Ping");

//
// Act
//
await mediator.PublishAsync(ping, cts.Token);

pingHandler.Verify(e => e.MyMethodAsync(ping, It.IsAny<CancellationToken>()), Times.Once);
pingHandler.Verify(e => e.MyCancellableForAsync(ping, It.IsAny<CancellationToken>()), Times.Once);
}

[Fact]
public void BuildSendAsyncPipeline_ThrowsException()
{
var services = new ServiceCollection();

Exception ex = Record.Exception(() =>
{
services.AddFluentMediator(m =>
{
m.On<PingRequest>().AsyncPipeline()
.Return<PingResponse, IPingHandler>(
(handler, req) => handler.MyMethodAsync(req)
);
m.On<PingRequest>().AsyncPipeline()
.Return<PingResponse, IPingHandler>(
(handler, req) => handler.MyMethodAsync(req)
);
});
});

Assert.NotNull(ex);
}

}
}
Loading

0 comments on commit 4d83168

Please sign in to comment.