views:

212

answers:

3

Hi all,

assume a data structure Person used for a contact database. The fields of the structure should be configurable, so that users can add user defined fields to the structure and even change existing fields. So basically there should be a configuration file like

FieldNo  FieldName          DataType           DefaultValue
 0        Name               String             ""
 1        Age                Integer            "0"
 ...

The program should then load this file, manage the dynamic data structure (dynamic not in a "change during runtime" way, but in a "user can change via configuration file" way) and allow easy and type-safe access to the data fields.

I have already implemented this, storing information about each data field in a static array and storing only the changed values in the objects.

My question: Is there any pattern describing that situation? I guess that I'm not the first one running into the problem of creating a user-adjustable class?

Thanks in advance. Tell me if the question is not clear enough.

A: 

The normal way to handle this is for the class to have a list of user-defined records, each of which consists of list of user-defined fields. The configuration information forc this can easily be stored in a database table containing the a type id, field type etc, The actual data is then stored in a simple table with the data represented only as (objectid + field index)/string pairs - you convert the strings to and from the real type when you read or write the database.

anon
That's how my implementation looks like at the moment. I was interested if there are common, more elegant solutions.
Smasher
+4  A: 

I've had a quick look through "Patterns of Enterprise Application Architecture" by Martin Folwer and the Metadata Mapping pattern describes (at quick glance) what you are describing.

An excerpt...

"A Metadata Mapping allows developers to define the mappings in a simple tabular form, which can then be processed bygeneric code to carry out the details of reading, inserting and updating the data."

HTH

Nazar
Thanks for this great answer! Unfortunately the pattern description on Martin Fowler's weg page is very short and I'm not in posession of his book. Are there any free and more detailed descriptions of this pattern?
Smasher
Of course you might also just buy the book. It'll be worth the investment.
Wouter van Nifterick
@Nazar: I send you a message. Thanks a lot for this very nice offer!I will consider buying the book if I find it helpful.
Smasher
+3  A: 

I suggest looking at the various Object-Relational pattern in Martin Fowler's Patterns of Enterprise Application Architecture available here. This is a list of patterns it covers here.

The best fit to your problem appears to be metadata mapping here. There are other patterns, Mapper, etc.

RS Conley
+1 same goes for you: great answer!
Smasher