I asked this question yesterday: Equality comparison for a surname value object class. The answerer advised that it was pointless creating a Surname Value Object and I tend to agree.
There are two benefits of removing Primitive Obsession by introducing a Value Object:
1) It makes the Domain Model more explicit. For example, I can talk to a business analyst about a Post Code instead of a string that contains a post code.
2) All the validation is in one place instead of across the application.
I believe it is point two that is pointless (because there is not enough validation logic). Please see the code below:
using Cost = System.Decimal; public class CurrencyCalculator { private readonly ICurrency currency; public CurrencyCalculator(ICurrency currency) { this.currency = currency; } public IEnumerable<KeyValuePair<int, int>> CalculateDenominationsFor(Cost cost) { var target = cost; foreach (var denomination in currency.AvailableDenominations.OrderByDescending(a => a)) { var numberRequired = target / denomination; if (numberRequired > 0) { yield return new KeyValuePair<int, int>(denomination, numberRequired); } target = target - (numberRequired * denomination); } } }
Notice that CalculateDenominationsFor is now accepting a Cost instead of a decimal (because of the type alias).
Q1) Is this normal practice?
Q2) Does this make the Domain Model more explicit without over engineering it by creating a Cost class (Value Object)?
Q3) Are there any pitfalls to this approach?