I normally use config (xml) files for my EPDM add-in configuration, however, there are a couple of issues with them.
- One needs to remember to add the configuration file when adding the add-in.
- Security is at risk as passwords are readable by anyone who knows where to look for them if they are stored in the configuration file. In the past I have included an encryption utility to get around this but it’s another step to take.
With this in mind I began to think of another method of storing add-in settings and decided upon IEdmDictionary5. The IEdmDictionary5 interface provides methods to store key-value pairs in the vault database. The advantage of using the database is that it can be updated at anytime and all a user needs to do to see the new configuration is log out and log back in again. There is no configuration file to change.
So how does one configure and edit a dictionary?
EPDM Dictionary Editor
I wrote this small app to edit dictionaries in the vault.
It’s pretty simple. Login to the vault, enter your dictionary name, and press load. It will load up the stored values. To save them, press save.
The ‘From File’ button will allow you to select your .dll containing your configuration class. Once you’ve selected the file, choose the appropriate class from the drop down menu and press the
’Get properties’ button to load the classes properties into the grid.
Using Reflection to Access Properties
The bonus of using a configuration class is that one can use reflection to set and get settings pretty easily.
public class Configuration { /// /// The username to connect to the OpenERP database with. /// public string OpenERPUsername { get; set; } /// /// The password to connect to the OpenERP database with. /// public string OpenERPPassword { get; set; } /// /// The OpenERP database to connect to /// public string OpenERPDatabase { get; set; } /// /// The OpenERP host/server hosting the database. IP or DNS name. /// public string OpenERPHost { get; set; } /// /// The OpenERP host/server port. /// public int OpenERPHostPort { get; set; } public bool LoadConfiguration(EdmLib.IEdmVault5 vault) { // load the assembly name from this dll Assembly assy = Assembly.GetExecutingAssembly(); string assemblyName = assy.GetName().Name; EdmLib.IEdmDictionary5 dictionary = vault.GetDictionary(assemblyName, false); if (dictionary != null) { //get all the properties of the configuration class PropertyInfo[] props = this.GetType().GetProperties(); foreach (var property in props) { string value; try { if (dictionary.StringGetAt(property.Name, out value)) property.SetValue(this, Convert.ChangeType(value, property.PropertyType), null); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show( String.Format("There was an error setting the property {0}.\r\n\r\n{1}", property.Name, ex.Message), "Property Error!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); return false; } } return true; } return false; } }
Now all one needs to load up the configuration are a couple of lines of code:
Configuration config = new Configuration(); bool configLoaded = config.LoadConfiguration(vault);
This should be straight forward and hopefully someone finds it useful!