4 ms·
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
by starik36 6y ago
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.
- GordonS 6y ago> 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.
- reader_mode 6y agoSmells like you're not doing DI properly :)
- scns 6y agoIs it a bad design, if it could be held wrong?
- reader_mode 6y agoWell 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.
- munchbunny 6y agoYou 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.
- p1necone 6y agoWhy does something 5 layers down need all the config values?
- starik36 6y agoNot necessarily all options, but just maybe settings for connection to some 3rd party server. You will need the configs.
- FroshKiller 6y agoWhy 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.
- BatteryMountain 6y agoCode smell OR a pattern, I believe they call it the "Options Pattern". Obviously not a real pattern like other design patterns.
- mmargerum 6y ago“5 layers down” This is precisely why I jumped ship 5 years ago to Go.