You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Add Dependency Injection and Hosting support for OpenFeature (#310)
> [!NOTE]
> This initial version of OpenFeature.DependencyInjection and
OpenFeature.Hosting introduces basic provider management and lifecycle
support for .NET Dependency Injection environments. While I haven't
included extensive tests, particularly for the Hosting project, if this
approach is approved and the scope is considered sufficient for the
first DI and Hosting release, I will expand the test coverage to include
both unit and integration tests. Additionally, I'll provide a sample
application to demonstrate the usage.
This pull request introduces key features and improvements to the
OpenFeature project, focusing on Dependency Injection and Hosting
support:
- **OpenFeature.DependencyInjection Project:**
- Implemented `OpenFeatureBuilder`, including
`OpenFeatureBuilderExtensions` for seamless integration.
- Added `IFeatureLifecycleManager` interface and its implementation.
- Introduced `AddProvider` extension method for easy provider
configuration.
- Created `OpenFeatureServiceCollectionExtensions` for service
registration.
- **OpenFeature.Hosting Project:**
- Added `HostedFeatureLifecycleService` to manage the lifecycle of
feature providers in hosted environments.
- **Testing Enhancements:**
- Created unit tests for critical methods, including
`OpenFeatureBuilderExtensionsTests` and
`OpenFeatureServiceCollectionExtensionsTests`.
- Replicated and tested `NoOpFeatureProvider` implementation for better
test coverage.
These changes significantly improve OpenFeature's extensibility and
lifecycle management for feature providers within Dependency Injection
(DI) and hosted environments.
---
**NuGet Packages for installation:**
```bash
dotnet add package OpenFeature
dotnet add package OpenFeature.DependencyInjection
dotnet add package OpenFeature.Hosting
```
---
**Usage Example:**
```csharp
builder.Services.AddOpenFeature(featureBuilder => {
featureBuilder
.AddHostedFeatureLifecycle() // From Hosting package
.AddContext((context, serviceProvider) => {
// Context settings are applied here. Each feature flag evaluation will
// automatically have access to these context parameters.
// Do something with the service provider.
context
.Set("kind", "tenant")
.Set("key", "<some key>");
})
// Example of a feature provider configuration
// .AddLaunchDarkly(builder.Configuration["LaunchDarkly:SdkKey"], cfg => cfg.StartWaitTime(TimeSpan.FromSeconds(10)));
});
```
Signed-off-by: Artyom Tonoyan <[email protected]>
Built a new hook? [Let us know](https://github.com/open-feature/openfeature.dev/issues/new?assignees=&labels=hook&projects=&template=document-hook.yaml&title=%5BHook%5D%3A+) so we can add it to the docs!
302
303
304
+
### DependencyInjection
305
+
> [!NOTE]
306
+
> The OpenFeature.DependencyInjection and OpenFeature.Hosting packages are currently experimental. They streamline the integration of OpenFeature within .NET applications, allowing for seamless configuration and lifecycle management of feature flag providers using dependency injection and hosting services.
307
+
308
+
#### Installation
309
+
To set up dependency injection and hosting capabilities for OpenFeature, install the following packages:
<br />To set up multiple providers with a selection policy, define logic for choosing the default provider. This example designates `name1` as the default provider:
To integrate a custom provider, such as InMemoryProvider, you’ll need to create a factory that builds and configures the provider. This section demonstrates how to set up InMemoryProvider as a new provider with custom configuration options.
343
+
344
+
**Configuring InMemoryProvider as a New Provider**
345
+
<br />Begin by creating a custom factory class, `InMemoryProviderFactory`, that implements `IFeatureProviderFactory`. This factory will initialize your provider with any necessary configurations.
**Adding an Extension Method to OpenFeatureBuilder**
355
+
<br />To streamline the configuration process, add an extension method, `AddInMemoryProvider`, to `OpenFeatureBuilder`. This allows you to set up the provider with either a domain-scoped or a default configuration.
0 commit comments