Skip to content

Commit bfcd449

Browse files
authored
Update README.md
Signed-off-by: Artyom Tonoyan <[email protected]>
1 parent 496a88e commit bfcd449

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

Diff for: README.md

+31-29
Original file line numberDiff line numberDiff line change
@@ -338,41 +338,43 @@ builder.Services.AddOpenFeature(featureBuilder => {
338338
});
339339
});
340340
```
341-
#### Creating a New Provider
342-
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.
343341

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.
346-
```csharp
347-
public class InMemoryProviderFactory : IFeatureProviderFactory
348-
{
349-
internal IDictionary<string, Flag>? Flags { get; set; }
350-
351-
public FeatureProvider Create() => new InMemoryProvider(Flags);
352-
}
353-
```
354-
**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.
342+
### Registering a Custom Provider
343+
You can register a custom provider, such as `InMemoryProvider`, with OpenFeature using the `AddProvider` method. This approach allows you to dynamically resolve services or configurations during registration.
356344

357345
```csharp
358-
public static partial class FeatureBuilderExtensions
359-
{
360-
public static OpenFeatureBuilder AddInMemoryProvider(this OpenFeatureBuilder builder, Action<IDictionary<string, Flag>>? configure = null)
361-
=> builder.AddProvider<InMemoryProviderFactory>(factory => ConfigureFlags(factory, configure));
346+
services.AddOpenFeature()
347+
.AddProvider(provider =>
348+
{
349+
// Resolve services or configurations as needed
350+
var configuration = provider.GetRequiredService<IConfiguration>();
351+
var flags = new Dictionary<string, Flag>
352+
{
353+
{ "feature-key", new Flag<bool>(configuration.GetValue<bool>("FeatureFlags:Key")) }
354+
};
355+
356+
// Register a custom provider, such as InMemoryProvider
357+
return new InMemoryProvider(flags);
358+
});
359+
```
362360

363-
public static OpenFeatureBuilder AddInMemoryProvider(this OpenFeatureBuilder builder, string domain, Action<IDictionary<string, Flag>>? configure = null)
364-
=> builder.AddProvider<InMemoryProviderFactory>(domain, factory => ConfigureFlags(factory, configure));
361+
#### Adding a Domain-Scoped Provider
365362

366-
private static void ConfigureFlags(InMemoryProviderFactory factory, Action<IDictionary<string, Flag>>? configure)
367-
{
368-
if (configure == null)
369-
return;
363+
You can also register a domain-scoped custom provider, enabling configurations specific to each domain:
370364

371-
var flag = new Dictionary<string, Flag>();
372-
configure.Invoke(flag);
373-
factory.Flags = flag;
374-
}
375-
}
365+
```csharp
366+
services.AddOpenFeature()
367+
.AddProvider("my-domain", (provider, domain) =>
368+
{
369+
// Resolve services or configurations as needed for the domain
370+
var flags = new Dictionary<string, Flag>
371+
{
372+
{ $"{domain}-feature-key", new Flag<bool>(true) }
373+
};
374+
375+
// Register a domain-scoped custom provider such as InMemoryProvider
376+
return new InMemoryProvider(flags);
377+
});
376378
```
377379

378380
<!-- x-hide-in-docs-start -->

0 commit comments

Comments
 (0)