C
C#2y ago
Shirro

❔ Client Api class best way to handle with depency injection

Hi guys, I'm using a client web api parser, and i would like to know what's the best way to go regarging dependency injection. Should i register that class as singleton and initialize it one time at the start of the app (with the api keys etc...) Or is there any other better solution ?
46 Replies
Shirro
Shirro2y ago
Also another solution would be to recreate the object each time, and configure it each time
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
No, i'm using a library that provides me a wrapper of their web APi and there is a Client class, I need to initialize with bunch of configuration (always the same in my case)
Shinyshark
Shinyshark2y ago
You have to create the same instance of that client class, each time?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
Yes for now, I'm creating and configuring it each time. I would like to use depency injection in order to avoid that
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
And register that class as singleton
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
I'm using that library not writing it
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
The client is trade safe, and can live indefinitely
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
And in general what are the best practice regarding dependency injection ? regarding Client classes
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
Ok then forget about the lifetime then, in general what would be the best way to go ? I mean I would like to avoid creating the object each time myself, and configuring it each time
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
Is it possible to use a factory method or soemthing like that coupled with dependency injection @TeBeConsulting
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
Ok so I will go transient, it's not a big deal
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
This is not the case here, Transient is fine.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
Now I would like to know how do I implement that ?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
services.AddTransient(sp => {// code to create and configure my client ?} );
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
Well, then what's the point if it's only to create a non configurated Client I would like to centralize the configuration inside that lambda
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
Oh ok using a Options class ?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
Well I don't have the hand on the Client class that's the problem
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
I can't add a constructor take my Options
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
Ok that's what I thought about. Thank you @TeBeConsulting
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
Also they have Factory.CreateClient(... differents keys) so I'm still DI compliant
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
So in that lamba (factory) it's fine to use another factory ?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
Yep
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Shirro
Shirro2y ago
Great thank you @TeBeConsulting
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts