On Mon, 22 Mar 2010 03:41:04 +0100, moebius206 <nwe...@gmail.com> wrote:

public class User
{
    public virtual IDictionary<string, object> Preferences { get;
set; }
}
with its usage as:

user.Preferences["preference1"] = "some value";
user.Preferences["preference2"] = 10;
user.Preferences["preference3"] = true;

...

Maybe there's a better way to do this, or maybe I should just suck it
up and use IDictionary<string, string>. Any ideas?


Yes, let NHibernate deal with strings to and from the database and do the conversion yourself. Since you're already storing the value's System.Type, it's easy.

Map the dictionary as a protected property expose its values through methods like GetPreference<T>(key) and SetPreference<T>(key, value) so you can work with the values in a typed way in your application.

The code inside GetPreference would look a little something like this:

   TypeConverter converter = TypeDescriptor.GetConverter(ValueType);
   return (T) converter.ConvertFromString(StringValue);

And in SetPreference() you would basically do the opposite (with ConvertToString() instead).

Another option is to have a custom implementation of IDictionary<string, string> with the same logic exposed within it (instead of in the User class). Either way, the code to store and retrieve values would be the same.

Depending on your needs, you might want to store XML instead of "dumb strings" in your database, and then you would do XML serialization and deserialization in the Get and Set methods instead. The basic idea is still the same. If all you want to store is simple types, ConvertFromString and ConvertToString should suit your needs just fine.

--
Asbjørn Ulsberg         -=|=-          asbj...@ulsberg.no
«He's a loathsome offensive brute, yet I can't look away»

--
You received this message because you are subscribed to the Google Groups "Fluent 
NHibernate" group.
To post to this group, send email to fluent-nhibern...@googlegroups.com.
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.

Reply via email to