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 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
4 changes: 3 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ jobs:
- name: Dotnet Pack
shell: bash
run: |
dotnet pack /p:Version=${{ steps.get_version.outputs.version }} --configuration=Release --output=./packages \
mkdir -p packages
dotnet pack /p:Version=${{ steps.get_version.outputs.version }} --configuration=Release \
/p:PublishDir=./packages \
/p:NoWarn=NU5105 \
/p:RepositoryUrl=https://github.com/EventStore/EventStore-Client-Dotnet \
/p:RepositoryType=git
Expand Down
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
37 changes: 36 additions & 1 deletion test/EventStore.Client.Streams.Tests/DependencyInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,47 @@ public void Register() =>
.GetRequiredService<EventStoreClient>();

[Fact]
public void RegisterSimple() =>
public void RegisterWithConnectionString() =>
new ServiceCollection()
.AddEventStoreClient("esdb://localhost:2113?tls=false")
.BuildServiceProvider()
.GetRequiredService<EventStoreClient>();

[Fact]
public void RegisterWithConnectionStringFactory() =>
new ServiceCollection()
.AddEventStoreClient(connectionStringFactory: provider => "esdb://localhost:2113?tls=false")
.BuildServiceProvider()
.GetRequiredService<EventStoreClient>();

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

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

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

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

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