From fd03c459421c23bae6c5da3e3ee965cdf515efd5 Mon Sep 17 00:00:00 2001 From: Oli Sturm Date: Wed, 22 Apr 2026 15:07:33 +0100 Subject: [PATCH] fix money type --- csharp-fp1/Domain/Money.cs | 27 +----------------------- csharp-oop-simplified2/Domain/Money.cs | 29 ++------------------------ 2 files changed, 3 insertions(+), 53 deletions(-) diff --git a/csharp-fp1/Domain/Money.cs b/csharp-fp1/Domain/Money.cs index 61f1c2f..3795b07 100644 --- a/csharp-fp1/Domain/Money.cs +++ b/csharp-fp1/Domain/Money.cs @@ -1,32 +1,7 @@ namespace CsharpFp1.Domain; -/// Value object used to represent money and enforce simple invariants. -/// Note that this implementation uses immutable patterns for the data -/// by returning a new instance for each modification. This is an early -/// recommendation for DDD with OO, but not necessarily the common practice -/// in many real-world implementations. -public sealed class Money +public sealed record Money(decimal Amount) { - // Potentially with a setter - see note above - public decimal Amount { get; } - - public Money(decimal amount) - { - Amount = amount; - } - - // In many existing DDD/OO codebases you may actually see the use - // of mutable value types. - // - // public void Add(Money other) - // { - // this.Amount += other.Amount; - // } - - // On the other hand, sometimes these helpers may be left out - // and operations encoded directly "from the outside": - // newBalance = new Money(oldBalance.Amount - charge.Amount) - // public Money Add(Money other) => new(Amount + other.Amount); public Money Subtract(Money other) => new(Amount - other.Amount); diff --git a/csharp-oop-simplified2/Domain/Money.cs b/csharp-oop-simplified2/Domain/Money.cs index 61f1c2f..17834d6 100644 --- a/csharp-oop-simplified2/Domain/Money.cs +++ b/csharp-oop-simplified2/Domain/Money.cs @@ -1,32 +1,7 @@ -namespace CsharpFp1.Domain; +namespace CsharpOopSimplified2.Domain; -/// Value object used to represent money and enforce simple invariants. -/// Note that this implementation uses immutable patterns for the data -/// by returning a new instance for each modification. This is an early -/// recommendation for DDD with OO, but not necessarily the common practice -/// in many real-world implementations. -public sealed class Money +public sealed record Money(decimal Amount) { - // Potentially with a setter - see note above - public decimal Amount { get; } - - public Money(decimal amount) - { - Amount = amount; - } - - // In many existing DDD/OO codebases you may actually see the use - // of mutable value types. - // - // public void Add(Money other) - // { - // this.Amount += other.Amount; - // } - - // On the other hand, sometimes these helpers may be left out - // and operations encoded directly "from the outside": - // newBalance = new Money(oldBalance.Amount - charge.Amount) - // public Money Add(Money other) => new(Amount + other.Amount); public Money Subtract(Money other) => new(Amount - other.Amount);