diff --git a/MediatR.sln b/MediatR.sln index fe11e313..5d4f47d9 100644 --- a/MediatR.sln +++ b/MediatR.sln @@ -48,6 +48,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples.SimpleInje EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples.Stashbox", "samples\MediatR.Examples.Stashbox\MediatR.Examples.Stashbox.csproj", "{F9148E20-5856-484C-8410-B515C6C56214}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediatR.DependencyInjectionTests", "test\MediatR.DependencyInjectionTests\MediatR.DependencyInjectionTests.csproj", "{C761C0E2-0655-40FB-98E9-1504D03DD930}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -106,6 +108,10 @@ Global {F9148E20-5856-484C-8410-B515C6C56214}.Debug|Any CPU.Build.0 = Debug|Any CPU {F9148E20-5856-484C-8410-B515C6C56214}.Release|Any CPU.ActiveCfg = Release|Any CPU {F9148E20-5856-484C-8410-B515C6C56214}.Release|Any CPU.Build.0 = Release|Any CPU + {C761C0E2-0655-40FB-98E9-1504D03DD930}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C761C0E2-0655-40FB-98E9-1504D03DD930}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C761C0E2-0655-40FB-98E9-1504D03DD930}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C761C0E2-0655-40FB-98E9-1504D03DD930}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -124,6 +130,7 @@ Global {004D029A-43E7-47B0-BA74-D0A9F7FC7713} = {E372BF0B-90E8-4DC1-A332-F023095A3C2A} {7CEB57F2-B6DC-4A18-A040-D12555C3D32F} = {E372BF0B-90E8-4DC1-A332-F023095A3C2A} {F9148E20-5856-484C-8410-B515C6C56214} = {E372BF0B-90E8-4DC1-A332-F023095A3C2A} + {C761C0E2-0655-40FB-98E9-1504D03DD930} = {962C5ACA-AB2B-4E9B-9EBB-7E7EE28CDBB1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {D58286E3-878B-4ACB-8E76-F61E708D4339} diff --git a/test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs b/test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs new file mode 100644 index 00000000..349c8049 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs @@ -0,0 +1,66 @@ +using MediatR.DependencyInjectionTests.Contracts.Notifications; +using MediatR.DependencyInjectionTests.Contracts.StreamRequests; +using Microsoft.Extensions.DependencyInjection; + +namespace MediatR.DependencyInjectionTests.Abstractions; + +public abstract class BaseAssemblyResolutionTests(BaseServiceProviderFixture fixture) : IClassFixture +{ + private readonly IServiceProvider _provider = fixture.Provider; + + [Fact] + public void Should_Resolve_Mediator() => + _provider.GetService() + .ShouldNotBeNull(); + + [Fact] + public void Should_Resolve_Public_RequestHandler() => + _provider.GetService>() + .ShouldNotBeNull(); + + [Fact] + public void Should_Resolve_Internal_RequestHandler() => + _provider.GetService>() + .ShouldNotBeNull(); + + [Fact] + public void Should_Resolve_Private_RequestHandler() => + _provider.GetService>() + .ShouldNotBeNull(); + + [Fact] + public void Should_Resolve_Public_Void_RequestHandler() => + _provider.GetService>() + .ShouldNotBeNull(); + + [Fact] + public void Should_Resolve_Internal_Void_RequestHandler() => + _provider.GetService>() + .ShouldNotBeNull(); + + [Fact] + public void Should_Resolve_Private_Void_RequestHandler() => + _provider.GetService>() + .ShouldNotBeNull(); + + [Fact] + public void Should_Resolve_Public_Private_Internal_Notification_Handlers() => + _provider.GetServices>() + .Count() + .ShouldBe(3); + + [Fact] + public void Should_Resolve_Public_Stream_Request_Handlers() => + _provider.GetService>() + .ShouldNotBeNull(); + + [Fact] + public void Should_Resolve_Internal_Stream_Request_Handlers() => + _provider.GetService>() + .ShouldNotBeNull(); + + [Fact] + public void Should_Resolve_Private_Stream_Request_Handlers() => + _provider.GetService>() + .ShouldNotBeNull(); +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Abstractions/BaseServiceProviderFixture.cs b/test/MediatR.DependencyInjectionTests/Abstractions/BaseServiceProviderFixture.cs new file mode 100644 index 00000000..3af8d86f --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Abstractions/BaseServiceProviderFixture.cs @@ -0,0 +1,6 @@ +namespace MediatR.DependencyInjectionTests.Abstractions; + +public class BaseServiceProviderFixture +{ + public virtual IServiceProvider Provider => throw new NotImplementedException(); +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/AutoFacDependencyInjectionTests.cs b/test/MediatR.DependencyInjectionTests/AutoFacDependencyInjectionTests.cs new file mode 100644 index 00000000..bf681419 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/AutoFacDependencyInjectionTests.cs @@ -0,0 +1,9 @@ +using MediatR.DependencyInjectionTests.Abstractions; +using MediatR.DependencyInjectionTests.Providers; + +namespace MediatR.DependencyInjectionTests; + +public class AutoFacDependencyInjectionTests : BaseAssemblyResolutionTests +{ + public AutoFacDependencyInjectionTests() : base(new AutoFacServiceProviderFixture()) { } +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Contracts/Notifications/Ding.cs b/test/MediatR.DependencyInjectionTests/Contracts/Notifications/Ding.cs new file mode 100644 index 00000000..3202d87e --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Contracts/Notifications/Ding.cs @@ -0,0 +1,22 @@ +namespace MediatR.DependencyInjectionTests.Contracts.Notifications; + +public record Ding : INotification +{ + public class Door1 : INotificationHandler + { + public Task Handle(Ding notification, CancellationToken cancellationToken) => + Task.CompletedTask; + } + + internal class Door2 : INotificationHandler + { + public Task Handle(Ding notification, CancellationToken cancellationToken) => + Task.CompletedTask; + } + + private class Door3 : INotificationHandler + { + public Task Handle(Ding notification, CancellationToken cancellationToken) => + Task.CompletedTask; + } +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Contracts/Requests/InternalPing.cs b/test/MediatR.DependencyInjectionTests/Contracts/Requests/InternalPing.cs new file mode 100644 index 00000000..2daa6b18 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Contracts/Requests/InternalPing.cs @@ -0,0 +1,10 @@ +namespace MediatR.DependencyInjectionTests.Contracts.Requests; + +internal record InternalPing : IRequest +{ + internal class Handler : IRequestHandler + { + public Task Handle(InternalPing request, CancellationToken cancellationToken) => + Task.FromResult(new Pong()); + } +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Contracts/Requests/InternalVoidPing.cs b/test/MediatR.DependencyInjectionTests/Contracts/Requests/InternalVoidPing.cs new file mode 100644 index 00000000..ff89bea1 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Contracts/Requests/InternalVoidPing.cs @@ -0,0 +1,10 @@ +namespace MediatR.DependencyInjectionTests.Contracts.Requests; + +internal record InternalVoidPing : IRequest +{ + internal class Handler : IRequestHandler + { + public Task Handle(InternalVoidPing request, CancellationToken cancellationToken) => + Task.CompletedTask; + } +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Contracts/Requests/PrivatePing.cs b/test/MediatR.DependencyInjectionTests/Contracts/Requests/PrivatePing.cs new file mode 100644 index 00000000..55d6b987 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Contracts/Requests/PrivatePing.cs @@ -0,0 +1,10 @@ +namespace MediatR.DependencyInjectionTests.Contracts.Requests; + +public record PrivatePing : IRequest +{ + private class Handler : IRequestHandler + { + public Task Handle(PrivatePing request, CancellationToken cancellationToken) => + Task.FromResult(new Pong()); + } +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Contracts/Requests/PrivateVoidPing.cs b/test/MediatR.DependencyInjectionTests/Contracts/Requests/PrivateVoidPing.cs new file mode 100644 index 00000000..d27597e9 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Contracts/Requests/PrivateVoidPing.cs @@ -0,0 +1,10 @@ +namespace MediatR.DependencyInjectionTests.Contracts.Requests; + +public record PrivateVoidPing : IRequest +{ + private class Handler : IRequestHandler + { + public Task Handle(PrivateVoidPing request, CancellationToken cancellationToken) => + Task.CompletedTask; + } +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Contracts/Requests/PublicPing.cs b/test/MediatR.DependencyInjectionTests/Contracts/Requests/PublicPing.cs new file mode 100644 index 00000000..74851402 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Contracts/Requests/PublicPing.cs @@ -0,0 +1,10 @@ +namespace MediatR.DependencyInjectionTests.Contracts.Requests; + +public record PublicPing : IRequest +{ + public class Handler : IRequestHandler + { + public Task Handle(PublicPing request, CancellationToken cancellationToken) => + Task.FromResult(new Pong()); + } +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Contracts/Requests/PublicVoidPing.cs b/test/MediatR.DependencyInjectionTests/Contracts/Requests/PublicVoidPing.cs new file mode 100644 index 00000000..ed8a3d3c --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Contracts/Requests/PublicVoidPing.cs @@ -0,0 +1,10 @@ +namespace MediatR.DependencyInjectionTests.Contracts.Requests; + +public record PublicVoidPing : IRequest +{ + public class Handler : IRequestHandler + { + public Task Handle(PublicVoidPing request, CancellationToken cancellationToken) => + Task.CompletedTask; + } +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Contracts/Responses/Pong.cs b/test/MediatR.DependencyInjectionTests/Contracts/Responses/Pong.cs new file mode 100644 index 00000000..f9562798 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Contracts/Responses/Pong.cs @@ -0,0 +1,4 @@ +namespace MediatR.DependencyInjectionTests.Contracts.Responses; + +public record Pong; +public record Zong; diff --git a/test/MediatR.DependencyInjectionTests/Contracts/StreamRequests/InternalZing.cs b/test/MediatR.DependencyInjectionTests/Contracts/StreamRequests/InternalZing.cs new file mode 100644 index 00000000..d3c2da3e --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Contracts/StreamRequests/InternalZing.cs @@ -0,0 +1,10 @@ +namespace MediatR.DependencyInjectionTests.Contracts.StreamRequests; + +internal record InternalZing : IStreamRequest +{ + internal class Handler : IStreamRequestHandler + { + public IAsyncEnumerable Handle(InternalZing request, CancellationToken token) => + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Contracts/StreamRequests/PrivateZing.cs b/test/MediatR.DependencyInjectionTests/Contracts/StreamRequests/PrivateZing.cs new file mode 100644 index 00000000..c1dad85c --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Contracts/StreamRequests/PrivateZing.cs @@ -0,0 +1,10 @@ +namespace MediatR.DependencyInjectionTests.Contracts.StreamRequests; + +internal record PrivateZing : IStreamRequest +{ + private class Handler : IStreamRequestHandler + { + public IAsyncEnumerable Handle(PrivateZing request, CancellationToken token) => + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Contracts/StreamRequests/PublicZing.cs b/test/MediatR.DependencyInjectionTests/Contracts/StreamRequests/PublicZing.cs new file mode 100644 index 00000000..a592d9d8 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Contracts/StreamRequests/PublicZing.cs @@ -0,0 +1,10 @@ +namespace MediatR.DependencyInjectionTests.Contracts.StreamRequests; + +public record PublicZing : IStreamRequest +{ + public class Handler : IStreamRequestHandler + { + public IAsyncEnumerable Handle(PublicZing request, CancellationToken token) => + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/DryIocDependencyInjectionTests.cs b/test/MediatR.DependencyInjectionTests/DryIocDependencyInjectionTests.cs new file mode 100644 index 00000000..bb0e2b17 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/DryIocDependencyInjectionTests.cs @@ -0,0 +1,7 @@ +using MediatR.DependencyInjectionTests.Abstractions; +using MediatR.DependencyInjectionTests.Providers; + +namespace MediatR.DependencyInjectionTests; + +public class DryIocDependencyInjectionTests() + : BaseAssemblyResolutionTests(new DryIocServiceProviderFixture()); \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/MediatR.DependencyInjectionTests.csproj b/test/MediatR.DependencyInjectionTests/MediatR.DependencyInjectionTests.csproj new file mode 100644 index 00000000..e1f1d769 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/MediatR.DependencyInjectionTests.csproj @@ -0,0 +1,34 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/MicrosoftDependencyInjectionTests.cs b/test/MediatR.DependencyInjectionTests/MicrosoftDependencyInjectionTests.cs new file mode 100644 index 00000000..85c10ee0 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/MicrosoftDependencyInjectionTests.cs @@ -0,0 +1,9 @@ +using MediatR.DependencyInjectionTests.Abstractions; +using MediatR.DependencyInjectionTests.Providers; + +namespace MediatR.DependencyInjectionTests; + +public class MicrosoftDependencyInjectionTests : BaseAssemblyResolutionTests +{ + public MicrosoftDependencyInjectionTests() : base(new MicrosoftServiceProviderFixture()) { } +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Providers/AutoFacServiceProviderFixture.cs b/test/MediatR.DependencyInjectionTests/Providers/AutoFacServiceProviderFixture.cs new file mode 100644 index 00000000..9126e734 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Providers/AutoFacServiceProviderFixture.cs @@ -0,0 +1,25 @@ +using Autofac; +using Autofac.Extensions.DependencyInjection; +using MediatR.DependencyInjectionTests.Abstractions; +using Microsoft.Extensions.DependencyInjection; + +namespace MediatR.DependencyInjectionTests.Providers; + +public class AutoFacServiceProviderFixture : BaseServiceProviderFixture +{ + public override IServiceProvider Provider + { + get + { + var services = new ServiceCollection(); + services.AddFakeLogging(); + services.AddMediatR(x => x.RegisterServicesFromAssemblyContaining(typeof(Pong))); + + var builder = new ContainerBuilder(); + builder.Populate(services); + + var container = builder.Build(); + return new AutofacServiceProvider(container); + } + } +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Providers/DryIocServiceProviderFixture.cs b/test/MediatR.DependencyInjectionTests/Providers/DryIocServiceProviderFixture.cs new file mode 100644 index 00000000..7919b2ac --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Providers/DryIocServiceProviderFixture.cs @@ -0,0 +1,23 @@ +using DryIoc; +using DryIoc.Microsoft.DependencyInjection; +using MediatR.DependencyInjectionTests.Abstractions; +using Microsoft.Extensions.DependencyInjection; + +namespace MediatR.DependencyInjectionTests.Providers; + +public class DryIocServiceProviderFixture : BaseServiceProviderFixture +{ + public override IServiceProvider Provider + { + get + { + var services = new ServiceCollection(); + services.AddFakeLogging(); + services.AddMediatR(x => x.RegisterServicesFromAssemblyContaining(typeof(Pong))); + + var container = new Container(Rules.MicrosoftDependencyInjectionRules); + container.WithDependencyInjectionAdapter(services); + return container.BuildServiceProvider(); + } + } +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Providers/MicrosoftServiceProviderFixture.cs b/test/MediatR.DependencyInjectionTests/Providers/MicrosoftServiceProviderFixture.cs new file mode 100644 index 00000000..c0c785e4 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Providers/MicrosoftServiceProviderFixture.cs @@ -0,0 +1,12 @@ +using MediatR.DependencyInjectionTests.Abstractions; +using Microsoft.Extensions.DependencyInjection; + +namespace MediatR.DependencyInjectionTests.Providers; + +public class MicrosoftServiceProviderFixture : BaseServiceProviderFixture +{ + public override IServiceProvider Provider => new ServiceCollection() + .AddFakeLogging() + .AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(PublicPing).Assembly)) + .BuildServiceProvider(); +} \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Usings.cs b/test/MediatR.DependencyInjectionTests/Usings.cs new file mode 100644 index 00000000..8054d6fd --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Usings.cs @@ -0,0 +1,3 @@ +global using MediatR.DependencyInjectionTests.Contracts.Requests; +global using MediatR.DependencyInjectionTests.Contracts.Responses; +global using Shouldly; \ No newline at end of file