Skip to content

Commit

Permalink
configureServices with default effect interpreter (#113)
Browse files Browse the repository at this point in the history
Co-authored-by: Radu Popovici <[email protected]>
  • Loading branch information
oncicaradupopovici and Radu Popovici authored Jan 20, 2021
1 parent f88d694 commit e57f20b
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
5 changes: 4 additions & 1 deletion src/Core/NBB.Core.Effects.FSharp/Interpreter.fs
Original file line number Diff line number Diff line change
@@ -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<IServiceCollection>(configureServices))
3 changes: 2 additions & 1 deletion src/Core/NBB.Core.Effects/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISideEffectBroker, SideEffectBroker>();
services.AddScoped<IInterpreter, Interpreter>();
return services;
}

public static IServiceCollection AddSideEffectHandler<TOutput>(this IServiceCollection services, ISideEffectHandler<ISideEffect<TOutput>, TOutput> handler)
Expand Down
7 changes: 4 additions & 3 deletions src/Core/NBB.Core.Effects/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public async Task<T> Interpret<T>(Effect<T> effect, CancellationToken cancellati
return result;
}

public static DisposableInterpreter CreateDefault()
=> new();
public static DisposableInterpreter CreateDefault(Action<IServiceCollection> configureServices = null)
=> new(configureServices);
}


Expand All @@ -58,10 +58,11 @@ public class DisposableInterpreter : IInterpreter, IDisposable, IAsyncDisposable
{
private readonly ServiceProvider _sp;
private readonly IInterpreter _interpreter;
public DisposableInterpreter()
public DisposableInterpreter(Action<IServiceCollection> configureServices = null)
{
var services = new ServiceCollection();
services.AddEffects();
configureServices?.Invoke(services);
_sp = services.BuildServiceProvider();
_interpreter = _sp.GetRequiredService<IInterpreter>();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Http/NBB.Http.Effects/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISideEffectHandler<HttpGet.SideEffect, HttpResponseMessage>, HttpGet.Handler>();
return services;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISideEffectHandler<PublishMessage.SideEffect, Unit>, PublishMessage.Handler>();
return services;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit e57f20b

Please sign in to comment.