News for hackers. Stuff that matters.
Re:.NET 5.0 by GordonS on Tuesday November 10, 2020 @01:35PM (#25049385)
> My biggest bugbear is the way you access your config, which is an absolute and utter kafka-esque mess. Because someone at MS was drinking the DI kool-aid you have to add a minimum of 5 lines of code to any class you want to access config values in

I'm not sure if maybe you're referring to the Options<T> stuff?

The way I do it is pretty simple.

1. Create a POCO that represents your config. You can have properties for bested configs if you want, e.g. SecurityConfig, MessageBusConfig 2. When the app starts, use a ConfigurationBuilder to build an IConfiguration from whatever Co fig sources you want - typically JSON files and env vars 3. Bind the IConfiguration to your POCO 4. Configure both IConfiguration and the POCO as singletons

Now you can inject the POCO config object wherever you want it, or the IConfiguration should you need it. I feel like this is simple, and works well for dev, test and prod.

Re:.NET 5.0 by Arelius on Tuesday November 10, 2020 @03:49PM (#25051708)
For other people who are curious POCO stands for "Plain Old CLR Object" So I think just a basic uninhereted object.
Re:.NET 5.0 by goric on Tuesday November 10, 2020 @04:39PM (#25052345)
Typically also implies that the object contains no real logic/functionality and is a simple container for storing some values. Something that could be a record type.
Re:.NET 5.0 by JohnL4 on Wednesday November 11, 2020 @08:24AM (#25058137)
I think of "plain old" as meaning "not having dependencies on any framework" as opposed to "not inheriting from anything" or "not having logic".
Re:.NET 5.0 by GordonS on Tuesday November 10, 2020 @05:17PM (#25052797)
Apologies, I usually remember to expand acronyms!
Re:.NET 5.0 by nikolay on Wednesday November 11, 2020 @12:52AM (#25055870)
POCO is the POJO of .Net.
Re:.NET 5.0 by jdking on Tuesday November 10, 2020 @04:17PM (#25052046)
+1 This is the approach I use, to register a config interface in the DI, just grab IConfiguration and bind it to a config class.

The options stuff is a car crash.

Re:.NET 5.0 by starik36 on Tuesday November 10, 2020 @06:24PM (#25053484)
I think the OP is referring to accessing the config 5 layers down from the controller. I've run into it myself. To be able to do that, you have to add Options<T> to the constructor of every class in the chain and then configure DI for it. It just has bad code smell.

On my last project, I just assigned the configs to a static class that's available everywhere and the code was just simply much cleaner.

Re:.NET 5.0 by GordonS on Tuesday November 10, 2020 @06:56PM (#25053778)
> To be able to do that, you have to add Options<T> to the constructor of every class in the chain and then configure DI for it.

You really don't - just inject your populated config object where you need it.

Re:.NET 5.0 by p1necone on Tuesday November 10, 2020 @10:05PM (#25055084)
Why does something 5 layers down need all the config values?
Re:.NET 5.0 by starik36 on Wednesday November 11, 2020 @11:00AM (#25059413)
Not necessarily all options, but just maybe settings for connection to some 3rd party server. You will need the configs.
Re:.NET 5.0 by FroshKiller on Wednesday November 11, 2020 @06:27PM (#25064262)
Why wouldn't you implement a service for the 3rd party that itself requires the config, then? If a caller of the service needs to know how it's configured for some reason, you should probably be exposing it as a property of the service, not sharing the config directly.
Re:.NET 5.0 by reader_mode on Tuesday November 10, 2020 @08:43PM (#25054643)
Smells like you're not doing DI properly :)
Re:.NET 5.0 by scns on Wednesday November 11, 2020 @01:05AM (#25055919)
Is it a bad design, if it could be held wrong?
Re:.NET 5.0 by reader_mode on Wednesday November 11, 2020 @01:09AM (#25055947)
Well from the sound of it he's instantiating objects manually 5 levels deep and he has control over all those constructors since he passed a new parameter through all of them.

So it sounds like basically he's not using DI (is just using it to pass stuff to controllers) and is surprised that using a library designed around DI is awkward. The correct solution is use DI to construct that hierarchy and then you can just request IOptions at the bottom/where you need it.

Re:.NET 5.0 by munchbunny on Wednesday November 11, 2020 @09:31AM (#25058684)
You can hold pretty much anything wrong. Sometimes the onus is on the user to understand the tools. Sometimes the onus is on the tools to not be unnecessarily dangerous. DI is a well understood pattern, it just takes some time to wrap your head around it.
Re:.NET 5.0 by mmargerum on Wednesday November 11, 2020 @07:59AM (#25057943)
“5 layers down”

This is precisely why I jumped ship 5 years ago to Go.

Re:.NET 5.0 by BatteryMountain on Wednesday November 11, 2020 @02:40AM (#25056310)
Code smell OR a pattern, I believe they call it the "Options Pattern". Obviously not a real pattern like other design patterns.
Re:.NET 5.0 by oaiey on Tuesday November 10, 2020 @05:31PM (#25052956)
Exactly. [Removed due to bad wording].
Re:.NET 5.0 by mattmanser on Tuesday November 10, 2020 @06:04PM (#25053285)
Well, work on your reading comprehension, I clearly said I ended up doing exactly that.

Plenty of other people piping up saying they also find this bad abstraction obnoxious, sorry you can't empathise and understand we all have difference preferences.

Re:.NET 5.0 by oaiey on Tuesday November 10, 2020 @06:12PM (#25053353)
My apologies for the wording. Was not intended like that.