views:

350

answers:

2

I have a dynamic class that serves as a storage container for configuration settings. The settings are variables of that class and it has methods to read from and write to a configuration file, database etc. Now I want to trigger writing to the persistant storage whenever a class variable is changed. As the variables are added dynamically at runtime, I can't use get/set methods, also, if I could, that would lead to a lot of boilerplate code.

How can I have an event triggered for changing properties of my class?

+2  A: 

You can subclass this class of yours from mx.utils.Proxy. It allows you to have the object be dynamic yet still be able to write some custom code that runs whenever properties are accessed (similarly to getters and setters -- see getProperty() and setProperty().)

hasseg
A: 

I would consider avoiding the dynamic object, and rather create the data storage class (SettingsModel?) with one method for storing and one method for retrieving data. So instead of using:

configurationSettings.randomSetting = value;

...you would write:

configurationSettings.store("randomSetting", value);

You could store the named settings internally in a Dictionary, and do whatever magic needs to be done in the store() method.

The main advantage here is readability: when you see a method being called, you know there's functionality behind it. For the same reason I tend to dislike getter/setter functions.

In the end it is a matter of taste, so if your code isn't broken, why fix it. :)

Niko Nyman