Skip to content

Commit

Permalink
feature(#52): Replace MediatR package with simpler implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
stefangrasboeck authored and davidroth committed Mar 29, 2024
1 parent 74d0fcd commit f1802a6
Show file tree
Hide file tree
Showing 53 changed files with 692 additions and 109 deletions.
9 changes: 8 additions & 1 deletion Extensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Email", "src\Email\src\Emai
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Email.Tests", "src\Email\test\Email.Tests.csproj", "{C0FDFA5A-5421-40A1-8653-823FB9C94E5C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR", "src\MediatR\src\MediatR.csproj", "{EE5FCA64-B743-423C-B329-A12C0F689636}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mediator", "src\Mediator\src\Mediator.csproj", "{EE5FCA64-B743-423C-B329-A12C0F689636}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hosting", "src\Hosting\src\Hosting.csproj", "{29CAAEDE-46F0-4C0E-A594-9B6E78E760BF}"
EndProject
Expand All @@ -57,6 +57,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests.ServiceProvider",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests.EntityFrameworkCore.SqlServer", "src\UnitTests.EntityFrameworkCore.SqlServer\src\UnitTests.EntityFrameworkCore.SqlServer.csproj", "{E8DFCBE3-2CBD-4F2B-81DD-CAE85C117239}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mediator.Tests", "src\Mediator\test\Mediator.Tests.csproj", "{286BFD1A-837D-4B26-8071-56B04148A864}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -147,6 +149,10 @@ Global
{E8DFCBE3-2CBD-4F2B-81DD-CAE85C117239}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E8DFCBE3-2CBD-4F2B-81DD-CAE85C117239}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E8DFCBE3-2CBD-4F2B-81DD-CAE85C117239}.Release|Any CPU.Build.0 = Release|Any CPU
{286BFD1A-837D-4B26-8071-56B04148A864}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{286BFD1A-837D-4B26-8071-56B04148A864}.Debug|Any CPU.Build.0 = Debug|Any CPU
{286BFD1A-837D-4B26-8071-56B04148A864}.Release|Any CPU.ActiveCfg = Release|Any CPU
{286BFD1A-837D-4B26-8071-56B04148A864}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -160,6 +166,7 @@ Global
{68F735B4-D3E5-477F-8071-F65D1C2137E2} = {95D2383B-80C5-46AA-9190-826A0377A156}
{94896282-9BBC-4373-B543-7FAC9F268695} = {95D2383B-80C5-46AA-9190-826A0377A156}
{86B0E8DD-25F8-46DF-95D5-781ECC1BCB13} = {95D2383B-80C5-46AA-9190-826A0377A156}
{286BFD1A-837D-4B26-8071-56B04148A864} = {95D2383B-80C5-46AA-9190-826A0377A156}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DCAD7035-5EC8-4B6E-82B5-769C5A4892EA}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Provides Hangfire extensions, especially suited for CQRS developement. (Out of b
Provides services and extensions for hosting. See the [documentation](docs/Hosting/README.md) for more details.

[![NuGet](https://img.shields.io/nuget/v/Fusonic.Extensions.MediatR.svg?label=Fusonic.Extensions.MediatR&style=plastic)](https://www.nuget.org/packages/Fusonic.Extensions.MediatR/)
Provides abstractions for MediatR. See the [documentation](docs/MediatR/README.md) for more details.
Provides simple mediator implementation. See the [documentation](docs/Mediator/README.md) for more details.

[![NuGet](https://img.shields.io/nuget/v/Fusonic.Extensions.UnitTests.svg?label=Fusonic.Extensions.UnitTests&style=plastic)](https://www.nuget.org/packages/Fusonic.Extensions.UnitTests/)
Xunit-based testing base classes with support for dependency injection. Libraries supporting specific DI containers (SimpleInjector, ServiceProvider) are in separate packages. See the [unit test documentation](docs/UnitTests/README.md) for more details.
Expand Down
8 changes: 4 additions & 4 deletions docs/MediatR/README.md → docs/Mediator/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# MediatR
# Mediator

This project contains some abstractions for MediatR.
This project contains a simple implementation of the common mediator pattern. The implementation focused on simplifying version 11.1.0 of the MediatR package. The pipelining was removed and a pure mediator implementation remains.

`ICommand`, `IQuery`: Use those instead of directly using the `IRequest` interfaces to clearly distinguish between commands and queries.
`OutOfBandAttribute`: You can put this on your `Handler`-class. When used together with the [Hangfire-Decorator](../Hangfire/README.md), the execution of the handler will be queued as a background job automatically.

## MediatR transaction handling
## Mediator transaction handling

There are decorators to run all MediatR-requests and notifications within a transaction.
There are decorators to run all requests and notifications within a transaction.
To enable this feature use the following SimpleInjector-Configuration:
```cs
Container.RegisterDecorator(typeof(IRequestHandler<,>), typeof(TransactionalRequestHandlerDecorator<,>));
Expand Down
5 changes: 1 addition & 4 deletions src/AspNetCore/src/AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MediatR" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Common\src\Common.csproj" />
<ProjectReference Include="..\..\Mediator\src\Mediator.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Fusonic GmbH. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license information.

using MediatR;
using Fusonic.Extensions.Mediator;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using FluentAssertions;
using Fusonic.Extensions.AspNetCore.Validation;
using MediatR;
using Fusonic.Extensions.Mediator;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using NSubstitute;
Expand Down
1 change: 0 additions & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<PackageVersion Include="Hangfire.Core" Version="1.8.6" />
<PackageVersion Include="Hangfire.PostgreSql" Version="1.20.4" />
<PackageVersion Include="MailKit" Version="4.3.0" />
<PackageVersion Include="MediatR" Version="[11.1.0,12.0.0)" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/Email/src/ContainerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Reflection;
using Fusonic.Extensions.AspNetCore.Razor;
using MediatR;
using Fusonic.Extensions.Mediator;
using Microsoft.AspNetCore.Mvc.Localization;
using SimpleInjector;

Expand Down
2 changes: 1 addition & 1 deletion src/Email/src/Email.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" Version="3.1.1" />
<ProjectReference Include="..\..\AspNetCore\src\AspNetCore.csproj" />
<ProjectReference Include="..\..\MediatR\src\MediatR.csproj" />
<ProjectReference Include="..\..\Mediator\src\Mediator.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions src/Email/src/SendEmail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// Licensed under the MIT License. See LICENSE file in the project root for license information.

using System.Globalization;
using Fusonic.Extensions.MediatR;
using MediatR;
using Fusonic.Extensions.Mediator;
using MimeKit;

namespace Fusonic.Extensions.Email;
Expand Down
2 changes: 1 addition & 1 deletion src/Email/test/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Licensed under the MIT License. See LICENSE file in the project root for license information.

using System.Diagnostics;
using Fusonic.Extensions.Mediator;
using Fusonic.Extensions.UnitTests;
using Fusonic.Extensions.UnitTests.SimpleInjector;
using MediatR;

namespace Fusonic.Extensions.Email.Tests;

Expand Down
15 changes: 2 additions & 13 deletions src/Email/test/TestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

using System.Diagnostics;
using System.Reflection;
using Fusonic.Extensions.Mediator;
using Fusonic.Extensions.UnitTests.SimpleInjector;
using MediatR;
using MediatR.Pipeline;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Localization;
Expand Down Expand Up @@ -33,20 +32,10 @@ protected sealed override void RegisterCoreDependencies(Container container)
container.Register<RazorEmailRenderingService>();

var mediatorAssemblies = new[] { typeof(IMediator).Assembly, typeof(SendEmail).Assembly };
container.Register(() => new ServiceFactory(container.GetInstance), Lifestyle.Singleton);
container.RegisterSingleton<IMediator, Mediator>();
container.RegisterSingleton<IMediator, SimpleInjectorMediator>();
container.Register(typeof(IRequestHandler<,>), mediatorAssemblies);

container.Collection.Register(typeof(INotificationHandler<>), mediatorAssemblies);
container.Collection.Register(typeof(IPipelineBehavior<,>),
new[]
{
typeof(RequestPreProcessorBehavior<,>),
typeof(RequestPostProcessorBehavior<,>)
});

container.Collection.Register(typeof(IRequestPreProcessor<>), mediatorAssemblies);
container.Collection.Register(typeof(IRequestPostProcessor<,>), mediatorAssemblies);

var services = new ServiceCollection();
services.AddRazorPages()
Expand Down
3 changes: 1 addition & 2 deletions src/Hangfire/src/ContainerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// Licensed under the MIT License. See LICENSE file in the project root for license information.

using System.Reflection;
using Fusonic.Extensions.MediatR;
using Fusonic.Extensions.Common.Security;
using MediatR;
using SimpleInjector;
using Fusonic.Extensions.Mediator;

namespace Fusonic.Extensions.Hangfire;

Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire/src/Hangfire.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Common\src\Common.csproj" />
<ProjectReference Include="..\..\MediatR\src\MediatR.csproj" />
<ProjectReference Include="..\..\Mediator\src\Mediator.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Hangfire/src/JobProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using System.Globalization;
using Fusonic.Extensions.Common.Security;
using Fusonic.Extensions.Mediator;
using Hangfire.Server;
using MediatR;
using SimpleInjector;

namespace Fusonic.Extensions.Hangfire;
Expand Down
4 changes: 2 additions & 2 deletions src/Hangfire/src/NotificationDispatcher`1.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Fusonic GmbH. All rights reserved.
// Copyright (c) Fusonic GmbH. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license information.

using MediatR;
using Fusonic.Extensions.Mediator;
using SimpleInjector;

namespace Fusonic.Extensions.Hangfire;
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire/src/OutOfBandNotificationHandlerDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using System.Globalization;
using Fusonic.Extensions.Common.Security;
using Fusonic.Extensions.Mediator;
using Hangfire;
using MediatR;

namespace Fusonic.Extensions.Hangfire;

Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire/src/OutOfBandRequestHandlerDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using System.Globalization;
using Fusonic.Extensions.Common.Security;
using Fusonic.Extensions.Mediator;
using Hangfire;
using MediatR;

namespace Fusonic.Extensions.Hangfire;

Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire/test/ContainerExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the MIT License. See LICENSE file in the project root for license information.

using Fusonic.Extensions.Common.Security;
using Fusonic.Extensions.Mediator;
using Hangfire;
using MediatR;
using NSubstitute;
using SimpleInjector;
using SimpleInjector.Lifestyles;
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire/test/DecoratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Fusonic GmbH. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license information.

using Fusonic.Extensions.Mediator;
using Hangfire;
using MediatR;
using NSubstitute;
using NSubstitute.ClearExtensions;
using Xunit;
Expand Down
1 change: 0 additions & 1 deletion src/Hangfire/test/Hangfire.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MediatR" />
<PackageReference Include="SimpleInjector" />
</ItemGroup>

Expand Down
15 changes: 1 addition & 14 deletions src/Hangfire/test/HelperTypes.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright (c) Fusonic GmbH. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license information.

using Fusonic.Extensions.MediatR;
using MediatR;
using Fusonic.Extensions.Mediator;

namespace Fusonic.Extensions.Hangfire.Tests;

Expand Down Expand Up @@ -63,22 +62,10 @@ public Task Handle(OutOfBandNotification notification, CancellationToken cancell
}
}

public class SyncNotificationHandler : NotificationHandler<Notification>
{
protected override void Handle(Notification notification) { }
}

public class Request : IRequest { }

public class RequestHandler : AsyncRequestHandler<Request>
{
protected override Task Handle(Request request, CancellationToken cancellationToken)
=> Task.CompletedTask;
}

public class SyncRequest : IRequest { }

public class SyncRequestHandler : RequestHandler<SyncRequest>
{
protected override void Handle(SyncRequest request) { }
}
15 changes: 1 addition & 14 deletions src/Hangfire/test/JobProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
using System.Security.Claims;
using FluentAssertions;
using Fusonic.Extensions.Common.Security;
using Fusonic.Extensions.MediatR;
using Fusonic.Extensions.Mediator;
using Hangfire;
using Hangfire.Server;
using Hangfire.Storage;
using MediatR;
using NSubstitute;
using NSubstitute.ClearExtensions;
using Xunit;
Expand All @@ -32,24 +31,12 @@ public async Task CanProcessNotification()
await GetJobProcessor().ProcessAsync(new MediatorHandlerContext(new Notification(), typeof(NotificationHandler).AssemblyQualifiedName!), CreatePerformContext());
}

[Fact]
public async Task CanProcessNotificationSync()
{
await GetJobProcessor().ProcessAsync(new MediatorHandlerContext(new Notification(), typeof(SyncNotificationHandler).AssemblyQualifiedName!), CreatePerformContext());
}

[Fact]
public async Task CanProcessRequest()
{
await GetJobProcessor().ProcessAsync(new MediatorHandlerContext(new Request(), typeof(RequestHandler).AssemblyQualifiedName!), CreatePerformContext());
}

[Fact]
public async Task CanProcessRequestSync()
{
await GetJobProcessor().ProcessAsync(new MediatorHandlerContext(new SyncRequest(), typeof(SyncRequestHandler).AssemblyQualifiedName!), CreatePerformContext());
}

[Fact]
public async Task RestoreCulture()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Hangfire/test/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the MIT License. See LICENSE file in the project root for license information.

using System.Diagnostics;
using Fusonic.Extensions.Mediator;
using Fusonic.Extensions.UnitTests.EntityFrameworkCore;
using MediatR;

namespace Fusonic.Extensions.Hangfire.Tests;

Expand All @@ -23,4 +23,4 @@ protected TestBase(TFixture fixture) : base(fixture)
[DebuggerStepThrough]
protected Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
=> ScopedAsync(() => GetInstance<IMediator>().Send(request));
}
}
21 changes: 5 additions & 16 deletions src/Hangfire/test/TestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@

using Fusonic.Extensions.Common.Security;
using Fusonic.Extensions.Common.Transactions;
using Fusonic.Extensions.UnitTests.EntityFrameworkCore.Npgsql;
using Fusonic.Extensions.Mediator;
using Fusonic.Extensions.UnitTests.EntityFrameworkCore;
using Fusonic.Extensions.UnitTests.EntityFrameworkCore.Npgsql;
using Fusonic.Extensions.UnitTests.SimpleInjector;
using Hangfire;
using MediatR;
using Hangfire.PostgreSql;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Npgsql;
using NSubstitute;
using SimpleInjector;
using Hangfire.PostgreSql;
using MediatR.Pipeline;
using Npgsql;

namespace Fusonic.Extensions.Hangfire.Tests;

Expand Down Expand Up @@ -46,19 +45,9 @@ protected sealed override void RegisterCoreDependencies(Container container)
private static void RegisterMediator(Container container)
{
var mediatorAssemblies = new[] { typeof(IMediator).Assembly, typeof(TestFixture).Assembly };
container.Register(() => new ServiceFactory(container.GetInstance), Lifestyle.Singleton);
container.RegisterSingleton<IMediator, Mediator>();
container.RegisterSingleton<IMediator, SimpleInjectorMediator>();
container.Register(typeof(IRequestHandler<,>), mediatorAssemblies);
container.Collection.Register(typeof(INotificationHandler<>), mediatorAssemblies);

container.Collection.Register(typeof(IPipelineBehavior<,>), new[]
{
typeof(RequestPreProcessorBehavior<,>),
typeof(RequestPostProcessorBehavior<,>)
});

container.Collection.Register(typeof(IRequestPreProcessor<>), mediatorAssemblies);
container.Collection.Register(typeof(IRequestPostProcessor<,>), mediatorAssemblies);
}

private void RegisterDatabase(IServiceCollection services)
Expand Down
Loading

0 comments on commit f1802a6

Please sign in to comment.