Skip to content

Commit

Permalink
Merge branch 'add-dependency-injection' of https://github.com/arttono…
Browse files Browse the repository at this point in the history
…yan/dotnet-sdk into add-dependency-injection
  • Loading branch information
arttonoyan committed Oct 21, 2024
2 parents 8ebdad4 + 55582d7 commit 3089129
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace OpenFeature.Internal;

internal sealed class FeatureLifecycleManager : IFeatureLifecycleManager
internal sealed partial class FeatureLifecycleManager : IFeatureLifecycleManager
{
private readonly Api _featureApi;
private readonly IServiceProvider _serviceProvider;
Expand All @@ -19,7 +19,7 @@ public FeatureLifecycleManager(Api featureApi, IServiceProvider serviceProvider,
/// <inheritdoc />
public async ValueTask EnsureInitializedAsync(CancellationToken cancellationToken = default)
{
_logger.LogInformation("Starting initialization of the feature provider");
this.LogStartingInitializationOfFeatureProvider();
var featureProvider = _serviceProvider.GetService<FeatureProvider>();
if (featureProvider == null)
{
Expand All @@ -31,7 +31,13 @@ public async ValueTask EnsureInitializedAsync(CancellationToken cancellationToke
/// <inheritdoc />
public async ValueTask ShutdownAsync(CancellationToken cancellationToken = default)
{
_logger.LogInformation("Shutting down the feature provider.");
this.LogShuttingDownFeatureProvider();
await _featureApi.ShutdownAsync().ConfigureAwait(false);
}

[LoggerMessage(200, LogLevel.Information, "Starting initialization of the feature provider")]
partial void LogStartingInitializationOfFeatureProvider();

[LoggerMessage(200, LogLevel.Information, "Shutting down the feature provider")]
partial void LogShuttingDownFeatureProvider();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public static OpenFeatureBuilder AddContext(
Guard.ThrowIfNull(configure);

builder.IsContextConfigured = true;
builder.Services.TryAddTransient(provider => {
builder.Services.TryAddTransient(provider =>
{
var contextBuilder = EvaluationContext.Builder();
configure(contextBuilder, provider);
return contextBuilder.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public static IServiceCollection AddOpenFeature(this IServiceCollection services

if (builder.IsContextConfigured)
{
services.TryAddScoped<IFeatureClient>(static provider => {
services.TryAddScoped<IFeatureClient>(static provider =>
{
var api = provider.GetRequiredService<Api>();
var client = api.GetClient();
var context = provider.GetRequiredService<EvaluationContext>();
Expand All @@ -40,7 +41,8 @@ public static IServiceCollection AddOpenFeature(this IServiceCollection services
}
else
{
services.TryAddScoped<IFeatureClient>(static provider => {
services.TryAddScoped<IFeatureClient>(static provider =>
{
var api = provider.GetRequiredService<Api>();
return api.GetClient();
});
Expand Down
2 changes: 1 addition & 1 deletion src/OpenFeature.Hosting/FeatureStartState.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OpenFeature;
namespace OpenFeature;

/// <summary>
/// Defines the various states for starting a feature.
Expand Down
2 changes: 1 addition & 1 deletion src/OpenFeature.Hosting/FeatureStopState.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OpenFeature;
namespace OpenFeature;

/// <summary>
/// Defines the various states for stopping a feature.
Expand Down
16 changes: 11 additions & 5 deletions src/OpenFeature.Hosting/HostedFeatureLifecycleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace OpenFeature;

/// <summary>
/// A hosted service that manages the lifecycle of features within the application.
/// It ensures that features are properly initialized when the service starts
/// A hosted service that manages the lifecycle of features within the application.
/// It ensures that features are properly initialized when the service starts
/// and gracefully shuts down when the service stops.
/// </summary>
public sealed class HostedFeatureLifecycleService : IHostedLifecycleService
public sealed partial class HostedFeatureLifecycleService : IHostedLifecycleService
{
private readonly ILogger<HostedFeatureLifecycleService> _logger;
private readonly IFeatureLifecycleManager _featureLifecycleManager;
Expand Down Expand Up @@ -74,7 +74,7 @@ private async Task InitializeIfStateMatchesAsync(FeatureStartState expectedState
{
if (_featureLifecycleStateOptions.Value.StartState == expectedState)
{
_logger.LogInformation("Initializing the Feature Lifecycle Manager for state {State}.", expectedState);
this.LogInitializingFeatureLifecycleManager(expectedState);
await _featureLifecycleManager.EnsureInitializedAsync(cancellationToken).ConfigureAwait(false);
}
}
Expand All @@ -86,8 +86,14 @@ private async Task ShutdownIfStateMatchesAsync(FeatureStopState expectedState, C
{
if (_featureLifecycleStateOptions.Value.StopState == expectedState)
{
_logger.LogInformation("Shutting down the Feature Lifecycle Manager for state {State}.", expectedState);
this.LogShuttingDownFeatureLifecycleManager(expectedState);
await _featureLifecycleManager.ShutdownAsync(cancellationToken).ConfigureAwait(false);
}
}

[LoggerMessage(200, LogLevel.Information, "Initializing the Feature Lifecycle Manager for state {State}.")]
partial void LogInitializingFeatureLifecycleManager(FeatureStartState state);

[LoggerMessage(200, LogLevel.Information, "Shutting down the Feature Lifecycle Manager for state {State}")]
partial void LogShuttingDownFeatureLifecycleManager(FeatureStopState state);
}
5 changes: 3 additions & 2 deletions src/OpenFeature.Hosting/OpenFeatureBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ public static partial class OpenFeatureBuilderExtensions
/// <returns>The <see cref="OpenFeatureBuilder"/> instance.</returns>
public static OpenFeatureBuilder AddHostedFeatureLifecycle(this OpenFeatureBuilder builder, Action<FeatureLifecycleStateOptions>? configureOptions = null)
{
if(configureOptions == null)
if (configureOptions == null)
{
builder.Services.Configure<FeatureLifecycleStateOptions>(cfg => {
builder.Services.Configure<FeatureLifecycleStateOptions>(cfg =>
{
cfg.StartState = FeatureStartState.Starting;
cfg.StopState = FeatureStopState.Stopping;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task EnsureInitializedAsync_ShouldLogAndSetProvider_WhenProviderExi
.Returns(featureProvider);

// Act
await _systemUnderTest.EnsureInitializedAsync();
await _systemUnderTest.EnsureInitializedAsync().ConfigureAwait(true);

// Assert
Api.Instance.GetProvider().Should().BeSameAs(featureProvider);
Expand All @@ -49,7 +49,7 @@ public async Task EnsureInitializedAsync_ShouldThrowException_WhenProviderDoesNo
var act = () => _systemUnderTest.EnsureInitializedAsync().AsTask();

// Assert
var exception = await Assert.ThrowsAsync<InvalidOperationException>(act);
var exception = await Assert.ThrowsAsync<InvalidOperationException>(act).ConfigureAwait(true);
exception.Message.Should().Be("Feature provider is not registered in the service collection.");
}
}

0 comments on commit 3089129

Please sign in to comment.