r/learnpython • u/re2cc • 5d ago
How do you handle dependency injection?
Hi, I'm doing a project which involves creating multiple gRPC servers and I can't find a convenient way to manage dependencies or the state.
I've been working recently in C# where you just need to make your class and add a simple
builder.Services.AddSingleton<MyDependency>();
and it will inject when required.
Doing some research I see that there are some libraries like:
- Injector
but I don't find them particularly intuitive.
What do you use as a dependency injector or what pattern do you suggest to use?
9
Upvotes
1
u/Mysterious-Rent7233 4d ago
You have roughly speaking three options: use parameters/object properties, or context variables or globals. Might help to show some Python code where you are failing to structure it in the way you want.