Wednesday, May 20, 2009

How to persist application config data in a database

Suppose we have some Application wide configuration data that needs to be editable by users. For example:

RBA Interest Rate = 3.75%;
Email Trigger = $50,000 or 20%;


How can this information be stored in the database? It doesn't really relate to any entity in particular, it is just application-wide config data. Here's one way I've found of doing it - using an enum and a persistable entity class.


public enum ConfigParameterEnum {
RBA_INTEREST_RATE,
EMAIL_TRIGGER
}

@Entity
public class ConfigParameter extends PersistableEntity {

private ConfigParameterEnum configParameterEnum;
private Class clazz;
private String stringValue;

...getters and setters...
}


This way we still get the benefit of defined parameter names and also typed data, so the "email trigger" can be stored as either a value or a percentage.

It will be easy for our Java code to find a persisted configuration parameter by name using the enum, and it will be easy to start using it thanks to the Class stored with it in the database.

0 comments: