-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Mapping to a SortedList in the .NET Framework Class Library
PostPosted: Sun Nov 18, 2007 12:43 pm 
Newbie

Joined: Fri Nov 09, 2007 10:01 am
Posts: 15
Hello NHibernate-users

I would like to map to a SortedList in the .NET Framework Class Library, but I can’t find useful information concerning this matter.

A Group has multiple Categories. But the ordering of the Category-items within a Group is not based on a property of the Category business entity. No, a column in the Categories-table (Cat_PositionInGrp) maintains the key for ordering the Categories-items within a Group. As a result, I decided to use a SortedList(Of Integer, bsCategory). The user should be allowed to change this ordering at all time.

Herewith the code of both the Group- and Category business entity classes:
Code:
Public Class bsGroup
    Private mId As Integer
    Private mName As String
    Private mCategories As SortedList(Of Integer, bsCategory)

    Public Overridable Property Id() As Integer
        Get
            Return mId
        End Get
        Set(ByVal value As Integer)
            mId = value
        End Set
    End Property
    Public Overridable Property Name() As String
        Get
            Return mName
        End Get
        Set(ByVal value As String)
            mName = value
        End Set
    End Property
    Public Overridable Property Categories() As SortedList(Of Integer, bsCategory)
        Get
            Return mCategories
        End Get
        Set(ByVal value As SortedList(Of Integer, bsCategory))
            mCategories = value
        End Set
    End Property
#End Region
End Class


Code:
Public Class bsCategory
    Private mId As Integer
    Private mName As String

    Public Overridable Property Id() As Integer
        Get
            Return mId
        End Get
        Set(ByVal value As Integer)
            mId = value
        End Set
    End Property
    Public Overridable Property Name() As String
        Get
            Return mName
        End Get
        Set(ByVal value As String)
            mName = value
        End Set
    End Property
End Class


A brief overview of the necessary tables and its columns in SQL Server 2005:
Code:
CREATE TABLE [dbo].[TBL_GROUPS](
   [Grp_Id] [int] IDENTITY(1,1) NOT NULL,
   [Grp_Name] [nvarchar](50) NOT NULL,

CREATE TABLE [dbo].[TBL_CATEGORIES](
   [Cat_Id] [int] IDENTITY(1,1) NOT NULL,
   [Cat_Grp_Id] [int] NOT NULL,
   [Cat_PositionInGrp] [int] NOT NULL,
   [Cat_Name] [nvarchar](50) NOT NULL,


I can not imagine that a .Net SortedList cannot be mapped through NHibernate. Or am I wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 18, 2007 2:02 pm 
Newbie

Joined: Fri Nov 09, 2007 10:01 am
Posts: 15
I prepared a little example to give you an idea what I want to achieve:

Image

When you iterate through the Groups and its Categories, the result should be (ordering the Groups is not necessary):

* GROUP 2
---> Category 208
---> Category 210
---> Category 205
---> Category 209
* GROUP 5
---> Category 204
---> Category 199


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 23, 2007 6:45 pm 
Newbie

Joined: Fri Nov 09, 2007 10:01 am
Posts: 15
Anyone?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 6:35 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
If your Categories property in the Group entity is mapped as a bag, then you can define the order-by column:

Code:
<class name="Group" table="TBL_GROUPS" ...>
  <bag name="Categories" inverse="true" order-by="Cat_PositionInGrp" ...>
    <key column="Cat_Grp_Id"/>
    <one-to-many class="Category"/>
  </bag>
  ...
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 06, 2007 5:57 pm 
Newbie

Joined: Fri Nov 09, 2007 10:01 am
Posts: 15
A simple but workable solution.

Thank you, Nels_P_Olsen.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 8:47 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
Nels_P_Olsen wrote:
If your Categories property in the Group entity is mapped as a bag, then you can define the order-by column:


I realize the original poster is satisfied with this answer, and I also realize that it probably works for what he or she is really trying to accomplish, but I do want to add some words of caution.

this really isn't doing the task which the original poster asked about. First, a SortedList is actually a combination of keys and values where all keys of the list are sorted and then the items are accessible via the key. This is very similar to a SortedDictionary.

Secondly, a bag technically has no order. It's not really a bag at all, it's just using a bag mapping to get through to the underlying implementation.

Third as soon as you add an item after the collection is loaded from the database your ordering is now lost. If the poster was actually using a SortedList the items would automatically be sorted correctly for every item which is added.

Personally when I develop applications I never rely on the order-by clause. Instead I would only use it as a performance improvement if it is advantageous to have a sorted collection before I manually sort it. Some algorithms are faster sorting an already sorted list.

The fact remains that I do not know the current state of an object (from my domain objects at least) regarding what came from a database and what has been modified since. For some of our operations many things could have taken place. Making assumptions like this that any given IList would be sorted would not suffice for us.

Caution should be taken in making assumptions regarding the ordering of the original posters list.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 8:15 pm 
Newbie

Joined: Fri Nov 09, 2007 10:01 am
Posts: 15
Thank you for your contribution, jchapman.

One of your sentences was a tricky thing I overlooked: "If the poster was actually using a SortedList the items would automatically be sorted correctly for every item which is added."

The best solution is to get the SortedList-thing working.

I do not know how, but in the coming days I will try to work something out to achieve exactly what I was trying to achieve.

fyi: I am a He, bennieke is just a Dutch diminutive of my real name Benny


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.