Service Locator Example

In this example you’ll see how to use the low-level Service Locator API of this framework.

We will be injecting dependencies inside our ServiceLocator class’s __init__ method by directly using the injectable.inject() and injectable.inject_multiple() service locator methods.

We declare the classes SampleService, SpecializedService, and StatefulRepository as injectables with the @injectable decorator to then inject the StatefulRepository into the SampleService classes which in turn will be injected to the ServiceLocator example class.

In ServiceLocator::run we illustrate how the injectable.inject() and injectable.inject_multiple() methods work.

Note

The high-level API, which uses decorators and annotations, is preferred over the low-level API.

See also

The Basic Usage Example describes the high-level API of this framework which is based on annotations and decorators.

See also

The Qualifier Overloading Example details how overloading an injectable works by using class inheritance.

service_locator_example.py
from examples import Example
from examples.service_locator.sample_service import SampleService
from injectable import load_injection_container, inject, inject_multiple


class ServiceLocator(Example):
    def __init__(
        self,
    ):
        self.primary_basic_service = inject(SampleService)
        self.all_basic_service_implementations = inject_multiple(SampleService)

    def run(self):
        print(self.primary_basic_service.get_repository_state())
        # None

        for service in self.all_basic_service_implementations:
            print(service.get_repository_state())
            # None
            # None

        for service in self.all_basic_service_implementations:
            service.set_repository_state(0)
            print(service.get_repository_state())
            # 0
            # 0

        self.primary_basic_service.set_repository_state(1)

        for service in self.all_basic_service_implementations:
            print(service.get_repository_state())
            # 1
            # 0

        for service in self.all_basic_service_implementations:
            service.set_repository_state(service.get_repository_state() + 1)
            print(service.get_repository_state())
            # 2
            # 1


def run_example():
    load_injection_container()
    example = ServiceLocator()
    example.run()


if __name__ == "__main__":
    run_example()
sample_service.py
from examples.service_locator.stateful_repository import StatefulRepository
from injectable import injectable, inject


@injectable(primary=True)
class SampleService:
    def __init__(self):
        self.repository: StatefulRepository = inject(StatefulRepository)

    def set_repository_state(self, state):
        self.repository.state = state

    def get_repository_state(self):
        return self.repository.state
specialized_service.py
from examples.service_locator.sample_service import SampleService
from injectable import injectable


@injectable
class SpecializedService(SampleService):
    pass
stateful_repository.py
from injectable import injectable


@injectable
class StatefulRepository:
    def __init__(self):
        self._state = None

    @property
    def state(self):
        return self._state

    @state.setter
    def state(self, value):
        self._state = value