Static Data

December 17th, 2007
by Daniel

Static Domain Model Data
Many design considerations take into account language type for translations. It’s really nice to store the translations for each object directly in the object itself. I like to keep Map of text keyed by LanguageType for easy retreival. The LanguageTypes themselves are also stored directly in the database but are considered static data and part of the schema itself, if only to maintain your data’s referential integrity. While it may seem redundant to keep static data in both a database table and a domain object, this will help enforce a covariant state between your static data and your framework code that will force you to update one when you update the other and keep bad data a minimum. Typically languages aren’t added dynamically and you have a decent target set in mind before jumping in to a project.

DB Table: LanguageType
languageTypeId | languageType
1              | English
2              | Spanish
3              | French
4              | German

I usually keep static domain data in an enum. Java enum’s do a lot of things, such as keep the ordinal of each enum and index it internally. I don’t like to rely on the internal ordinal of each value though, I like to keep the identifier value of each enum as a part of the enum itself.

public enum LanguageType {
    English(1, "English"),
    Spanish(2, "Spanish"),
    French(3, "French"),
    German(4, "German"),
    ;

    private int value;
    private String label;

    private LanguageType(int value, String label) {
        this.value = value;
        this.label = label;
    }

    public int getValue() {
        return value;
    }

    public String getLabel() {
        return label;
    }

    public static LanguageType getLanguageType(int value) {
        for (LanguageType languageType : LanguageType.values()) {
            if (languageType.getValue() == value) {
                return languageType;
            }
        }
        throw new IllegalArgumentException("LanguageType value not found: " + value);
    }

}

Comments (0)

No comments yet

Leave a Reply

Spam Protection by WP-SpamFree

Daniel Blaisdell is Digg proof thanks to caching by WP Super Cache