How do i create a "pseudo" References() map?

2 views
Skip to first unread message

cliff vaughn

unread,
Dec 17, 2009, 4:21:03 PM12/17/09
to fluent-nhibernate
Hello all,

I've been playing around with FNH and i'm having difficulty with a mapping.  Suppose I have the following classes:

class Parent
{

  public   IList<Item> Items{get;set;}
  public Item LastItem{get;set;}
  public Item FavoriteItem {get;set;}
}

class Item
{
     public string Name {get;set;}
     public bool IsFavorite{get;set;}
}

Now, I know how to map Items using HasMany, and I know how to map LastItem and FavoriteItem using References if i add a LastItemId and a FavoriteItemId to the Parent table.  Is there a way to get FNH to map those properties without polluting Parent with extra columns.  I've thought about the following:

class Parent
{

  public   IList<Item> Items{get;set;}
  public Item LastItem{get{return Items.LastOrDefault();}}
  public Item FavoriteItem {get{return FavoriteItems.FirstOrDefault();}}

  private IList<Item> FavoriteItems {get;set;}
}

and using a Where for the FavoriteItems mapping, but this doesn't "feel" right to me.  Also, now my lazy loaded Items collection with hundreds of items is loaded and I don't want or need the performance hit.

Can FNH save me?

--
thanks

cliff

Paul Batum

unread,
Dec 20, 2009, 12:17:25 AM12/20/09
to fluent-nhibernate
Hi Cliff,

What schema do you want for this? You mention you don't want LastItemId and FavoriteItemId columns on the Parent table, so does that mean you would prefer IsLastItem and IsFavouriteItem flags on the Item table? Or do you want to use a many to many relationship and create a ParentItem table, a FavouriteItem table and a LastItem table (each with a ParentID and a ItemID)? You have to decide on a schema before we can assist you with the mapping.

Paul Batum

--

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

cliff vaughn

unread,
Dec 20, 2009, 8:47:25 AM12/20/09
to fluent-nhibernate
Paul,

well, ideally, i'd like to have an IsFavouriteItem on the Item table, and instead of the IsLastItem, i'd like a way to just get the last item in the list by date.  Does that help?

cliff
--
thanks

cliff

Paul Batum

unread,
Dec 20, 2009, 4:14:27 PM12/20/09
to fluent-nhibernate
The problem with a IsFavouriteItem flag on the item table, is that relationally, there's no way to enforce that only ONE item for a particular parent has that flag set. As a result, there isn't a straightforward way to map that to a single Item reference (as far as I know). I think you'd have to implement it as you've suggested, with a private list of favourite items, which should always have a maximum of one item. It would be mapped with a where clause.

As for the LastItem, I think you could do something similar again. A where clause should let you filter the items so only the last item is returned. Again it would be stored in a list of max length 1. At least this way, you don't have to touch the Items list and force it to be evaluated.

I expect that there is a way to avoid having these private length-1 lists, but it would involve using some of NH's lower level hooks, such as a custom Tuplizer.

And of course, as I mentioned in the previous email, a different schema would give you different mapping options.
Reply all
Reply to author
Forward
0 new messages