Results 1 to 5 of 5

Thread: How to best mapping for List of Value (Select)

Hybrid View

  1. #1

    Default How to best mapping for List of Value (Select)

    Which is best strategy to map service table used in html select component?
    - String
    - enum
    - reference to Object

    I'm going to explein:

    My root object is Customer
    is has some property the user can choose using a html select component,
    the only relevant of theese properties is its LABEL.
    some example could be:
    - service type (possible values: Gold; Silver; Platinum)
    - company type (possible values: ltd, simple, private, other)
    When the user choose theese options the the Customer will be saved.
    Now serviceType and companyType could be a String, enum, or refernece to Objects (ServiceType, CompanyType) saved on a DB on its own table;
    This last option is useful if you want change name to one pre-existing type say (Gold change in GOLD), this changes is very simple changing only one record on a Service_Type DB table, while it's expensive when retriving object from database.
    Converse adventage for String mapping.

  2. #2

    Default

    Hi anyone respond me?

    is the question hard to understand or to easy to respond?

    Please any suggestion on how to choose rigth strategy.

  3. #3
    Join Date
    Nov 2006
    Location
    Boston, MA
    Posts
    303

    Default

    Your question is worded in a way that is somewhat difficult to follow. If I understand you correctly, you have a select box in your HTML form that displays a list of values of a particular property, e.g. "service type" that may be [gold, silver, platinum]. In such cases, I would use enums. Don't forget that you can add various properties for a single enum constant, like "value", "description", etc. So your ServiceType enum may look like

    Code:
    public enum ServiceType  {
        
        SILVER(1, "Silver", "Silver description here"),
        GOLD(2, "Gold", "gold description here"),
        PLATINUM(3, "Platinum", "plat description here");
    
        private final int value;
        private final String name;
        private final String description;
       
        ServiceType(int val, String nameStr, String desc) {
            this.value = val;
            this.name = nameStr;
            this.description = desc;
        }
    
        
        public int getValue() {
            return value;
        }
    
        
        public String getName() {
            return name;
        }
        
       public String getDescription() {
            return description;
        }
    }
    You can add more useful things, like abbreviated codes, if those things may be useful to the applications in different contexts. You can also consider creating reversible enums - if your codes are stored as ints in the database and need to be translated into enums on the application side. (You'd have to read up on that yourself, though.) Then you can choose, which property/presentation of the same enum to display in the select box, which to save in the database, which to use elsewhere, etc.

    Not sure if that was exactly your question, but hope this helps.

  4. #4

    Default

    Yes, my english is very bad, even worst when my thougth are not clean.
    in any case you got partially the question.

    The question focus is:
    In that cases is it best to store on the DB the string itself, or some reference to object?

    With first option showing data does not require any translation and can be directly displayed, but has the following cons: if you require to change that string, you have to make an update on all already stored data, and secondly if you need to add one more option later you need to modify the project sources.

    Converse pros and cons for second option,

    now what are you opinion and you practices?

    Regards

  5. #5
    Join Date
    Nov 2006
    Location
    Boston, MA
    Posts
    303

    Default

    Ok, this sounds like a very basic I18N issue: how to store and manage text strings that are meant for presentation purposes. Correct? Do not hard-code or store string constants directly. Use the constants/codes/enums (whatever most appropriate in each case) that represent them, and resolve these constants to the appropriate text strings from a message resource - on the front-end. Read about I18N, message resources, etc.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •