Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 3 overloads of AddEventStoreClient #238

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,23 @@ public static IServiceCollection AddEventStoreClient(this IServiceCollection ser
options.ConnectivitySettings.Address = address;
options.CreateHttpMessageHandler = createHttpMessageHandler;
});


/// <summary>
/// Adds an <see cref="EventStoreClient"/> to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services"></param>
/// <param name="addressFactory"></param>
/// <param name="createHttpMessageHandler"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static IServiceCollection AddEventStoreClient(this IServiceCollection services,
Func<IServiceProvider, Uri> addressFactory,
Func<HttpMessageHandler>? createHttpMessageHandler = null)
=> services.AddEventStoreClient(provider => options => {
options.ConnectivitySettings.Address = addressFactory(provider);
options.CreateHttpMessageHandler = createHttpMessageHandler;
});

/// <summary>
/// Adds an <see cref="EventStoreClient"/> to the <see cref="IServiceCollection"/>.
/// </summary>
Expand All @@ -38,6 +54,17 @@ public static IServiceCollection AddEventStoreClient(this IServiceCollection ser
Action<EventStoreClientSettings>? configureSettings = null) =>
services.AddEventStoreClient(new EventStoreClientSettings(), configureSettings);

/// <summary>
/// Adds an <see cref="EventStoreClient"/> to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services"></param>
/// <param name="configureSettings"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static IServiceCollection AddEventStoreClient(this IServiceCollection services,
Func<IServiceProvider, Action<EventStoreClientSettings>> configureSettings) =>
services.AddEventStoreClient(new EventStoreClientSettings(),
configureSettings);

/// <summary>
/// Adds an <see cref="EventStoreClient"/> to the <see cref="IServiceCollection"/>.
Expand All @@ -56,6 +83,23 @@ public static IServiceCollection AddEventStoreClient(this IServiceCollection ser
return services.AddEventStoreClient(EventStoreClientSettings.Create(connectionString), configureSettings);
}

/// <summary>
/// Adds an <see cref="EventStoreClient"/> to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services"></param>
/// <param name="connectionStringFactory"></param>
/// <param name="configureSettings"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static IServiceCollection AddEventStoreClient(this IServiceCollection services,
Func<IServiceProvider, string> connectionStringFactory,
Action<EventStoreClientSettings>? configureSettings = null) {
if (services == null) {
throw new ArgumentNullException(nameof(services));
}

return services.AddEventStoreClient(provider => EventStoreClientSettings.Create(connectionStringFactory(provider)), configureSettings);
}

private static IServiceCollection AddEventStoreClient(this IServiceCollection services,
EventStoreClientSettings settings,
Expand All @@ -71,6 +115,39 @@ private static IServiceCollection AddEventStoreClient(this IServiceCollection se

return services;
}

private static IServiceCollection AddEventStoreClient(this IServiceCollection services,
Func<IServiceProvider, EventStoreClientSettings> settingsFactory,
Action<EventStoreClientSettings>? configureSettings = null) {

services.TryAddSingleton(provider => {
var settings = settingsFactory(provider);
configureSettings?.Invoke(settings);

settings.LoggerFactory ??= provider.GetService<ILoggerFactory>();
settings.Interceptors ??= provider.GetServices<Interceptor>();

return new EventStoreClient(settings);
});

return services;
}

private static IServiceCollection AddEventStoreClient(this IServiceCollection services,
EventStoreClientSettings settings,
Func<IServiceProvider, Action<EventStoreClientSettings>> configureSettingsFactory) {

services.TryAddSingleton(provider => {
configureSettingsFactory(provider).Invoke(settings);

settings.LoggerFactory ??= provider.GetService<ILoggerFactory>();
settings.Interceptors ??= provider.GetServices<Interceptor>();

return new EventStoreClient(settings);
});

return services;
}
}
}
// ReSharper restore CheckNamespace
28 changes: 28 additions & 0 deletions test/EventStore.Client.Streams.Tests/DependencyInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,41 @@ public void Register() =>
.BuildServiceProvider()
.GetRequiredService<EventStoreClient>();

[Fact]
public void RegisterWithServiceProvider() =>
new ServiceCollection()
.AddEventStoreClient(provider => { })
NielsPilgaard marked this conversation as resolved.
Show resolved Hide resolved
.BuildServiceProvider()
.GetRequiredService<EventStoreClient>();

[Fact]
public void RegisterSimple() =>
new ServiceCollection()
.AddEventStoreClient(new Uri("https://localhost:1234"))
.BuildServiceProvider()
.GetRequiredService<EventStoreClient>();

[Fact]
public void RegisterSimpleWithServiceProvider() =>
new ServiceCollection()
.AddEventStoreClient(provider => new Uri("https://localhost:1234"))
.BuildServiceProvider()
.GetRequiredService<EventStoreClient>();

[Fact]
public void RegisterWithAction() =>
new ServiceCollection()
.AddEventStoreClient(settings => { })
.BuildServiceProvider()
.GetRequiredService<EventStoreClient>();

[Fact]
public void RegisterWithActionAndServiceProvider() =>
new ServiceCollection()
.AddEventStoreClient(provider => settings => { })
.BuildServiceProvider()
.GetRequiredService<EventStoreClient>();

[Fact]
public void RegisterInterceptors() {
bool interceptorResolved = false;
Expand Down