Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>NHiberante has nice support for what you would like to get. In fact there is not only one way, there are two:</p> <p>1) mapping with the <code>&lt;list&gt;</code></p> <p>Use the list mapping which supports the <code>&lt;index&gt;</code> column. this is the same as the internal <em>index</em> in the C# used inside the <code>List&lt;T&gt;</code> object</p> <p>Small disadvantage is, that you have to keep that column to be from 0 ... including all numbers. The same as the underlying C# object. but it works</p> <p>Please see more here: <a href="http://ayende.com/blog/4043/nhibernate-mapping-list" rel="nofollow">http://ayende.com/blog/4043/nhibernate-mapping-list</a> and here for indexed collections: <a href="http://nhibernate.info/doc/nh/en/index.html#collections-ofvalues" rel="nofollow">http://nhibernate.info/doc/nh/en/index.html#collections-ofvalues</a></p> <p>2) extend the mapping with the <code>order-by="column_name asc|desc"</code> </p> <p>This allows to use some more <em>user-friendly</em> column (with some editable value) to be used for the sorting of the list during its loading</p> <p>Plase, see more here: <a href="http://nhibernate.info/doc/nh/en/index.html#collections-mapping" rel="nofollow">6.2. Mapping a Collection</a></p> <p><strong>EDIT</strong>: to follow the Question Edit</p> <p>C# <code>SortedList</code> mapping is again supported by NHibernate. Please, se the section <a href="http://nhibernate.info/doc/nh/en/index.html#collections-sorted" rel="nofollow">6.6. Sorted Collections</a></p> <p>Mapping should look like this</p> <pre><code>&lt;map name="Controls" order-by="SlotOrder" lazy="true" cascade="all-delete-orphan"&gt; &lt;key column="PageConfigurationId" /&gt; &lt;index column="SlotOrder" type="System.Int32"/&gt; &lt;one-to-many class="WidgetConfiguration"/&gt; &lt;/map&gt; </code></pre> <p>the <code>SlotOrder</code> is now managed by the Parent. It plays the role of the key of the <em>Controls</em> <code>SortedList</code>. So we should change its mapping to be readonly <em>(only one editing point in time should exist)</em></p> <pre><code>&lt;class name="WidgetConfiguration" ...&gt; ... &lt;property name="SlotOrder" type="System.Int32" insert="false" update="false" /&gt; </code></pre> <p>Having this mapping, we can add new <em>item</em> into <code>Controls</code> collection:</p> <pre><code>WidgetConfiguration config = ...; PageConfiguration pageConfig = ...; pageConfig.Controls[config.SlotOrder] = config; session.Update(pageConfig); </code></pre> <p>So, </p> <ol> <li>we have mapped the <em>SortedList</em> (in fact interface <code>IDictionary&lt;,&gt;</code>), </li> <li>the <code>SlotOrder</code> is used as a key <em>(inserted, updated by NHibernate)</em> and </li> <li>available <em>(as readonly)</em> on the WidgetConfiguration</li> </ol> <p>That should answer the question about sorting and keys of dictionaries... please, let me know if I did miss something...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload