Dependency Injection (How-to)

Updated Oct 15, 2026

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

import falcon.asgi

from falcon_pachinko import ServiceContainer, WebSocketRouter, install

app = falcon.asgi.App()
install(app)
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.attach(app, "/ws")

Configure the Falcon application and any application services before calling attach(). It registers the router's exact and descendant routes and installs the raw-frame media handlers needed by real Falcon WebSocket responders. mount() only compiles routes for simulator-based tests; it does not register the router on a Falcon application.

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.