Choosing Synchronous PublicApi Calls vs. Integration Events
Decide when a .NET module needs an immediate in-process PublicApi read versus an asynchronous integration event over the Outbox and EventBus.
In Orbit, a module exposes a synchronous *.PublicApi interface only when a caller requires an immediate, read-only answer to validate a command before committing. Every cross-module state side effect—stock reservation, customer projection sync, and shipment creation—travels asynchronously via IntegrationEvents backed by PostgreSQL Outbox and Inbox tables.
Decision matrix: PublicApi vs. IntegrationEvents
| Criterion | Synchronous *.PublicApi |
Asynchronous *.IntegrationEvents |
|---|---|---|
| Direction | Read-only in-process query (ICatalogPublicApi) |
Immutable past-tense fact (OrderPlacedIntegrationEvent) |
| Transaction scope | Completes before caller commits its DbContext |
Staged in <schema>.outbox_messages inside caller’s commit |
| Temporal coupling | Caller waits (~sub-millisecond Dapper/EF read) | Decoupled; consumers process independently with retries |
| Failure handling | Returns Result.Failure / ProblemDetails immediately |
Retried with backoff; compensated via domain saga events |
Walk both communication seams in one request
When PlaceOrderCommandHandler executes:
- Synchronous read (
Catalog.PublicApi):OrderscallsICatalogPublicApi.GetActiveProductsByIdsAsyncto confirm every product isActiveand to copySku,Name,Currency, andUnitPriceontoOrderItem. If a SKU is discontinued or missing, the HTTP request fails immediately with400/404 ProblemDetailsand writes nothing. - Asynchronous fact (
Orders.IntegrationEvents): OnceOrderscommits theOrder,OrderPlacedIntegrationEventflows throughorders.outbox_messagesandOrbit.Common.EventBustoInventory. Later, when an order is cancelled,OrderCancelledIntegrationEventfans out simultaneously to bothInventory(releasing reserved stock) andShipments(cancelling the pending pick-list).
Exercise multi-subscriber cancellation across Orders, Inventory, and Shipments:
dotnet test tests/Orbit.IntegrationTests/Orbit.IntegrationTests.csproj \
--filter FullyQualifiedName~OrderCancellation_PublishesToMultipleSubscribers
Summary
- Restrict
*.PublicApiprojects to narrow DTOs and read-only lookups; never expose domain entities or mutating commands across modules. - Default cross-module workflows to past-tense
IntegrationEventsso new subscribers (Shipments, analytics, notifications) can subscribe without modifying the publisher.
Verified C# source & test evidence
Frequently asked questions
When is a synchronous cross-module PublicApi call justified in a modular monolith?
Use a synchronous PublicApi only when a command cannot validate or construct its local aggregate without an immediate read-only snapshot from another module, such as verifying active SKUs and snapshotting prices during order placement.
Why shouldn't Orders call Inventory synchronously to reserve stock during POST /api/orders?
Calling Inventory synchronously from Orders couples checkout latency and availability to warehouse lock contention and forces either two-phase commits or partial rollback bugs if the second SaveChanges fails.