Skip to content

Dependency Injection

Dependency Injection pattern (DI) separates the concerns of constructing objects and using them, leading to loosely coupled programs.

Usage in Diator

In Diator, dependency injection is implemented via special Container class, which is literally an abstraction over various dependency injection frameworks. This allows developers to easily manage dependencies and decouple components, leading to more modular and maintainable code. Currently, Diator supports two popular DI-frameworks, adriangb/di and Neoteroi/rodi, but we plan to expand support for other frameworks in the future.

Neoteroi/rodi

Configure by 2 steps:

  1. Setup dependencies:

    from rodi import Container
    
    
    def configure_di():
        container = Container()
    
        container.register(UserJoinedEventHandler)
        container.register(JoinMeetingRoomCommandHandler)
    
  2. Integrate with Diator:

    from rodi import Container
    
    from diator.container.rodi import RodiContainer
    
    
    def configure_di() -> RodiContainer:
        container = Container()
    
        container.register(UserJoinedEventHandler)
        container.register(JoinMeetingRoomCommandHandler)
    
        rodi_container = RodiContainer()
        rodi_container.attach_external_container(container)
    
        return rodi_container
    

adriangb/di

Configure by 2 steps:

  1. Setup dependencies:

    from di import Container, bind_by_type
    from di.dependent import Dependent
    
    
    def configure_di():
        container = Container()
    
        container.bind(bind_by_type(Dependent(UserJoinedEventHandler, scope="request"), UserJoinedEventHandler))
        container.bind(
            bind_by_type(
                Dependent(JoinMeetingRoomCommandHandler, scope="request"),
                JoinMeetingRoomCommandHandler,
            )
        )
    
  2. Integrate with Diator:

    from di import Container, bind_by_type
    from di.dependent import Dependent
    
    from diator.container.di import DIContainer
    
    
    def configure_di() -> DIContainer:
        container = Container()
    
        container.bind(bind_by_type(Dependent(UserJoinedEventHandler, scope="request"), UserJoinedEventHandler))
        container.bind(
            bind_by_type(
                Dependent(JoinMeetingRoomCommandHandler, scope="request"),
                JoinMeetingRoomCommandHandler,
            )
        )
    
        di_container = DIContainer()
        di_container.attach_external_container(container)
    
        return di_container
    

Recources