Skip to content

Get all services of a type

Sometimes we want to resolve all services registered for a given type. For example, we might want to resolve all implementations of an interface.

services.add_transient(NotificationService, EmailService)
services.add_transient(NotificationService, SmsService)
services.add_transient(NotificationService, WhatsAppService)


class UserService:
    def __init__(
        self,
        notification_services: Sequence[NotificationService]
    ) -> None:
        self.notification_services = notification_services

We could also resolve all implementations with a specific key.

services.add_keyed_transient("key", EmailService)
services.add_keyed_transient("key", SmsService)
services.add_keyed_transient("key", WhatsAppService)


class UserService:
    def __init__(
        self,
        notification_services: Annotated[
            Sequence[NotificationService], FromKeyedServices("key")
        ]
    ) -> None:
        self.notification_services = notification_services

Using a ServiceProvider, we can resolve all services of a type using get_services and get_keyed_services.