diff --git a/src/Application/NBB.Application.MediatR.Effects/DependencyInjectionExtensions.cs b/src/Application/NBB.Application.MediatR.Effects/DependencyInjectionExtensions.cs index dacba61e..6c31b91b 100644 --- a/src/Application/NBB.Application.MediatR.Effects/DependencyInjectionExtensions.cs +++ b/src/Application/NBB.Application.MediatR.Effects/DependencyInjectionExtensions.cs @@ -4,9 +4,10 @@ namespace NBB.Application.MediatR.Effects { public static class DependencyInjectionExtensions { - public static void AddMediatorEffects(this IServiceCollection services) + public static IServiceCollection AddMediatorEffects(this IServiceCollection services) { services.AddSingleton(typeof(MediatorSendQuery.Handler<>)); + return services; } } } diff --git a/src/Core/NBB.Core.Effects.FSharp/Interpreter.fs b/src/Core/NBB.Core.Effects.FSharp/Interpreter.fs index 7229c4f6..b579c894 100644 --- a/src/Core/NBB.Core.Effects.FSharp/Interpreter.fs +++ b/src/Core/NBB.Core.Effects.FSharp/Interpreter.fs @@ -1,5 +1,8 @@ namespace NBB.Core.Effects.FSharp +open Microsoft.Extensions.DependencyInjection + module Interpreter = - let createInterpreter = NBB.Core.Effects.Interpreter.CreateDefault + let createInterpreter () = NBB.Core.Effects.Interpreter.CreateDefault(null) + let createInterpreterWith (configureServices:IServiceCollection -> unit) = NBB.Core.Effects.Interpreter.CreateDefault(System.Action(configureServices)) diff --git a/src/Core/NBB.Core.Effects/DependencyInjectionExtensions.cs b/src/Core/NBB.Core.Effects/DependencyInjectionExtensions.cs index 2bedc0f0..ed0ab080 100644 --- a/src/Core/NBB.Core.Effects/DependencyInjectionExtensions.cs +++ b/src/Core/NBB.Core.Effects/DependencyInjectionExtensions.cs @@ -7,13 +7,14 @@ namespace NBB.Core.Effects { public static class DependencyInjectionExtensions { - public static void AddEffects(this IServiceCollection services) + public static IServiceCollection AddEffects(this IServiceCollection services) { services.AddSingleton(typeof(Thunk.Handler<>)); services.AddSingleton(typeof(Parallel.Handler<,>)); services.AddSingleton(typeof(Sequenced.Handler<>)); services.AddScoped(); services.AddScoped(); + return services; } public static IServiceCollection AddSideEffectHandler(this IServiceCollection services, ISideEffectHandler, TOutput> handler) diff --git a/src/Core/NBB.Core.Effects/Interpreter.cs b/src/Core/NBB.Core.Effects/Interpreter.cs index d074ed14..cda33928 100644 --- a/src/Core/NBB.Core.Effects/Interpreter.cs +++ b/src/Core/NBB.Core.Effects/Interpreter.cs @@ -48,8 +48,8 @@ public async Task Interpret(Effect effect, CancellationToken cancellati return result; } - public static DisposableInterpreter CreateDefault() - => new(); + public static DisposableInterpreter CreateDefault(Action configureServices = null) + => new(configureServices); } @@ -58,10 +58,11 @@ public class DisposableInterpreter : IInterpreter, IDisposable, IAsyncDisposable { private readonly ServiceProvider _sp; private readonly IInterpreter _interpreter; - public DisposableInterpreter() + public DisposableInterpreter(Action configureServices = null) { var services = new ServiceCollection(); services.AddEffects(); + configureServices?.Invoke(services); _sp = services.BuildServiceProvider(); _interpreter = _sp.GetRequiredService(); } diff --git a/src/Http/NBB.Http.Effects/DependencyInjectionExtensions.cs b/src/Http/NBB.Http.Effects/DependencyInjectionExtensions.cs index 173a0ad1..8b53d682 100644 --- a/src/Http/NBB.Http.Effects/DependencyInjectionExtensions.cs +++ b/src/Http/NBB.Http.Effects/DependencyInjectionExtensions.cs @@ -6,10 +6,11 @@ namespace NBB.Http.Effects { public static class DependencyInjectionExtensions { - public static void AddHttpEffects(this IServiceCollection services) + public static IServiceCollection AddHttpEffects(this IServiceCollection services) { services.AddHttpClient(); services.AddSingleton, HttpGet.Handler>(); + return services; } } } diff --git a/src/Messaging/NBB.Messaging.Effects/DependencyInjectionExtensions.cs b/src/Messaging/NBB.Messaging.Effects/DependencyInjectionExtensions.cs index 30731435..37a8dc80 100644 --- a/src/Messaging/NBB.Messaging.Effects/DependencyInjectionExtensions.cs +++ b/src/Messaging/NBB.Messaging.Effects/DependencyInjectionExtensions.cs @@ -5,9 +5,10 @@ namespace NBB.Messaging.Effects { public static class DependencyInjectionExtensions { - public static void AddMessagingEffects(this IServiceCollection services) + public static IServiceCollection AddMessagingEffects(this IServiceCollection services) { services.AddSingleton, PublishMessage.Handler>(); + return services; } } } diff --git a/test/UnitTests/Core/NBB.Core.Effects.Tests/EffectCompositionTests.cs b/test/UnitTests/Core/NBB.Core.Effects.Tests/EffectCompositionTests.cs index f9e8c5b0..093990fa 100644 --- a/test/UnitTests/Core/NBB.Core.Effects.Tests/EffectCompositionTests.cs +++ b/test/UnitTests/Core/NBB.Core.Effects.Tests/EffectCompositionTests.cs @@ -49,7 +49,7 @@ public async Task Composition_of_pure_effects_should_have_no_side_effects_3() Effect.Pure(6).Then(Double)) .Then(((int first, int second) t) => t.first + t.second); - await using var interpreter = Interpreter.CreateDefault(); + await using var interpreter = Interpreter.CreateDefault(services=> services.AddEffects()); var actual = await interpreter.Interpret(effect); actual.Should().Be(Add1(5)+Double(6));