Skip to main content

Tenant-scoped singleton provider for dependency-injector framework

Project description

PyPI - Python Version Build Status Latest Version Supported Python versions License

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 identifier
  • factory - 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:

  1. Resolves the current tenant identifier from tenant_provider
  2. Creates a singleton for that tenant if it doesn't exist
  3. Returns the tenant's singleton instance

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dependency_injector_tenant_singleton-1.0.1.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file dependency_injector_tenant_singleton-1.0.1.tar.gz.

File metadata

  • Download URL: dependency_injector_tenant_singleton-1.0.1.tar.gz
  • Upload date:
  • Size: 3.0 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

Hashes for dependency_injector_tenant_singleton-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d4da4eb9ef284c633f79b3ce96efc99444393c2e57a6447b3376f2f6b9817fe4
MD5 392a3800d945199b0d070cb9a11d381a
BLAKE2b-256 02eaed441c0c59b21545227176aa8e07c33ac4b3925e25d407b511f02dbfc496

See more details on using hashes here.

File details

Details for the file dependency_injector_tenant_singleton-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: dependency_injector_tenant_singleton-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.3 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

Hashes for dependency_injector_tenant_singleton-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ba67aec720c6102bc531f3b50cd8567392222d4cf615ae5a575041860cdcfd94
MD5 30b4c913ff9e7a50ea34e5ac165b7208
BLAKE2b-256 33f43751100b5f541d86c9e1e7a66b95dd44401caf74acd1d28f6b3e56ca78cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page