简体   繁体   中英

Most optimal way to model a generic class in nHibernate

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)

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");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM