A design walkthrough of modeling money with type safety in mind, using Domain-Driven Design principles. Starting from a basic Fowler-style Money value object, the author iteratively refines the design by introducing PreciseMoney and RoundedMoney as distinct types, currency-specific subtypes (e.g., PreciseEUR, RoundedBTC), and a ConversionRate value object to separate side-effectful exchange rate fetching from pure conversion logic. Key benefits highlighted include compiler-enforced contracts, reduced test burden through declarative typing, Single Responsibility Principle adherence, and minimalist interfaces. The post also touches on Bounded Contexts, the Repository pattern for value objects, and extends the type-safety concept to timezone handling.

17m read timeFrom verraes.net
Post cover image
Table of contents
HistoryDesign FlawsRequirementsAssumptionsFowler-styleItchType JugglingConversionsSimpler ElementsCurrency Type SafetyMinimalist InterfacesSingle ResponsibilityParent interface?OverdesignPracticalityDefensive modelGoing FurtherTimezone Type SafetyConclusion