kodemore / kink

Dependency injection container made for Python
MIT License
404 stars 26 forks source link

Transient or Scoped dependency support #65

Open sparky2708 opened 3 weeks ago

sparky2708 commented 3 weeks ago

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.

dkraczkowski commented 1 day ago

@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.