From 76d3b5aad2b9b821c6c02bd813e8826736298c30 Mon Sep 17 00:00:00 2001 From: Jithin Johnson Date: Sun, 22 Sep 2024 20:11:19 +0530 Subject: [PATCH 1/7] Added project --- MediatR.sln | 7 ++++++ .../MediatR.DependencyInjectionTests.csproj | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 test/MediatR.DependencyInjectionTests/MediatR.DependencyInjectionTests.csproj 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/MediatR.DependencyInjectionTests.csproj b/test/MediatR.DependencyInjectionTests/MediatR.DependencyInjectionTests.csproj new file mode 100644 index 00000000..9c5b30a2 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/MediatR.DependencyInjectionTests.csproj @@ -0,0 +1,23 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + From 3721e9de1d375d297b72c8a533ae09dea0a88bc3 Mon Sep 17 00:00:00 2001 From: Jithin Johnson Date: Sun, 22 Sep 2024 22:21:32 +0530 Subject: [PATCH 2/7] - Added abstract DI tests - Added abstract fixtures for providers - Added GlobalUsings - Added public, private, internal contracts --- .../BaseAssemblyResolutionTests.cs | 54 +++++++++++++++++++ .../BaseServiceProviderFixture.cs | 6 +++ .../Contracts/Requests/InternalPing.cs | 10 ++++ .../Contracts/Requests/InternalVoidPing.cs | 10 ++++ .../Contracts/Requests/PrivatePing.cs | 10 ++++ .../Contracts/Requests/PrivateVoidPing.cs | 10 ++++ .../Contracts/Requests/PublicPing.cs | 10 ++++ .../Contracts/Requests/PublicVoidPing.cs | 10 ++++ .../Contracts/Responses/Pong.cs | 3 ++ .../MediatR.DependencyInjectionTests.csproj | 43 +++++++++------ .../Usings.cs | 3 ++ 11 files changed, 153 insertions(+), 16 deletions(-) create mode 100644 test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs create mode 100644 test/MediatR.DependencyInjectionTests/Abstractions/BaseServiceProviderFixture.cs create mode 100644 test/MediatR.DependencyInjectionTests/Contracts/Requests/InternalPing.cs create mode 100644 test/MediatR.DependencyInjectionTests/Contracts/Requests/InternalVoidPing.cs create mode 100644 test/MediatR.DependencyInjectionTests/Contracts/Requests/PrivatePing.cs create mode 100644 test/MediatR.DependencyInjectionTests/Contracts/Requests/PrivateVoidPing.cs create mode 100644 test/MediatR.DependencyInjectionTests/Contracts/Requests/PublicPing.cs create mode 100644 test/MediatR.DependencyInjectionTests/Contracts/Requests/PublicVoidPing.cs create mode 100644 test/MediatR.DependencyInjectionTests/Contracts/Responses/Pong.cs create mode 100644 test/MediatR.DependencyInjectionTests/Usings.cs diff --git a/test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs b/test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs new file mode 100644 index 00000000..6fff31ce --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs @@ -0,0 +1,54 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace MediatR.DependencyInjectionTests.Abstractions; + +public abstract class BaseAssemblyResolutionTests : IClassFixture +{ + private readonly IServiceProvider _provider; + + protected BaseAssemblyResolutionTests(BaseServiceProviderFixture fixture) => + _provider = fixture.Provider; + + [Fact] + public void Should_Resolve_Mediator() => + _provider.GetService() + .ShouldNotBeNull(); + + #region REQUESTS + + [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(); + + #endregion + + #region VOID_REQUESTS + + [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(); + + #endregion +} \ 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/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..4ece9db3 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Contracts/Responses/Pong.cs @@ -0,0 +1,3 @@ +namespace MediatR.DependencyInjectionTests.Contracts.Responses; + +public record Pong; \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/MediatR.DependencyInjectionTests.csproj b/test/MediatR.DependencyInjectionTests/MediatR.DependencyInjectionTests.csproj index 9c5b30a2..391d2256 100644 --- a/test/MediatR.DependencyInjectionTests/MediatR.DependencyInjectionTests.csproj +++ b/test/MediatR.DependencyInjectionTests/MediatR.DependencyInjectionTests.csproj @@ -1,23 +1,34 @@ - - net8.0 - enable - enable + + net8.0 + enable + enable - false - true - + false + true + - - - - - - + + + - - - + + + + + + + + + + + + + + + + + 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 From 6541f08e54677891d534a000f77c5aa2233bac02 Mon Sep 17 00:00:00 2001 From: Jithin Johnson Date: Sun, 22 Sep 2024 22:22:59 +0530 Subject: [PATCH 3/7] Added tests for MicrosoftDependencyInjection --- .../MicrosoftDependencyInjectionTests.cs | 9 +++++++++ .../Providers/MicrosoftServiceProviderFixture.cs | 11 +++++++++++ 2 files changed, 20 insertions(+) create mode 100644 test/MediatR.DependencyInjectionTests/MicrosoftDependencyInjectionTests.cs create mode 100644 test/MediatR.DependencyInjectionTests/Providers/MicrosoftServiceProviderFixture.cs 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/MicrosoftServiceProviderFixture.cs b/test/MediatR.DependencyInjectionTests/Providers/MicrosoftServiceProviderFixture.cs new file mode 100644 index 00000000..374c49dc --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Providers/MicrosoftServiceProviderFixture.cs @@ -0,0 +1,11 @@ +using MediatR.DependencyInjectionTests.Abstractions; +using Microsoft.Extensions.DependencyInjection; + +namespace MediatR.DependencyInjectionTests.Providers; + +public class MicrosoftServiceProviderFixture : BaseServiceProviderFixture +{ + public override IServiceProvider Provider => new ServiceCollection() + .AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(PublicPing).Assembly)) + .BuildServiceProvider(); +} \ No newline at end of file From 4ac5b650dfeb2fe6b596fb125d92fcd8bf99af7d Mon Sep 17 00:00:00 2001 From: Jithin Johnson Date: Sun, 22 Sep 2024 22:23:09 +0530 Subject: [PATCH 4/7] Added tests for AutoFacDependencyInjection --- .../AutoFacDependencyInjectionTests.cs | 9 +++++++ .../AutoFacServiceProviderFixture.cs | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 test/MediatR.DependencyInjectionTests/AutoFacDependencyInjectionTests.cs create mode 100644 test/MediatR.DependencyInjectionTests/Providers/AutoFacServiceProviderFixture.cs 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/Providers/AutoFacServiceProviderFixture.cs b/test/MediatR.DependencyInjectionTests/Providers/AutoFacServiceProviderFixture.cs new file mode 100644 index 00000000..337082a4 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Providers/AutoFacServiceProviderFixture.cs @@ -0,0 +1,24 @@ +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.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 From 9ea77d79161d37038f057b1031a4bb9d677349b7 Mon Sep 17 00:00:00 2001 From: Jithin Johnson Date: Sun, 22 Sep 2024 22:23:20 +0530 Subject: [PATCH 5/7] Added tests for DryIocDependencyInjection --- .../DryIocDependencyInjectionTests.cs | 9 +++++++++ .../Providers/DryIocServiceProviderFixture.cs | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/MediatR.DependencyInjectionTests/DryIocDependencyInjectionTests.cs create mode 100644 test/MediatR.DependencyInjectionTests/Providers/DryIocServiceProviderFixture.cs diff --git a/test/MediatR.DependencyInjectionTests/DryIocDependencyInjectionTests.cs b/test/MediatR.DependencyInjectionTests/DryIocDependencyInjectionTests.cs new file mode 100644 index 00000000..0b3a4775 --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/DryIocDependencyInjectionTests.cs @@ -0,0 +1,9 @@ +using MediatR.DependencyInjectionTests.Abstractions; +using MediatR.DependencyInjectionTests.Providers; + +namespace MediatR.DependencyInjectionTests; + +public class DryIocDependencyInjectionTests : BaseAssemblyResolutionTests +{ + public DryIocDependencyInjectionTests() : base(new DryIocServiceProviderFixture()) { } +} \ 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..4ca72d5a --- /dev/null +++ b/test/MediatR.DependencyInjectionTests/Providers/DryIocServiceProviderFixture.cs @@ -0,0 +1,19 @@ +using DryIoc; +using DryIoc.Microsoft.DependencyInjection; +using MediatR.DependencyInjectionTests.Abstractions; + +namespace MediatR.DependencyInjectionTests.Providers; + +public class DryIocServiceProviderFixture : BaseServiceProviderFixture +{ + public override IServiceProvider Provider + { + get + { + var container = new Container(); + container.RegisterMany(new[] { typeof(IMediator).GetAssembly(), typeof(Pong).GetAssembly() }, Registrator.Interfaces); + container.Register(made: Made.Of(() => new Mediator(Arg.Of()))); + return container.BuildServiceProvider(); + } + } +} \ No newline at end of file From 17f2073d634f9696aa0b19e6ff39b924c9732934 Mon Sep 17 00:00:00 2001 From: Jithin Johnson Date: Sun, 22 Sep 2024 23:01:59 +0530 Subject: [PATCH 6/7] Added StreamRequestHandler, NotificationHandler tests --- .../BaseAssemblyResolutionTests.cs | 31 ++++++++++++++----- .../Contracts/Notifications/Ding.cs | 22 +++++++++++++ .../Contracts/Responses/Pong.cs | 3 +- .../Contracts/StreamRequests/InternalZing.cs | 10 ++++++ .../Contracts/StreamRequests/PrivateZing.cs | 10 ++++++ .../Contracts/StreamRequests/PublicZing.cs | 10 ++++++ 6 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 test/MediatR.DependencyInjectionTests/Contracts/Notifications/Ding.cs create mode 100644 test/MediatR.DependencyInjectionTests/Contracts/StreamRequests/InternalZing.cs create mode 100644 test/MediatR.DependencyInjectionTests/Contracts/StreamRequests/PrivateZing.cs create mode 100644 test/MediatR.DependencyInjectionTests/Contracts/StreamRequests/PublicZing.cs diff --git a/test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs b/test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs index 6fff31ce..0a391ee7 100644 --- a/test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs +++ b/test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs @@ -1,4 +1,6 @@ -using Microsoft.Extensions.DependencyInjection; +using MediatR.DependencyInjectionTests.Contracts.Notifications; +using MediatR.DependencyInjectionTests.Contracts.StreamRequests; +using Microsoft.Extensions.DependencyInjection; namespace MediatR.DependencyInjectionTests.Abstractions; @@ -14,8 +16,6 @@ public void Should_Resolve_Mediator() => _provider.GetService() .ShouldNotBeNull(); - #region REQUESTS - [Fact] public void Should_Resolve_Public_RequestHandler() => _provider.GetService>() @@ -31,10 +31,6 @@ public void Should_Resolve_Private_RequestHandler() => _provider.GetService>() .ShouldNotBeNull(); - #endregion - - #region VOID_REQUESTS - [Fact] public void Should_Resolve_Public_Void_RequestHandler() => _provider.GetService>() @@ -50,5 +46,24 @@ public void Should_Resolve_Private_Void_RequestHandler() => _provider.GetService>() .ShouldNotBeNull(); - #endregion + [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/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/Responses/Pong.cs b/test/MediatR.DependencyInjectionTests/Contracts/Responses/Pong.cs index 4ece9db3..f9562798 100644 --- a/test/MediatR.DependencyInjectionTests/Contracts/Responses/Pong.cs +++ b/test/MediatR.DependencyInjectionTests/Contracts/Responses/Pong.cs @@ -1,3 +1,4 @@ namespace MediatR.DependencyInjectionTests.Contracts.Responses; -public record Pong; \ No newline at end of file +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 From 85348266b0efcb7a28c86613b60b13ca69dbeff2 Mon Sep 17 00:00:00 2001 From: Jithin Johnson Date: Sun, 24 Aug 2025 00:47:45 -0700 Subject: [PATCH 7/7] - Added FakeLogging to resolve ILogger errors - Added IServiceProvider compatible container for DryIoc --- .../Abstractions/BaseAssemblyResolutionTests.cs | 7 ++----- .../DryIocDependencyInjectionTests.cs | 6 ++---- .../MediatR.DependencyInjectionTests.csproj | 4 ++-- .../Providers/AutoFacServiceProviderFixture.cs | 1 + .../Providers/DryIocServiceProviderFixture.cs | 10 +++++++--- .../Providers/MicrosoftServiceProviderFixture.cs | 1 + 6 files changed, 15 insertions(+), 14 deletions(-) diff --git a/test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs b/test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs index 0a391ee7..349c8049 100644 --- a/test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs +++ b/test/MediatR.DependencyInjectionTests/Abstractions/BaseAssemblyResolutionTests.cs @@ -4,12 +4,9 @@ namespace MediatR.DependencyInjectionTests.Abstractions; -public abstract class BaseAssemblyResolutionTests : IClassFixture +public abstract class BaseAssemblyResolutionTests(BaseServiceProviderFixture fixture) : IClassFixture { - private readonly IServiceProvider _provider; - - protected BaseAssemblyResolutionTests(BaseServiceProviderFixture fixture) => - _provider = fixture.Provider; + private readonly IServiceProvider _provider = fixture.Provider; [Fact] public void Should_Resolve_Mediator() => diff --git a/test/MediatR.DependencyInjectionTests/DryIocDependencyInjectionTests.cs b/test/MediatR.DependencyInjectionTests/DryIocDependencyInjectionTests.cs index 0b3a4775..bb0e2b17 100644 --- a/test/MediatR.DependencyInjectionTests/DryIocDependencyInjectionTests.cs +++ b/test/MediatR.DependencyInjectionTests/DryIocDependencyInjectionTests.cs @@ -3,7 +3,5 @@ namespace MediatR.DependencyInjectionTests; -public class DryIocDependencyInjectionTests : BaseAssemblyResolutionTests -{ - public DryIocDependencyInjectionTests() : base(new DryIocServiceProviderFixture()) { } -} \ No newline at end of file +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 index 391d2256..e1f1d769 100644 --- a/test/MediatR.DependencyInjectionTests/MediatR.DependencyInjectionTests.csproj +++ b/test/MediatR.DependencyInjectionTests/MediatR.DependencyInjectionTests.csproj @@ -22,8 +22,8 @@ + - @@ -31,4 +31,4 @@ - + \ No newline at end of file diff --git a/test/MediatR.DependencyInjectionTests/Providers/AutoFacServiceProviderFixture.cs b/test/MediatR.DependencyInjectionTests/Providers/AutoFacServiceProviderFixture.cs index 337082a4..9126e734 100644 --- a/test/MediatR.DependencyInjectionTests/Providers/AutoFacServiceProviderFixture.cs +++ b/test/MediatR.DependencyInjectionTests/Providers/AutoFacServiceProviderFixture.cs @@ -12,6 +12,7 @@ public override IServiceProvider Provider get { var services = new ServiceCollection(); + services.AddFakeLogging(); services.AddMediatR(x => x.RegisterServicesFromAssemblyContaining(typeof(Pong))); var builder = new ContainerBuilder(); diff --git a/test/MediatR.DependencyInjectionTests/Providers/DryIocServiceProviderFixture.cs b/test/MediatR.DependencyInjectionTests/Providers/DryIocServiceProviderFixture.cs index 4ca72d5a..7919b2ac 100644 --- a/test/MediatR.DependencyInjectionTests/Providers/DryIocServiceProviderFixture.cs +++ b/test/MediatR.DependencyInjectionTests/Providers/DryIocServiceProviderFixture.cs @@ -1,6 +1,7 @@ using DryIoc; using DryIoc.Microsoft.DependencyInjection; using MediatR.DependencyInjectionTests.Abstractions; +using Microsoft.Extensions.DependencyInjection; namespace MediatR.DependencyInjectionTests.Providers; @@ -10,9 +11,12 @@ public override IServiceProvider Provider { get { - var container = new Container(); - container.RegisterMany(new[] { typeof(IMediator).GetAssembly(), typeof(Pong).GetAssembly() }, Registrator.Interfaces); - container.Register(made: Made.Of(() => new Mediator(Arg.Of()))); + 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(); } } diff --git a/test/MediatR.DependencyInjectionTests/Providers/MicrosoftServiceProviderFixture.cs b/test/MediatR.DependencyInjectionTests/Providers/MicrosoftServiceProviderFixture.cs index 374c49dc..c0c785e4 100644 --- a/test/MediatR.DependencyInjectionTests/Providers/MicrosoftServiceProviderFixture.cs +++ b/test/MediatR.DependencyInjectionTests/Providers/MicrosoftServiceProviderFixture.cs @@ -6,6 +6,7 @@ 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