Dependency Injection (How-to)

Updated Dec 08, 2025

1) Choose a factory – Pass resource_factory to WebSocketRouter. 2) Provide services – Register shared services in the container. 3) Mount the router – All route instantiation flows through your factory.

from falcon_pachinko import ServiceContainer, WebSocketRouter

conn_mgr = app.ws_connection_manager
container = ServiceContainer()
container.register("conn_mgr", conn_mgr)
container.register("db", my_db_pool)

router = WebSocketRouter(resource_factory=container.create_resource)
router.add_route("/rooms/{room}", ChatResource)
router.mount("/ws")

Test-friendly factories

from falcon_pachinko.unittests.resource_factories import resource_factory

router = WebSocketRouter(resource_factory=resource_factory(fake_service))
  • Inject mocks/spies in tests without bootstrapping the production container.
  • See tests/behaviour/dependency_injection.feature for an end-to-end example.