Skip to content

Service container

The built-in backend classes also implement the service container in order to simplify the binding process:

Source code in combadge/core/backend.py
class ServiceContainer(ProvidesBinder):  # noqa: D101
    def __init__(self) -> None:  # noqa: D107
        self._service_cache: Dict[Type, Any] = {}

    def __getitem__(self, protocol: Type[ServiceProtocolT]) -> ServiceProtocolT:
        """
        Bind the protocol to the current backend and return the service instance.

        This method caches the service instances, and so may be used repeatedly
        without a performance impact.

        Examples:
            >>> class ServiceProtocolA(Protocol): ...
            >>> class ServiceProtocolB(Protocol): ...
            >>>
            >>> backend = HttpxBackend()
            >>>
            >>> service_a = backend[ServiceProtocolA]
            >>> service_b = backend[ServiceProtocolB]
        """
        service = self._service_cache.get(protocol)
        if service is None:
            service = self._service_cache[protocol] = bind(protocol, self)
        return service  # noqa: RET504

    def __delitem__(self, protocol: Type) -> None:
        """
        Delete the cached service instance for the specified protocol.

        Tip: This operation is idempotent
            It is safe to remove a non-existing service instance.
        """
        self._service_cache.pop(protocol)

__getitem__

__getitem__(protocol: Type[ServiceProtocolT]) -> ServiceProtocolT

Bind the protocol to the current backend and return the service instance.

This method caches the service instances, and so may be used repeatedly without a performance impact.

Examples:

>>> class ServiceProtocolA(Protocol): ...
>>> class ServiceProtocolB(Protocol): ...
>>>
>>> backend = HttpxBackend()
>>>
>>> service_a = backend[ServiceProtocolA]
>>> service_b = backend[ServiceProtocolB]
Source code in combadge/core/backend.py
def __getitem__(self, protocol: Type[ServiceProtocolT]) -> ServiceProtocolT:
    """
    Bind the protocol to the current backend and return the service instance.

    This method caches the service instances, and so may be used repeatedly
    without a performance impact.

    Examples:
        >>> class ServiceProtocolA(Protocol): ...
        >>> class ServiceProtocolB(Protocol): ...
        >>>
        >>> backend = HttpxBackend()
        >>>
        >>> service_a = backend[ServiceProtocolA]
        >>> service_b = backend[ServiceProtocolB]
    """
    service = self._service_cache.get(protocol)
    if service is None:
        service = self._service_cache[protocol] = bind(protocol, self)
    return service  # noqa: RET504

__delitem__

__delitem__(protocol: Type) -> None

Delete the cached service instance for the specified protocol.

This operation is idempotent

It is safe to remove a non-existing service instance.

Source code in combadge/core/backend.py
def __delitem__(self, protocol: Type) -> None:
    """
    Delete the cached service instance for the specified protocol.

    Tip: This operation is idempotent
        It is safe to remove a non-existing service instance.
    """
    self._service_cache.pop(protocol)