Clean Markdown mirror for AI agents:
/guides/module-communication.md · /llms.txt
Walkthrough

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:

  1. Synchronous read (Catalog.PublicApi): Orders calls ICatalogPublicApi.GetActiveProductsByIdsAsync to confirm every product is Active and to copy Sku, Name, Currency, and UnitPrice onto OrderItem. If a SKU is discontinued or missing, the HTTP request fails immediately with 400/404 ProblemDetails and writes nothing.
  2. Asynchronous fact (Orders.IntegrationEvents): Once Orders commits the Order, OrderPlacedIntegrationEvent flows through orders.outbox_messages and Orbit.Common.EventBus to Inventory. Later, when an order is cancelled, OrderCancelledIntegrationEvent fans out simultaneously to both Inventory (releasing reserved stock) and Shipments (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 *.PublicApi projects to narrow DTOs and read-only lookups; never expose domain entities or mutating commands across modules.
  • Default cross-module workflows to past-tense IntegrationEvents so 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.

Keep exploring