Avoiding 'new' in IOC
Hello,
I am learning about dependency injection and one of the things said to avoid is using
new
in the constructor. How can I avoid doing stuff like this:
where I need _sendingClient
throughout multiple methods in the class?
Thanks!43 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
So I should inject
TcpClient
in the constructor?
Which means registering it in the DI
Problem is, what about stuff like a ListUnknown User•2y ago
Message Not Public
Sign In & Join Server To View
What if it were a string?
Would I need a wrapper for that as well
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Well the TcpClient part isn't as big of a deal
The main problem is where I need to initialize a
ConcurrentDictionary<int, TcpClient>
And then it is used in multiple methods in the class
That one I can't seem to wrap my head aroundUnknown User•2y ago
Message Not Public
Sign In & Join Server To View
I wish I could but I cannot upload to github
I think I will need tor research more about holding state
Or just use factory
Do you by chance have any resources on this topic which I can read
Usually things like
HttpClient/TcpClient
or DbContext
are either Transient/Scoped or injected as factories of themselvesUnknown User•2y ago
Message Not Public
Sign In & Join Server To View
Not sure how a simple question can turn in such a discussion 😆
What you are looking for is injecting your
TcpClient
as a transient
service, same as how you (probably) injected DowntimeClient
. That said, IMO having a new
keyword inside an injected class is really not a bad thing in cases, but your TcpClient does sound like a dependency you should define.Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Determinism#3334
The main problem is where I need to initialize a
ConcurrentDictionary<int, TcpClient>
Quoted by
<@!689473681302224947> from #Avoiding 'new' in IOC (click here)
React with ❌ to remove this embed.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
TcpClient
implements IDisposable
which leaks though
Wouldn't want to make it transient I thoughtDeterminism#3334
The main problem is where I need to initialize a
ConcurrentDictionary<int, TcpClient>
Quoted by
<@!689473681302224947> from #Avoiding 'new' in IOC (click here)
React with ❌ to remove this embed.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
No it doesn't
DI will dispose of it properly
Unfortunately I cannot upload my github, sorry, this was meant to be a basic question at first as I didn't really understand how in depth it was at the time
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Dependency injection guidelines - .NET
Learn various dependency injection guidelines and best practices for .NET application development.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
That's what I was thinking
My case is very basic
I'm only using it in this method
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
That's what I would put in the repo
That method is the only one that uses the
TcpClient
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I'm just initializing the
TcpClient
in the constructorUnknown User•2y ago
Message Not Public
Sign In & Join Server To View
That's why I would want to do this
But I can't because I need to avoid
new
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Hastebin: Send and Save Text or Code Snippets for Free | Toptal®
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
It actually is in a backgroundservice
The GetStream on every loop was a mistype
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Okay, thanks, so it should be fine for the
ConcurrentDictionary
as well thenUnknown User•2y ago
Message Not Public
Sign In & Join Server To View
Well I only use it in that one class
Which implements BackgroundService
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
It's a different BackgroundService
Hastebin: Send and Save Text or Code Snippets for Free | Toptal®
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Why would it do that? the id is outside the loop
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Np, thanks for looking over it