Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,12 @@ var builder = Host.CreateApplicationBuilder();
builder.ConfigureContainer<ServiceRegistry>(new LamarServiceProviderFactory());
```

## Splitting Configuration Across Modules <Badge type="tip" text="5.0" />

To keep your `UseWolverine()` configuration from becoming too huge or to keep specific configuration maybe
within different modules within your system, you can use [Wolverine extensions](/guide/extensions).

You can also use the `IServiceCollection.ConfigureWolverine()` method to add configuration to your
Wolverine application from outside the main `UseWolverine()` code as shown below:

snippet: sample_using_configure_wolverine
42 changes: 42 additions & 0 deletions src/Testing/CoreTests/Configuration/using_configure_wolverine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NSubstitute.Extensions;
using Wolverine.Runtime;
using Xunit;

namespace CoreTests.Configuration;

public class using_configure_wolverine
{
[Fact]
public async Task use_configure_wolverine()
{
#region sample_using_configure_wolverine

var builder = Host.CreateApplicationBuilder();

// Baseline Wolverine configuration
builder.Services.AddWolverine(opts =>
{

});

// This would be applied as an extension
builder.Services.ConfigureWolverine(w =>
{
// There is a specific helper for this, but just go for it
// as an easy example
w.Durability.Mode = DurabilityMode.Solo;
});

using var host = builder.Build();

host.Services.GetRequiredService<IWolverineRuntime>()
.Options
.Durability
.Mode
.ShouldBe(DurabilityMode.Solo);

#endregion
}
}
16 changes: 16 additions & 0 deletions src/Wolverine/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,22 @@ public static IServiceCollection UseWolverineSoloMode(this IServiceCollection se
return services;
}

/// <summary>
/// Apply either overrides or additional configuration to Wolverine in this application
/// Useful for testing overrides or for splitting configuration between modules
/// </summary>
/// <param name="services"></param>
/// <param name="configure"></param>
/// <returns></returns>
public static IServiceCollection ConfigureWolverine(this IServiceCollection services,
Action<WolverineOptions> configure)
{
var extension = new LambdaWolverineExtension(configure);
services.AddSingleton<IWolverineExtension>(extension);

return services;
}

internal class UseSoloDurabilityMode : IWolverineExtension
{
public void Configure(WolverineOptions options)
Expand Down
15 changes: 15 additions & 0 deletions src/Wolverine/IWolverineExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@ public interface IAsyncWolverineExtension
{
ValueTask Configure(WolverineOptions options);
}

internal class LambdaWolverineExtension : IWolverineExtension
{
private readonly Action<WolverineOptions> _action;

public LambdaWolverineExtension(Action<WolverineOptions> action)
{
_action = action ?? throw new ArgumentNullException(nameof(action));
}

public void Configure(WolverineOptions options)
{
_action(options);
}
}
Loading