简体   繁体   中英

Fluent NHibernate mapping tables with similar properties but no inheritance hierarchy

I'm having problems developing a sane set of Fluent NHibernate mappings for the following model structure:

public class BaseLookup
{
    public virtual int Id {get; set;}
    public virtual string Code {get; set;}
    public virtual string Value {get; set;}
    public virtual bool Active {get; set;}
}
public class Subdivision : BaseLookup { }
public class AvisCode : BaseLookup { }
public class District : BaseLookup { }
/*etc.*/

These lookups all share properties, but otherwise have no relationship to each other. These tables have special semantic meanings for reports and will be referenced specifically in stored procedures, so I don't wish to mash them into a common 'lookups' table that would require me to use a discriminator. That seems to eliminate Table-per-Hierarchy and Table-per-Sublass strategies in my mappings. I'm also having difficulty employing Table-per-Concrete-Class because each lookup has its own identity column - I do not want to have to assign an Id manually in the application, and there's no requirement for Id's to be unique across all these tables.

My mappings, at the moment, look like this, and are identical for each superclass of BaseLookup:

public class AvisCodeMap : ClassMap<AvisCode>
{
    public AvisCodeMap()
    {
        Schema(Schemas.pva.ToString());
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Code).Not.Nullable();
        Map(x => x.Value).Not.Nullable();
    }
}

Is there no mapping convention that allows me to extract the repetitive mappings to a block of re-usable code?

If I understand your question right, you might need to create a generic base class map then reuse it in derived maps.

Example as follows:

public class BaseLookupMap<T> : ClassMap<T> where T : BaseLookup
{
     public BaseLookupMap()
     {
        // ... base mapping code goes here ...
     }
}

To create that base type:

public class BaseLookupMap : BaseLookupMap<BaseLookup>
{  
}

And the derived class map:

public class AvisCodeMap : BaseLookupMap<AvisCode>
{
    public AvisCodeMap()
    {
        Polymorphism.Explicit();

        // ... your other mappings here, if needed ...
    }
}

HTH.

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