2

Say I have a class something like...

public class SomeClass<T> where T : ISomeConstrainingInterface
{
    public T MyPropertyOfTypeT {get;set;}
    public int SomeIntProp {get;set;}
    public string SomeStringProp {get;set;}
}

Where T can be a fairly small limited set (say 5 or 6 types)

What is the best and most efficient way to map this class in nHibernate ? (using fluentNHibernate)

Craig
  • 36,306
  • 34
  • 114
  • 197
Tim Jarvis
  • 18,465
  • 9
  • 55
  • 92
  • What are the tables named, or are you free to name them what you want? – Daniel Schilling Aug 21 '13 at 20:47
  • @DanielSchilling Yep free to model the tables as I like. I see 2 main options, a table with 2 additional cols, one to represent the type and the other with the Id. The down side to this is its a more complex join each time (a case type join). Or the other way would be to have a col for each additional type and just populate the one that is used. Not very elegant (CS teachers would cringe) but probably better performing. – Tim Jarvis Aug 21 '13 at 22:25
  • I assume `T` is an Entity, not a value type. So `SomeClass` is used to decorate a couple pieces of extra data onto an assortment of various entities, like a tag. Is that the idea? – Daniel Schilling Aug 22 '13 at 02:08
  • Yeah, that's part of it. It also will contain collections of things that are related to each other only in the context of the joined Entity. – Tim Jarvis Aug 22 '13 at 03:43

1 Answers1

0

this will create a seperate table for each type concreate of SomeClass, so there is no problem with different Id types and foreign keys are possible. SomeClass should implement an interface so all can be used be queried and handled generically.

public abstract class SomeClassMapBase<T> : ClassMap<SomeClass<T>>
{
    public SomeClassMapBase()
    {
        Map(x => x.SomeIntProp);
        Map(x => x.SomeStringProp);
    }
}

public class SomeClassReferencedClassMap : SomeClassMapBase<ReferencedClass>
{
    public SomeClassReferencedClassMap()
    {
        CompositeId()
            .KeyReference(x => x.Referenced, "Refernece_id");
    }
}
Firo
  • 30,626
  • 4
  • 55
  • 94