❔ 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
Also another solution would be to recreate the object each time, and configure it each time
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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)
You have to create the same instance of that client class, each time?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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•2y ago
Message Not Public
Sign In & Join Server To View
And register that class as singleton
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I'm using that library not writing it
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
The client is trade safe, and can live indefinitely
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
And in general what are the best practice regarding dependency injection ?
regarding Client classes
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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•2y ago
Message Not Public
Sign In & Join Server To View
Is it possible to use a factory method or soemthing like that coupled with dependency injection @TeBeConsulting
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Ok so I will go transient, it's not a big deal
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
This is not the case here, Transient is fine.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Now I would like to know how do I implement that ?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
services.AddTransient(sp => {// code to create and configure my client ?} );
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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•2y ago
Message Not Public
Sign In & Join Server To View
Oh ok
using a Options class ?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Well I don't have the hand on the Client class
that's the problem
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I can't add a constructor take my Options
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Ok that's what I thought about. Thank you @TeBeConsulting
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Also they have Factory.CreateClient(... differents keys) so I'm still DI compliant
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
So in that lamba (factory) it's fine to use another factory ?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Yep
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Great thank you @TeBeConsulting
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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.