-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Using factory for provider creation #157
feat: Using factory for provider creation #157
Conversation
Signed-off-by: Vladimir Petrusevici <[email protected]>
de9d787
to
cc5fd4d
Compare
Codecov Report
@@ Coverage Diff @@
## main #157 +/- ##
==========================================
+ Coverage 93.91% 93.93% +0.02%
==========================================
Files 20 20
Lines 542 544 +2
Branches 39 41 +2
==========================================
+ Hits 509 511 +2
Misses 20 20
Partials 13 13
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
Signed-off-by: Vladimir Petrusevici <[email protected]>
Signed-off-by: Vladimir Petrusevici <[email protected]>
Signed-off-by: Vladimir Petrusevici <[email protected]>
Signed-off-by: Vladimir Petrusevici <[email protected]>
I have another (I think better) option. Just make API constuctor public. So we could use smth like this
|
or even like this
|
With .net8 we could even use keyed-services to inject specific client. |
My final working solution with DI and without factories and singleton providers:
|
I'm a bit confused. I can definitely see the value of a client per request (we use this pattern internally at my org) but not a provider per request. A provider per request seems like a bad idea in many cases - there's lots of overhead associated with initializing some providers. Am I misunderstanding? |
I want to say that some of implementations need transient or scoped lifecycle (like flagsmith which uses httpClient). |
This PR
Notes
This is a problem when your provider uses some services like HttpClient (which should be scoped or transient to avoid DNS problems) or configuration that can be changed in runtime. Using factories we can configure lifecycle of providers by registering a client in DI.
How to test
In this example I can change configuration at runtime and be sure that new Provider with all deps will be created per request.
You still can set singleton instance of provider or register client itself as singleton.
This is not the best DI using, but it looks difficult to change current static implementation without major changes