带有自动映射的映射接口

mapping interface with automap

我无法使接口映射工作,知道该怎么做吗?

public class Event 
{
    public virtual int EventId { get; set; }
    public virtual string Name { get; set; }
    public virtual ENEventNodeType EventType { get; set; }
    public virtual IEventNode EventNode { get; set; }
}

public interface IEventNode {}

public enum ENEventNodeType : short
{
    [Description("Mission")]
    Mission = 1,
    [Description("Task")]
    Task = 2,
    [Description("Quiz")]
    Quiz = 3
}

public class Mission : IEventNode
{
    public virtual int MissionId { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Event> EventList { get; set; }   
}

public class Task : IEventNode
{
    public virtual int TaskId { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Event> EventList { get; set; }    
}

我为什么这样做: IEventNode 可能有 20 多个不同 class,而 Event 只能有一个。所以其他 19+ 字段将始终为空。 我试图在同一字段上设置 Id,EventNodeType 显示字段中有哪个 class 的 id。欢迎选择

解决方案 1) AutoMappingOverride

public class EventMappingOverride : IAutoMappingOverride<Event>
{
    public void Override(AutoMapping<Event> mapping)
    {
        var any = mapping.ReferencesAny(x => x.EventNode)
            .EntityIdentifierColumn("EventNodeId")
            .EntityTypeColumn("EventType")
            .IdentityType<int>()
            .MetaType<int>();

        foreach (var eventNodeType in typeof(Event).Assembly.GetExportedTypes().Where(typeof(IEventNode).IsAssignableFrom))
        {
            if (!eventNodeType.IsInterface && !eventNodeType.IsAbstract)
                any.AddMetaValue(eventNodeType, eventNodeType.Name);
        }
    }
}
public class Event
{
    public virtual int EventId { get; set; }
    public virtual string Name { get; set; }
    public virtual IEventNode EventNode { get; set; }
}

public interface IEventNode { }

// example using FluentNHibernate Mapping
public class EventMap : ClassMap<Event>
{
    public EventMap()
    {
        Id(x => x.EventId);

        Map(x => x.Name);
        var any = ReferencesAny(x => x.EventNode)
            .EntityIdentifierColumn("eventNode_id")
            .EntityTypeColumn("EventType")
            .IdentityType<int>()
            .MetaType<int>();

        foreach (var eventNodeType in   typeof(Event).Assembly.GetExportedTypes().Where(typeof(IEventNode).IsAssignableFrom))
        {
            if (!eventNodeType.IsInterface && !eventNodeType.IsAbstract)
                any.AddMetaValue(eventNodeType, eventNodeType.Name);
        }
    }
}

然后做

if (event.EventNode is Mission)
    Write("Hey its a mission");
...