Short Version: What do you think is the best option for a Value Object that needs to access currency conversion rates in the database? ex:
Invoice.Amount = Invoice.Amount.toCurrency('CAD')
Long Version:
I have a Value Object called InvoiceAmount
and it has a method, toCurrency
, that will convert the invoice amount to a specified currency. I'd like to reuse this currency conversion logic because I know I'm going to need it in other bounded contexts(e.g. SalesOrderAmount
, FreightCost
, etc.). My first thought was to create a Money
object in a SharedKernel and then have SalesOrderAmount
, FreightCost
and InvoiceAmount
inherit from Money
. The toCurrency
would be implemented in the Money
class so it is only in one place. This sounds ok to me but maybe there is a better way. Now the problem I have is that in my system the currency conversion rates are stored in the database so I need a repository to access conversion rates in order for Money
to implement toCurrency
. I'm pretty sure DDD says not to call Repositories in Entities or Value Objects so I'm struggling to figure out how to achieve this. I was thinking of using a Domain Service to actually do the currency conversion and Money
would just call the Domain Service, however, I'm not sure if DDD considers this a good option either. Maybe I need to inject the Domain Service into each Aggregate Root (SalesOrder
, Invoice
and Shipment
) so I can inject that into each Money value object so it can in turn do the currency conversion. What do you think? Thank you in advance.