Guidance For Implementing With A Database
Example of a web application with the following particular requirements (see the example application for further illustration):
BaseText | Locale | Translation |
---|---|---|
Fish And Chips | en | Fish And Chips |
Fish And Chips | fr | Poisson et Frites |
delete | en | delete |
delete | fr | supprimer |
add.edit.button | en | Add/Edit |
add.edit.button | fr | Ajouter/Changer |
Only the last two rows use a "coder key". The BaseText column holds either the coder key, or the user-presentable English. The BaseText is the lookup key. The fact that there is repetition of data between the BaseText and English columns is not a real duplication problem, since this is a ResultSet, not a table - the underlying tables will not have such repetition (if designed properly, of course).
For example, such a ResultSet can be constructed from three underlying tables - BaseText, Locale, and Translation. Translation is a cross-reference table, with foreign keys to both BaseText and Locale. When such a scheme is normalized, it will have no repeated data. (In underlying queries, however, the base language will be necessarily treated differently than the other languages.)
Upon startup, the tables are read, the above ResultSet is created and stored in a private static Map, represented schematically as Map[BaseText, Map[Locale, Translation]]. When a translation is required, this Map is used as an in-memory lookup table. This avoids repeated fetching from the database of trivial data that rarely changes.
Note on Thread Safety
In the suggested implementation style, the private static Map that stores translation data is static, and thus shared by multiple threads. Implementations are free to implement any desired policy regarding thread safety, from relaxed to strict. A relaxed implementation might completely ignore the possibility of a mistaken read/write, since no writes are expected in production, or simply because the consequences are usually trivial. A strict implementation would take the opposite approach, and demand that the private static Map be used in an fully thread-safe manner.
|
|
|
|
|
|