Tenant-scoped singleton provider for dependency-injector framework
Project description
dependency-injector-tenant-singleton
Tenant-scoped singleton provider for the dependency-injector framework.
Overview
TenantSingleton is a provider that creates singleton instances scoped to individual tenants. Unlike a global singleton that returns the same instance everywhere, TenantSingleton returns the same instance for the same tenant while providing different instances for different tenants.
This is useful in multi-tenant applications where you need:
- Separate configurations per tenant
- Isolated resources for each tenant
- Tenant-scoped caching or state
Installation
pip install dependency-injector-tenant-singleton
Usage
from dependency_injector import containers, providers
from dependency_injector_tenant_singleton import TenantSingleton
def get_current_tenant() -> str:
"""Get the current tenant identifier."""
return "tenant-a"
class Container(containers.DeclarativeContainer):
#: Current active tenant
active_tenant = providers.Factory(get_current_tenant)
#: Tenant-scoped service instance
service = TenantSingleton(
active_tenant,
SomeService,
# Additional args/kwargs are passed to the factory
)
container = Container()
# Same tenant - same instance
with container.active_tenant.override("tenant-a"):
instance1 = container.service()
instance2 = container.service()
assert instance1 is instance2 # Same instance for tenant-a
# Different tenant - different instance
with container.active_tenant.override("tenant-b"):
instance3 = container.service()
assert instance3 is not instance1 # Different instance for tenant-b
# Back to tenant-a - same instance as before
with container.active_tenant.override("tenant-a"):
instance4 = container.service()
assert instance4 is instance1 # Same instance again
How It Works
TenantSingleton accepts a provider that returns a tenant identifier and a factory callable:
TenantSingleton(tenant_provider, factory, *args, **kwargs)
tenant_provider- A provider that returns the current tenant identifierfactory- Callable that creates the instance (class, function, etc.)*args,**kwargs- Additional arguments passed to the factory
Internally, TenantSingleton maintains a mapping of tenant identifiers to singleton instances. When called, it:
- Resolves the current tenant identifier from
tenant_provider - Creates a singleton for that tenant if it doesn't exist
- Returns the tenant's singleton instance
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dependency_injector_tenant_singleton-0.9.0.tar.gz.
File metadata
- Download URL: dependency_injector_tenant_singleton-0.9.0.tar.gz
- Upload date:
- Size: 2.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ad9a154f407b4dbeb53a09f6914a296cf70b788127424f4d436d8f33a9c0059
|
|
| MD5 |
b05acc39e521570305b7c93f48a1b64e
|
|
| BLAKE2b-256 |
20b2716f89baaf4d190a975cf050df6906308f657075625e58e2015af75534b1
|
File details
Details for the file dependency_injector_tenant_singleton-0.9.0-py3-none-any.whl.
File metadata
- Download URL: dependency_injector_tenant_singleton-0.9.0-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74b19405d82db22409a66934c505823de654e7b7b8773bd130fb92102546a8a0
|
|
| MD5 |
0b31bfba576a8b4b7ca8d24ef4878575
|
|
| BLAKE2b-256 |
0869d3dfa3282f92ffbe53a382d73ccc66573862b96b9d78187b47dcdbf4f7ae
|