✅ When using Dependency Injection, how can I pass other parameters into the constructor?
I'm just starting to get my head around the basics of DI. I've got a number of instances that were being manually created, and I'm working to get them shifted over to using DI.
Having references to other classes in the constructor makes some kind of sense to me now. However, I'm now wondering if there's a way to pass other values into the constructor.
For example, if I have the following, how could I define what
version
is in the constructor, and is that even possible?
If it's not possible, then if it's a nullable type
Having played a bit more while writing this question, this seems to work, but it only allows one value per type, and it doesn't feel like it's the right way to do it...
What's the best approach to this kind of thing?
Thanks9 Replies
I've just found this SO post which I think answers my question...
https://stackoverflow.com/questions/34834295/dependency-injection-inject-with-parameters
Stack Overflow
Dependency injection, inject with parameters
I'm using vNext implementation of DI.
How to pass parameters to constructor?
For example, i have class:
public class RedisCacheProvider : ICacheProvider
{
private readonly string _connectionS...
I'll leave this open for a bit in case anyone else has any comments on this approach
I've now got:
What I don't like about this is that
GetService()
returns a T?
, and so I need to either manually check, accept an ILogger?
in MyService
or add a !
at the end, none of which feel greatyou have to use a factory or a provider
GetRequiredService
returns a T
ahh - awesome
thanks - didn't spot that one
I think I'll go with this then. Thanks!
It's a bit of a shame that I can't just add new things to the MyService contstructor and have them appear - I have to add them in 2 places, but that's fine
if it's gonna be just a single instance of the version, you can register that with DI also
Would that be using:
Because it would need to be a specific instance
or, I guess, I could use AddSingleton with a provider...
just
AddSingleton(version)
should workah - very cool
I think that'll be my approach then
As it will just be the one instance of that type
appreciate it!
(always helpful)
np