views:

244

answers:

0

Hi all

I am struggling a little in getting my mapping right.

What I have is a single self joined table of look up values of certain types. Each lookup can have a parent, which can be of a different type.

For simplicities sake lets take the Country and State example.

So the lookup table would look like this:

Lookups Id Key Value LookupType ParentId - self joining to Id

base class

public class Lookup : BaseEntity { public Lookup() {}

    public Lookup(string key, string value)
    {
        Key = key;
        Value = value;
    }

    public virtual Lookup Parent { get; set; }

    [DomainSignature]
    [NotNullNotEmpty]
    public virtual LookupType LookupType { get; set; }

    [NotNullNotEmpty]
    public virtual string Key { get; set; }

    [NotNullNotEmpty]
    public virtual string Value { get; set; }
}

The lookup map

public class LookupMap : IAutoMappingOverride<DBLookup>
    {
        public void Override(AutoMapping<Lookup> map)
        {
            map.Table("Lookups");
            map.References(x => x.Parent, "ParentId").ForeignKey("Id");
            map.DiscriminateSubClassesOnColumn<string>("LookupType").CustomType(typeof(LookupType));

        }
    }

BASE SubClass map for subclasses

public class BaseLookupMap : SubclassMap where T : DBLookup {

    protected BaseLookupMap()
    {
    }

    protected BaseLookupMap(LookupType lookupType) 
    {
        DiscriminatorValue(lookupType); 
        Table("Lookups");
    } 
}

Example subclass map

public class StateMap : BaseLookupMap<State>
    {
        protected StateMap() : base(LookupType.State) { }
    }

Now I've almost got my mappings set, however the mapping is still expecting a table-per-class setup, so is expecting a 'State' table to exist with a reference to the states Id in the Lookup table.

I hope this makes sense.

This doesn't seem like an uncommon approach when wanting to keep lookup-type values configurable.

Thanks in advance.

Al