6 ms·
>and you're in for an even bigger nightmare if you want to separate business logic and web code into two projects (which is a pretty common design). I've given
by tester756 6y ago
>and you're in for an even bigger nightmare if you want to separate business logic and web code into two projects (which is a pretty common design). I've given up with the config DI and just assign them all to static variables.
So you have static variable in web project and then add reference to web project to obtain that config? Do I get that right?
I never felt like config DI was a problem once you did all the stuff that you do at the beginning of the project
- mattmanser 6y agoTo me it's just so pointless, here's an example: 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.
- nightski 6y agoDo you type every line typically? If you use Rider or VS pretty much every one of those lines can be auto generated with autocomplete. Also, this can easily be worked around. Just create a Singleton which has the settings injected into it. I prefer the testability of the injected version but if you'd rather access a static global it is very easy to do.
- tester756 6y agoI do agree I just type IOptions<T> config in ctor, use ctrl+. and private readonly property and it's initialization is generated
- mattmanser 6y agoThat doesn't work unless you already have the using statements, which you won't have as you haven't added it yet. I just tried it.
- deleted 6y ago[deleted]
- tester756 6y agoI'm using Roslynator: https://i.imgur.com/cWs1M77.png https://i.imgur.com/cWs1M77.png and I have some naming rule https://i.imgur.com/iPwNj5b.png https://i.imgur.com/iPwNj5b.png So I basically have to type IOptions<T> Name and use ctrl+. twice in order to generate private readonly property and initialize it and add using to IOptions
- merb 6y agoyou can use property injection when you use something like autofac as the di container.
- FridgeSeal 6y agoCool, so I'll add another dependency and figure out how to use that, to save myself time having to write out MS's convoluted dependency-injected configuration code.