Emberfire
Emberfire
CC#
Created by Emberfire on 7/5/2024 in #help
Dependency injection approach for object, used only in a single method
I'm going to be testing this solution and I'll report back with any problems I find with the approach. Thank you very much for taking the time!
27 replies
CC#
Created by Emberfire on 7/5/2024 in #help
Dependency injection approach for object, used only in a single method
Here is the question posted on StackOverflow and here is the article mentioned.
27 replies
CC#
Created by Emberfire on 7/5/2024 in #help
Dependency injection approach for object, used only in a single method
Absolutely. Give me a moment to respond to the guy in SO and I'll post everything here.
27 replies
CC#
Created by Emberfire on 7/5/2024 in #help
Dependency injection approach for object, used only in a single method
Okay, so this lazy instantiation seems like a good approach to my problem. It basically works like IEnumerable<T> does for collections: it injects placeholder values for the objects needed, then instantiates them the first time they are used. This seems like exactly what I need, and doesn't add obvious drawbacks. I'm going to read up on it some more to learn if it's also considered an anti-patter, bad practice or anything similar
27 replies
CC#
Created by Emberfire on 7/5/2024 in #help
Dependency injection approach for object, used only in a single method
I feel like this would be an even worse anti-pattern than just using a IServiceProvider 😄
27 replies
CC#
Created by Emberfire on 7/5/2024 in #help
Dependency injection approach for object, used only in a single method
Currently reading up on it, give me a moment
27 replies
CC#
Created by Emberfire on 7/5/2024 in #help
Dependency injection approach for object, used only in a single method
I posted the question to StackOverflow as well and got an interesting comment about Lazy<T> injections
27 replies
CC#
Created by Emberfire on 7/5/2024 in #help
Dependency injection approach for object, used only in a single method
This sounds like a better approach. However, I don't know how much of it can be implemented in my situation, since I'm trying to not have to rewrite the entire application during this migration
27 replies
CC#
Created by Emberfire on 7/5/2024 in #help
Dependency injection approach for object, used only in a single method
Unfortunately this would result in hundreds of ApiControllers, since the application is quite large, and that would make supporting it even more difficult than if I just used a IServiceProvider, for example
27 replies
CC#
Created by Emberfire on 7/5/2024 in #help
Dependency injection approach for object, used only in a single method
I could of course separate out this controller, but as per my P.S. I feel this would just kick the can down the road, as the derived new class would still need to be instantiated somewhere
27 replies
CC#
Created by Emberfire on 7/5/2024 in #help
Dependency injection approach for object, used only in a single method
It does not, however in the context this TestClass is a controller, and to my understanding they usually serve as a hub for routing requests to business logic
27 replies
CC#
Created by Emberfire on 7/5/2024 in #help
Dependency injection approach for object, used only in a single method
P.S. One approach would be to indeed separate the method to a different object. However, doesn't that just kick the can down the road? If you would need to then inject that object to call the method, doesn't the injection still happen for the whole TestClass?
27 replies