Skip to main content

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 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.0.tar.gz (2.6 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.0.tar.gz.

File metadata

  • Download URL: dependency_injector_tenant_singleton-1.0.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

Hashes for dependency_injector_tenant_singleton-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8b52cd3604a2809aabad1315b57e6f0ec0c966f94479bf3b05855ae02bf6a581
MD5 389ec7fe8771eb07e42eeb199e5b5110
BLAKE2b-256 c13865a747cba49a548be2632ed02abad594ae8fb32f3bfe5cdfac9a546387ba

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dependency_injector_tenant_singleton-1.0.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

Hashes for dependency_injector_tenant_singleton-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3d5ed412e8fd122936bbcc4aad3a82df0c1510f92e0f01754bb883b9b6fcc13
MD5 0c9d540a92010a9855ca55c3988fd568
BLAKE2b-256 12388d706bd6e2fbd96e96737fb7eb3191eb5c1bc625bd48cb442653d75dab73

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