✅ Access a runtime Dictionary easier
My company is switching from
ConfigurationManager
with transforms over to a 3rd party secrets manager. The secrets manager returns a Dictionary<string, object>
on start up. I'm needing to go through and replace all ConfigurationManager
calls throughout the project and I was wondering if there's an easier way to access these values rather than magic strings. My first thought is a static class where I define all the property shortcuts and keys. Then verify all keys are present on project load when we get the secrets. Am I barking up the wrong tree and there's an established way?18 Replies
So you're not using IConfiguration?
General guidance in order to avoid the issue you mentioned is to use IOptions and go through DI
Instead of using magic strings everywhere
Your company decided to go with something that's not type-safe and relies on magic strings
So, yeah
Magic strings it is
I wonder what kind of 3rd party config provider this is
Sure, you could store the keys in constants or something, and call
or you could use
IOptions
pattern and just do
like a sane human being
If your company is made up of halfwits, then sure, constants make more sense than magic strings everywhereIt's a .NET Framework project from 2014 that we're slowly porting to core that has changed hands more than a dozen times.
But if you have any pull, slap them in the face and tell them to do it correctly
An outdated version of framework doesn't mean you have to settle for shit code
The most proper way would be to get a good and proper strongly-typed object instead of a dictionary
No, but it does say a lot about the quantity of tech debt that has been established for more than a decade.
If the proper way is a no-go for whatever reason (it's 100% a bullshit reason btw) you can go with constants as you planned
If it's of any use, you can make an options-style binding work in net framework as well, worst case you might have to do a bit of custom binding but I'm reasonably sure there's a path to make it work as well, granted you have a DI container setup in place
We'd probably have to wait for a DI option until after the full upgrade.
I imagine we'd want to use the built-in. We don't use IoC containers atm.
I'll look into the IOptions pattern though.
Thanks!
If you're not familiar with it it'll probably be a good read to get the idea behind transforming configuration based on key value indexing to a properly typed configuration object to pass around, yeah
Yep. Will do, thanks! Wasn't sure if there was an established pattern. Good to know.
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
appsettings.json:
src/Foo/FooOptions.cs:
src/Foo/FooServiceCollectionExtensions.cs:
Program.cs / Startup.cs:
Bar.cs:
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Okay, I made some progress on it. HashiCorp returns the secrets in a
Dictionary<string, object>
or a custom object if provided. The problem I'm on is I can't find any documentation on how to bind that to a configuration. The closest I could come up with is a json stream, by serializing a custom object then passing that in as a memory stream but that seems like a hack.Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Ah. Didn't realize someone else closed it. No worries. I'll figure something out.