CQRS Pattern Implementation in Swift
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓CQRS Pattern runs in O(1) time and O(n) space, in Swift as in every other language.
- ✓An enum with associated values plus an exhaustive switch, which is the same shape Rust uses and is checked the same way by the compiler.
- ✓Model commands as one enum with associated values and switch over it without a default branch, so the compiler reports every site that needs updating when you add a case.
- ✓The Swift trap: For an enum you declare yourself, a default branch throws away exhaustiveness exactly as in Rust and Java.
- ✓Switching on an enum tag compiles to a jump table and associated values are stored inline, so dispatch allocates nothing.
What CQRS Pattern actually is
CQRS Pattern in Swift: what you actually reach for
Swift implementation
import Foundation
enum Command {
case createOrder(orderID: UUID, items: [String])
case cancelOrder(orderID: UUID, reason: String)
case shipOrder(orderID: UUID, carrier: String)
}
enum DomainEvent {
case orderCreated(orderID: UUID, items: [String])
case orderCancelled(orderID: UUID, reason: String)
case orderShipped(orderID: UUID, carrier: String)
}
/// No "default", on purpose: the compiler then rejects this function the
/// moment a fourth case is added to Command, which is the whole benefit.
func handle(_ command: Command) -> DomainEvent {
switch command {
case let .createOrder(orderID, items):
return .orderCreated(orderID: orderID, items: items)
case let .cancelOrder(orderID, reason):
return .orderCancelled(orderID: orderID, reason: reason)
case let .shipOrder(orderID, carrier):
return .orderShipped(orderID: orderID, carrier: carrier)
}
}
// TWO keywords, two different situations, and only one of them is right
// for an enum you own:
//
// default -> disables exhaustiveness. Never on your own enum.
// @unknown default -> REQUIRED for a non-frozen enum imported from a
// library, which may add cases in a later version
// without breaking binary compatibility.
//
// The compiler SUGGESTS @unknown default when it applies, and people then
// apply it to their own enums too, losing the check without noticing that
// the two cases are not the same.Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Swift-specific trap
Performance in Swift, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Swift type or library should I use for CQRS Pattern?
An enum with associated values plus an exhaustive switch, which is the same shape Rust uses and is checked the same way by the compiler.
What is the most common Swift mistake when implementing CQRS Pattern?
For an enum you declare yourself, a default branch throws away exhaustiveness exactly as in Rust and Java. The wrinkle unique to Swift is the second keyword: enums imported from a library that has not been frozen require an unknown default branch, because the library may add cases in a future version without breaking binary compatibility. That is correct for library enums and completely wrong for your own, and because the compiler suggests adding unknown default when it appears, people apply it to their own enums as well and lose the check without realising the two situations are different. Use neither on an enum you own. The other consequence to know is that Swift enums are value types, so a command with a large associated payload is copied on every hand-off unless the enum is marked indirect.
How does CQRS Pattern actually perform in Swift?
Switching on an enum tag compiles to a jump table and associated values are stored inline, so dispatch allocates nothing. Marking the enum indirect boxes the payload on the heap, which is worth doing only when a case carries something genuinely large or recursive. Time complexity is O(1) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for GitHub Copilot, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.