|
| 1 | +# Schematic .NET Provider |
| 2 | + |
| 3 | +The Schematic provider allows you to connect to your Schematic instance through the OpenFeature SDK |
| 4 | + |
| 5 | +# .Net SDK usage |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +- open-feature/dotnet-sdk v1.5.0 > v2.0.0 |
| 10 | + |
| 11 | +## Install dependencies |
| 12 | + |
| 13 | +The first things we will do is install the **Open Feature SDK** and the **Schematic OpenFeature provider**. |
| 14 | + |
| 15 | +### .NET Cli |
| 16 | +```shell |
| 17 | +dotnet add package OpenFeature.Contrib.Providers.Schematic |
| 18 | +``` |
| 19 | +### Package Manager |
| 20 | + |
| 21 | +```shell |
| 22 | +NuGet\Install-Package OpenFeature.Contrib.Providers.Schematic |
| 23 | +``` |
| 24 | +### Package Reference |
| 25 | + |
| 26 | +```xml |
| 27 | +<PackageReference Include="OpenFeature.Contrib.Providers.Schematic" /> |
| 28 | +``` |
| 29 | +### Paket cli |
| 30 | + |
| 31 | +```shell |
| 32 | +paket add OpenFeature.Contrib.Providers.Schematic |
| 33 | +``` |
| 34 | + |
| 35 | +### Cake |
| 36 | + |
| 37 | +```shell |
| 38 | +// Install OpenFeature.Contrib.Providers.Schematic as a Cake Addin |
| 39 | +#addin nuget:?package=OpenFeature.Contrib.Providers.Schematic |
| 40 | + |
| 41 | +// Install OpenFeature.Contrib.Providers.Schematic as a Cake Tool |
| 42 | +#tool nuget:?package=OpenFeature.Contrib.Providers.Schematic |
| 43 | +``` |
| 44 | + |
| 45 | +## Using the Schematic Provider with the OpenFeature SDK |
| 46 | + |
| 47 | +To use Schematic as an OpenFeature provider, define your provider and Schematic settings. |
| 48 | + |
| 49 | +```csharp |
| 50 | +using OpenFeature; |
| 51 | +using OpenFeature.Contrib.Providers.Schematic; |
| 52 | +using System; |
| 53 | + |
| 54 | +var schematicProvider = new SchematicFeatureProvider("your-api-key"); |
| 55 | + |
| 56 | +// Set the schematicProvider as the provider for the OpenFeature SDK |
| 57 | +await OpenFeature.Api.Instance.SetProviderAsync(schematicProvider); |
| 58 | + |
| 59 | +// Get an OpenFeature client |
| 60 | +var client = OpenFeature.Api.Instance.GetClient("my-app"); |
| 61 | + |
| 62 | +// Set company and/or user context |
| 63 | +var context = EvaluationContext.Builder() |
| 64 | + .Set("company", new Dictionary<string, string> { |
| 65 | + { "id", "your-company-id" }, |
| 66 | + }) |
| 67 | + .Set("user", new Dictionary<string, string> { |
| 68 | + { "id", "your-user-id" }, |
| 69 | + }) |
| 70 | + .Build(); |
| 71 | + |
| 72 | +// Evaluate a flag |
| 73 | +var val = await client.GetBooleanValueAsync("your-flag-key", false, context); |
| 74 | + |
| 75 | +// Print the value of the 'your-flag-key' feature flag |
| 76 | +Console.WriteLine(val); |
| 77 | +``` |
| 78 | + |
| 79 | +You can also provide additional configuration options to the provider to manage caching behavior, offline mode, and other capabilities: |
| 80 | + |
| 81 | +```csharp |
| 82 | +using OpenFeature; |
| 83 | +using OpenFeature.Contrib.Providers.Schematic; |
| 84 | +using SchematicHQ.Client; |
| 85 | +using System; |
| 86 | + |
| 87 | +var options = new ClientOptions |
| 88 | +{ |
| 89 | + Offline = true, // Run in offline mode |
| 90 | + FlagDefaults = new Dictionary<string, bool> // Default values for offline mode |
| 91 | + { |
| 92 | + { "some-flag-key", true } |
| 93 | + }, |
| 94 | + Logger = new ConsoleLogger(), // Optional custom logger |
| 95 | + CacheProviders = new List<ICacheProvider<bool?>> // Optional cache configuration |
| 96 | + { |
| 97 | + new LocalCache<bool?>(1000, TimeSpan.FromSeconds(30)) |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +var schematicProvider = new SchematicFeatureProvider("your-api-key", options); |
| 102 | + |
| 103 | +// Set the schematicProvider as the provider for the OpenFeature SDK |
| 104 | +await OpenFeature.Api.Instance.SetProviderAsync(schematicProvider); |
| 105 | + |
| 106 | +// Get an OpenFeature client |
| 107 | +var client = OpenFeature.Api.Instance.GetClient("my-app"); |
| 108 | + |
| 109 | +// Set company and/or user context |
| 110 | +var context = EvaluationContext.Builder() |
| 111 | + .Set("company", new Dictionary<string, string> { |
| 112 | + { "id", "your-company-id" }, |
| 113 | + }) |
| 114 | + .Set("user", new Dictionary<string, string> { |
| 115 | + { "id", "your-user-id" }, |
| 116 | + }) |
| 117 | + .Build(); |
| 118 | + |
| 119 | +// Evaluate a flag |
| 120 | +var val = await client.GetBooleanValueAsync("your-flag-key", false, context); |
| 121 | + |
| 122 | +// Print the value of the 'your-flag-key' feature flag |
| 123 | +Console.WriteLine(val); |
| 124 | +``` |
0 commit comments