Skip to content
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

Transient or Scoped dependency support #65

Open
sparky2708 opened this issue Sep 27, 2024 · 3 comments
Open

Transient or Scoped dependency support #65

sparky2708 opened this issue Sep 27, 2024 · 3 comments

Comments

@sparky2708
Copy link

sparky2708 commented Sep 27, 2024

Is there a way to implement transient or scoped dependency? I am thinking of a way to create services that would only exist for the life of the api request in FastApi. E.g. let’s say you get a message with 2 different sections:

[
{trees: [ {tree: {id: 1, desc: “pine”}, …]
{dogs: [ {dog: {id: 1, name: “potato”}, …]
]

When I receive this message I want to use a TreeProvider and DogProvider that source their data from the above message and can be injected just for processing that request and not for other requests.

@inject() #message will be injected
def class DogProvider(IDogProvider):
     def __init__(self, message):
          …

     def get_dogs() -> Dog:
          …

@inject() #message will be injected
def class TreeProvider(ITreeProvider):
     def __init__(self, message)
          …

     def get_trees() -> Tree:
          …

This is similar to a C# scoped or transient registration.

@sparky2708 sparky2708 changed the title Transient or Scooe dependency support Transient or Scoped dependency support Sep 27, 2024
@dkraczkowski
Copy link
Contributor

@sparky2708 I think it can be done, my only concern is that I don't think I will be able to find time to work on it. The way I use kink in my professional work differs slightly, and since I don't need this feature, its development might not be prioritized from my side. However, I'm happy to collaborate with you on it and provide my feedback.

@sparky2708
Copy link
Author

sparky2708 commented Nov 10, 2024

For process-scope I have figured out how to achieve it:

import threading

class MyController:
     def __init__():
           self.context = threading.local()
           di[IMyService] = lambda _: self.context.my_service

    def process_api_request(self, request):
           self.context.my_service = inject()(MyService)(date=request.date)
           . . .

@sparky2708
Copy link
Author

sparky2708 commented Nov 10, 2024

Ideally I’d like to see it like the following - will try to work on it when I have some free time and create a PR:

class MyController:    
    def process_api_request(self, request):
           with di.create_scope() as scope:
                scope[IMyService] = inject()(MyService)(date=request.date)
                . . .
           #scope is destroyed 
           . . .
 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants