aspnet / MicrosoftConfigurationBuilders

Microsoft.Configuration.Builders
MIT License
118 stars 61 forks source link

[Question] Specify ConnStr's ProviderName in SimpleJsonConfigBuilder #194

Closed novelhawk closed 1 year ago

novelhawk commented 2 years ago

Is it possible to specify the ConnectionString's ProviderName in the json configuration builded by SimpleJsonConfigBuilder? None of the following worked:

{
    "name": {
        "[Cc]onnectionString": "...",
        "[Pp]roviderName": "..."
    }
}
{
    "name": "connectionstring",
    "name:providerName": "..."
}
<connectionString configBuilders="JsonBuilder">
    <add name="name" connectionString="random" providerName="..."/>
</connectionString>

Builder:

<add name="JsonBuilder" mode="Greedy" jsonMode="Sectional" ignoreMissingFile="false" jsonFile="~/appsettings.json" type="Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

(also does ignoreMissingFile actually exists as a property?)

Version 2.0.0

StephenMolloy commented 1 year ago

In version 2.0? You would want to use Expand mode and specify your connection string like this:

<connectionString configBuilders="JsonBuilder-InExpandMode">
    <add name="name" connectionString="${name:connectionString}" providerName="${name:providerName}"/>
</connectionString>

However -

In 3.0, Expand mode is gone. Left behind. Gone the way of the Dodo. While it was a useful mode for freestyle updates that are not restricted by xml format and ConfigurationSection objects, it came with a lot of baggage that complicated so many other things. (See the first question here for reasoning.)

However, your question got me thinking and tinkering. I started with the idea to write a long-winded piece about how this suite of builders is based on the premise of Key/Value exchange. The whole architecture is set up to do simple 1:1 replacement in some form or another. Doing Key/Value,Value exchange is different. Brings a whole other layer of complexity that these builders just aren't prepared to offer. For unique or complex needs, it would be better to write your own ConfigurationBuilder to handle your unique situation. It is a pretty simple public API of the Framework after all.

But the idea behind these builders is to provide a simple no-code entry to dynamic configuration for the ancient .Net configuration system. Specifically the two places where key/value configuration is most often needed: <appSettings> and <connectionStrings>. It didn't feel right to leave this not-uncommon scenario out of the scope of this repo just because it doesn't nicely fit the simple Key/Value model.

So I did some tinkering, and ConnectionStringsSectionHandler2 was born. It's a little more work to set up than simply hopping over to Expand mode, but you can now approach the Key/Value,Value issue in <connectionStrings> in all three available modes (Strict, Greedy, Token). Read more about how it works and how to use it here.

(ConnectionStringsSectionHandler2 has only recently checked in and is not in the 'preview' nuget package. It will be available in the upcoming 'release' package soon.)