views:

699

answers:

1

Domain:

class Category   
   string Name 
class Product   
   IDictionary<string, Product> Parents

Tables:

   Categories (ID, Name) 
   Products (ID)
   ProductParents (ID, ParentID, ChildID, CategoryID)

The questions: I need to get list of parent products. Is it possible to map parent products to dictionary so that I can do: product.Parents["CategoryName"] and it will give me a list of parent products for the given category.

Or maybe

product.Parents[Category("CategoryName")]

if I use

IDictionary<Category, Product> Parents

Or how do I do this? Maybe a method like product.GetParentProducts(string category) instead of properties? I'd rather use dictionary, though.

Notice that I don't really want to introduce ProductMapping class.

+1  A: 

This is possible with NHibernate; what you're looking for is called a ternary association.

This is currently not available in Fluent NHibernate, but I'm working to add it. However, it probably won't be available for some time yet, due to the "replumbing" going on in FNH. Your best bet might be to use a standard HBM file for this mapping.

If you want to investigate further into doing it in FNH (note: you'll have to maintain your own local copy of the library), you can see what I've done at github.

Stuart Childs
Thanks for pointing out. However as far as I can see this IS available, fixed as part of http://code.google.com/p/fluent-nhibernate/issues/detail?id=226, and is integrated in http://code.google.com/p/fluent-nhibernate/source/detail?r=524. I'm going to get and test the latest revision.
queen3
Yes, however it is my understanding that the above (which I implemented) is useless... http://code.google.com/p/fluent-nhibernate/issues/detail?id=230 I wasn't really thinking when I implemented it, so while it's possible to generate a correct *looking* mapping with that patch, it won't actually work for NHibernate. NH expects an IDictionary which the original patch doesn't support (see issue 230). I created a hack patch which might work for you, but again it's not in the FNH trunk so you're back to maintaining your own FNH copy.
Stuart Childs
I tried your hack, it did not work for me. I get "expected 2 generic arguments but got 1". This may be because I misuse the feature, though, since I'm still not expert. So I continue to use an intermediate entity (which is pretty fine to do) and I think I'll wait for an "official" fix and hopefully a bit of documentation... ;-)
queen3
I've already committed the official fix to the main FNH repository on github. If you're willing to use the "beta" version, checkout the model branch which has the functionality you're wanting. Alternatively, you can wait a week or two since we should be releasing 1.0 around that time.
Stuart Childs