title: How to map IDictionary<string, object> in Fluent NHibernate?
body: <p>I am looking to persist user preferences into a collection of name value pairs, where the value may be an int, bool, or string.</p>
  
  <p>There are a few ways to skin this cat, but the most convenient method I can think of is something like this:</p>
  
  <pre><code>public class User
  {
      public virtual IDictionary&lt;string, object&gt; Preferences { get; set; }
  }
  </code></pre>
  
  <p>with its usage as:</p>
  
  <pre><code>user.Preferences["preference1"] = "some value";
  user.Preferences["preference2"] = 10;
  user.Preferences["preference3"] = true;
  
  var pref = (int)user.Preferences["preference2"];
  </code></pre>
  
  <p>I'm not sure how to map this in Fluent NHibernate, though I do think it is possible.  </p>
  
  <p>Generally, you would map a simpler Dictionary&lt;string, string&gt; as:</p>
  
  <pre><code>HasMany(x =&gt; x.Preferences)
      .Table("Preferences")
      .AsMap("preferenceName")
      .Element("preferenceValue");
  </code></pre>
  
  <p>But with a type of 'object', NHibernate doesn't know how to deal with it.  I imagine a custom UserType could be created that breaks an 'object' down to a string representing its Type and a string representing the value.  We would have a table that looks kind of like this:</p>
  
  <pre><code>Table Preferences
      userId (int)
      preferenceName (varchar)
      preferenceValue (varchar)
      preferenceValueType (varchar)
  </code></pre>
  
  <p>and the hibernate mapping would like this:</p>
  
  <pre><code>&lt;map name="Preferences" table="Preferences"&gt; 
    &lt;key column="userId"&gt;&lt;/key&gt; 
    &lt;index column="preferenceName" type="String" /&gt;
    &lt;element type="ObjectAsStringUserType, Assembly"&gt;
      &lt;column name="preferenceValue" /&gt; 
      &lt;column name="preferenceValueType"/&gt; 
    &lt;/element&gt; 
  &lt;/map&gt; 
  </code></pre>
  
  <p>I'm not sure how you would map this in Fluent NHibernate.    </p>
  
  <p>Maybe there's a better way to do this, or maybe I should just suck it up and use IDictionary&lt;string, string&gt;.  Any ideas?</p>