๐ Dependency Injection (DI) with FletX¶
FletX provides a lightweight and flexible manual dependency injection system, inspired by GetX. It allows you to register and retrieve singleton instances of your services, repositories, and other dependencies from anywhere in your app.
๐ Getting Started¶
All interactions with the DI container are done via the FletX class:
from fletx import FletX
This exposes the following methods:
| Method | Description |
|---|---|
FletX.put(instance) |
Registers a singleton instance |
FletX.find(MyClass) |
Retrieves the registered instance |
FletX.delete(MyClass) |
Removes an instance from the container |
FletX.reset() |
Clears the entire DI container |
โ Registering an Instance¶
from fletx import FletX
class AuthService:
def __init__(self):
self.user = None
def login(self, username):
self.user = username
FletX.put(AuthService())
This registers a singleton of
AuthServicethat you can use throughout your app.
๐ฆ Retrieving an Instance¶
auth = FletX.find(AuthService)
auth.login("johndoe")
This retrieves the instance you registered with
FletX.put().
๐ท Using Tags¶
If you need multiple versions of the same class:
FletX.put(AuthService(), tag="admin")
FletX.put(AuthService(), tag="user")
admin_auth = FletX.find(AuthService, tag="admin")
user_auth = FletX.find(AuthService, tag="user")
๐งน Deleting and Resetting¶
FletX.delete(AuthService)
FletX.reset()
delete()removes a single instance;reset()clears the entire container.
๐งช Example Usage¶
Step 1: Register your service¶
FletX.put(AuthService())
Step 2: Retrieve and use it in your view, route, or controller¶
class Dashboard:
def __init__(self):
self.auth = FletX.find(AuthService)
def do_stuff(self):
...
โ ๏ธ Notes¶
- ๐ง FletX does not support automatic dependency resolution (e.g., constructor or function injection) yet.
- ๐ก You must manually retrieve your dependencies using
FletX.find().
๐ Recap¶
| Task | Code Example |
|---|---|
| Register a service | FletX.put(MyService()) |
| Retrieve a service | FletX.find(MyService) |
| Register multiple | FletX.put(MyService(), tag="x") |
| Retrieve with tag | FletX.find(MyService, tag="x") |
| Remove a service | FletX.delete(MyService) |
| Clear all services | FletX.reset() |
๐ง Next Steps¶
- Explore sevices
- Dive into routing
- Learn about the Architecture