0

I need some help with a value object. Consider the following example.

I have entity Card which has pan currency(USD, EUR, GBP) and card type (Visa, Visa Electron, Mastercard). I think currencies and card types are value objects and they are stored in the source code. According to DDD value objects don't have repositories.

  1. The question is how to fill the dropdowns of currencies and a card types in the interface when creating new Card entity.
  2. Where to put that classes in Application, Domain or Infrastructure layer and how to retrieve data from them.

A simple example would be priceless"

UPDATE1

That was my first approach but.. I use currencies in other entities like MoneyTransfer there i use EUR USD GBP and a few more so i have to create one more enum

public enum MoneyTransferCurrency{
    USD, EUR, GBP and a few other
}

This way i duplicate some currencies and if we stop processing payments in USD i have to find all enums and delete the USD currency.

Stasa
  • 115
  • 2
  • 13
  • http://stackoverflow.com/questions/679005/how-are-value-objects-stored-in-the-database – QuietNaN Nov 03 '16 at 12:45
  • What means "stop processing payments in USD"? It means that for your application USD is not used anymore for new transactions, or that everything that has USD should be deleted? What should happen to your stored data when you stop using USD? It seems to me more a validation problem than domain modelling. Think about it from your domain perspective. – rascio Nov 03 '16 at 15:02
  • Means that you can't order card in USD or you can't send money transfer in USD. Nothing should be deleted. – Stasa Nov 03 '16 at 15:05
  • So USD still exists as a possible value in your domain, you don't have to delete it from the enum. – rascio Nov 03 '16 at 15:06
  • I have to remove it from user interface dropdowns but i won't delete purchased cards or transfers in USD – Stasa Nov 03 '16 at 15:09
  • so you can have a static function, to return just the valid values. This static function is a query in CQRS terms. – rascio Nov 03 '16 at 16:02
  • Yes! That's the answer of my troubles. Yesterday i read these two questions http://stackoverflow.com/questions/5478253/loading-a-value-object-in-list-or-dropdownlist-ddd/5487793#5487793 http://stackoverflow.com/questions/4921899/simple-aggregate-root-and-repository-question/4931457#4931457 add them to your answer and i'll accept it. Thank you for your time. – Stasa Nov 04 '16 at 07:11
  • Links was added ;) You're welcome! – rascio Nov 04 '16 at 08:14

3 Answers3

1

You misunderstood VOs, read some more info about them, i think easy way to understand them is to look up Person / Addres example.

What about your case, just create 3 tables, don't try to make simple things complicated:

Card (CardID, CurrencyID, CardTypeID)  
Currency (CurrencyID, CurrencyName)  
CardType (CardTypeID, CardTypeName)

For DropDowns you will just query Currency & CardType tables. And in future if you will add new Currency or CardType it will be easy to do.

QuietNaN
  • 371
  • 1
  • 14
1

Don't try to make simple things complicated.

If you know a-priori all the possible values of these VO, you can have them as enum.

public enum Currency{
    USD, EUR, GBP
}

And then from the code:

Currency.values()

Ask yourself, how often these values changes? (how often a new kind of currency is used by the system?)
Is the cost of developing and maintaining a system (like db tables) to add remove these values dynamically without restart the application less than using an enum and recompile the project when there is a change?

A lot of times I was critizied for this solution with arguments like "I need a description of the value", like in a view writing "Euro" when you have the EUR value, if you have a table you can have a description column.
This can be easily solved using a properties file like:

labels.currency.EUR=Euro
labels.currency.USD=Dollars

Check also this:
- Loading a Value object in List or DropdownList, DDD
- Simple aggregate root and repository question

Community
  • 1
  • 1
rascio
  • 8,968
  • 19
  • 68
  • 108
0

Currency and CardType aren't necessarily sensible value objects. Value objects model a conceptual whole containing related attributes. Value objects can be compared by comparing all of their attributes.

Think for example of an MonetaryValue value object that consists of a numerical value together with a currency:

public class MonetaryValue {

    private BigDecimal value;
    private Currency currency;

    public Amount(BigDecimal value, Currency currency) {
        // validations:
        assertNotNull(value);
        assertNotNull(currency);

        this.value = value;
        this.currency = currency;
    }
    [...]
}

Value (e.g. 100) and currency (e.g. eur) together describe the worth of a thing. The thing isn't worth just "100" and not just "eur", but "100 eur".

Comparing of value objects is also important. When talking about monetary values, both attributes, value and currency, need to be compared. Comparing the value without the currency wouldn't help much, since 100 usd isn't the same as 100 eur. This is a further indication that MonetaryValue could be a sensible value object.

Markus Pscheidt
  • 6,853
  • 5
  • 55
  • 76