using Microsoft.Extensions.Configuration; //extra line
using Microsoft.Extensions.Options; //extra line
public class AdminController : Controller
{
IOptions<Settings> settings; //extra line
public AdminController(IOptions<Settings> settings) //extra code
{
this.settings = settings; //extra line
}
public void RandomMethod()
{
var a = settings.Value.FinallyMySetting; // 5 extra lines to access your config, genius...
}
}
A complete and utter waste of my time to put all those lines in, it's just pointless boilerplate trash, which C# has been getting rid of excellently, all to be undone by whoever wrote Microsoft.Extensions.Configuration. And you have to do it any where you want to get a config value.On top of that, if you want to use it in a console app, you have to add like 5 packages. Yes, FIVE.
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.9" />
Then add this delightful mess just to initialize it: var environmentName = Environment.GetEnvironmentVariable("ENVIRONMENT");
var builder = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true)
.AddJsonFile($"appsettings.{environmentName}.json", true, true)
.AddEnvironmentVariables();
var configuration = builder.Build();
When you compare that to what any other framework does, it just beggars belief that they though this was a good way of handling config.When I was setting this all up, I obviously hit up SO, and it's clear from many of the (incorrect) answers on SO that people just don't get it. It's too complicated for something that should be super simple.
I get that it's handy to be able to configure all that yourself, but all of that should be default and a single line/package.