Skip to content

Application facades

Facades provide typed access to application services from application code. They resolve the active application’s canonical service through its Container. Use dependency injection inside reusable services and controllers. Use facades at application entry points where they improve clarity.

Titanspan provides Config, Log, Http, Route, Event, and Cache. Each facade declares its native contract and exposes explicit methods. Facades do not forward unknown attributes dynamically and do not keep service instances in process-global caches.

from titanspan import Application
from titanspan.facades import Cache, Config, Log
application = Application(config={"app": {"name": "Example"}})
with application.context():
assert Config.string("app.name") == "Example"
Cache.put("app:ready", True)
Log.info("Application ready")

Application execution establishes this context through Foundation. A facade call outside an active application context raises a native context error.

Use Facade.use() to replace one facade contract for the current execution context:

from titanspan import Application
from titanspan.config import Repository
from titanspan.contracts.config import ConfigRepository
from titanspan.facades import Config
application = Application()
replacement = Repository({"app": {"name": "Test"}})
with application.context(), Config.use(replacement):
assert application.make(ConfigRepository) is replacement
assert Config.get("app.name") == "Test"

The replacement applies to direct resolution and facade calls. Nested scopes restore their parent service. Exceptions and cancellation also restore the prior Container graph. The caller owns the replacement instance.

Replacements follow Container safety rules. Enter them before execution scopes. The Container rejects a replacement when a cached singleton already captures the replaced dependency. Use the Container’s documented override behavior for additional replacement types.

Component test helpers retain their component-specific behavior. Log.fake() replaces the logging manager through the Container and records calls in a bounded memory sink. A logger injected from the Container observes the same fake manager. Http.fake() selects a local fake transport for outgoing requests. Event.fake() uses the active application’s dispatcher. Cache.fake() replaces cache operations in the current execution context; facade calls, injected repositories, and direct manager access share the fake.

Use the relevant component guide for fake setup, assertions, and lifecycle. Do not use facade replacement as a production service registry.

See the Facade behavioral specification for supported methods, ownership rules, failure behavior, and test evidence.