Skip to content

Service Registration

santiago edited this page Jan 29, 2026 · 1 revision

Service Registration

The framework supports two ways to register services in your registry: simple registration and lambda registration.

Simple Registration

Use simple registration when the service class can be instantiated automatically by the container. This works when all dependencies are registered services with type annotations:

from azfunc_boot import BaseServiceRegistry, DependencyContainer, register_service

class ServicesRegistry(BaseServiceRegistry):
    def __init__(self, container: DependencyContainer):
        self.container = container
        super().__init__()

    @register_service
    def register_services(self):
        self.container.add_scoped(ExampleService)
        self.container.add_singleton(Configuration)
        self.container.add_transient(SomeService)

The container will automatically resolve dependencies using type annotations:

class ExampleService:
    def __init__(self, repository: IUserRepository, logger: ILogger):
        self.repository = repository
        self.logger = logger

Lambda Registration

Use a lambda function when:

  • The service implements an interface/abstraction
  • The constructor requires primitive types (strings, numbers, etc.)
  • You need custom instantiation logic

Registration with Interface

When registering a service that implements an interface:

@register_service
def register_services(self):
    self.container.add_scoped(
        IExampleService,
        lambda: ExampleService(self.container.get_service(ExampleDisposableClient)),
    )

Registration with Primitive Types

When your service constructor requires primitive types (strings, numbers, etc.):

@register_service
def register_services(self):
    self.container.add_scoped(
        ExampleService,
        lambda: ExampleService(
            connection_string="your-connection-string",
            timeout=30,
            client=self.container.get_service(ExampleDisposableClient)
        ),
    )

Service Lifetimes

All registration methods support the three service lifetimes:

  • add_singleton() - Single instance for the application lifetime
  • add_scoped() - New instance per scope (typically per request)
  • add_transient() - New instance every time it's requested

See Service Lifetimes for more details.

Related Topics

Clone this wiki locally