microsoft/FeatureManagement-Dotnet

Support for Code-first Configuration (builder)

mike7ang1rdz opened this issue · 6 comments

I would like to package my features within the dll without JSON to avoid modifications when we distribute the dll (package).

ideally using a builder

thanks

@mike7ang1rdz

are you able to provide an example of what type of code you are looking to be able to write. Even if pseudo-code. Ideally it can help seed the conversation and make sure we're accomplishing what you're looking for.

Sure.

Serilog is a great example

  1. You can build an instance using a builder:
using var log = new LoggerConfiguration()
    .WriteTo.Console()
    .WriteTo.File("log.txt")
    .CreateLogger();

  1. Or creating an instance reading from appsettings.json

{
  "Serilog": {
    "Using":  [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      { "Name": "Console" },
      { "Name": "File", "Args": { "path": "Logs/log.txt" } }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
    "Destructure": [
      { "Name": "With", "Args": { "policy": "Sample.CustomPolicy, Sample" } },
      { "Name": "ToMaximumDepth", "Args": { "maximumDestructuringDepth": 4 } },
      { "Name": "ToMaximumStringLength", "Args": { "maximumStringLength": 100 } },
      { "Name": "ToMaximumCollectionCount", "Args": { "maximumCollectionCount": 10 } }
    ],
    "Properties": {
        "Application": "Sample"
    }
  }
}

https://github.com/serilog/serilog-settings-configuration

@mike7ang1rdz
The FeatureManagement is built on the IConfiguration. After release 3.1.0, FeatureManager and ConfigurationFeatureDefintionProvider are exposed to public. You can use them directly without DI.

I am digesting your request. If my understanding is correct, for your case, the feature flag will be hard coded.
You can create IConfiguration like this ref and create an instance of ConfigurationFeatureDefinitionProvider manually.

var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>()
{
    ["MyFeature"] = "true"
})
.Build();

var featureDefinitionProvider = new ConfigurationFeatureDefinitionProvider(configuration);

Hello @mike7ang1rdz, does the approach that @zhiyuanliang-ms suggested work for your scenario?

It works perfectly thanks.

You can use them directly without DI.

To expand upon what @zhiyuanliang-ms mentioned, this approach can also be used when FeatureManager is in .NET core's DI container. Just make sure to add the in-memory collection to the main configuration that is also added to DI.