views:

25

answers:

2

In NHibernate manual I have found mapping like this:

<bag name="Sizes" table="SIZES" order-by="SIZE ASC">
    <key column="OWNER"/>
    <element column="SIZE" type="Int32"/>
</bag>

I can't help wondering why would anyone want to do something like this? Is there something better about mapping plain integers than creating an entity corresponding to a given integer (Size entity in this scenario) and creating true one-to-many relationship?

+1  A: 

The question is not if you want to map it like this, the question is, if you need a list of ints in your model.

When you have a list of ints in your model, then you want to map it like this. You don't want to write complex code in your model, just because of the mapping.

So, do you think it is useful to have a list of ints in a class? Or a list of Guids, enums, doubles?

class Graph
{
  IList<double> Values { get; private set; }
}

Doesn't it make sense to you?

Stefan Steinegger
+1  A: 

The requirements of your business domain that your application operates in will dictate whether you should have a list of ints (a collection of value types), or a list of entities. If there's a good use case for a IList<int> then by all means go for it, and let NHibernate map this association accordingly. Otherwise just remove it.

However, removing it because it seems unfamiliar to you, isn't a valid reason.

I use this a lot in my domain models. Right now I have a lot of 'Twitter Applications' that index tweets based on search 'Keywords', so I have mapped it like this:

public class TwitterApplication
{
    public virtual int Id { get; set; }
    public virtual string ApplicationName { get; set; }

    // other properties (snip)

    public virtual ISet<string> Keywords { get; set; }
}

I use this mapping because I know that:

  1. The number of Keywords will be small (about 4 - 6)
  2. I'm not interested in storing Keyword DateAdded etc.
  3. I'm not going to be doing Paging or Querying on the Keywords, just retrieve them all at the same time, or not at all

On this basis, I decided mapping it as a collection of strings was appropriate.

Sunday Ironfoot
Is there an advantage for using ISet<T> instead of IList<T> ?
Rafael Belliard
@Rafael they have different semantics. A Set is an unordered collection of unique items. A List is an ordered collection of non-unique items.
Diego Mijelshon

related questions