简体   繁体   English

NHibernate(Fluent)映射抽象了一个中间类

[英]NHibernate (Fluent) mapping abstracting an intermediate class

I have a generic object type Entity and some classes that can relate to Entity such as EntityDescription and EntityLocation (many-to-one relationships). 我有一个通用的对象类型Entity和一些可以与Entity相关的类,例如EntityDescription和EntityLocation(多对一关系)。

Entity class: 实体类:

public class Entity
{
    public virtual Int64 ID { get; set; }
    // and other properties
}

EntityDescription class: EntityDescription类:

public class EntityDescription 
{
    public virtual Int64 ID { get; set; }
    public virtual Entity Entity { get; set; }
    public virtual String Description { get; set; }
    // and other properties
}

EntityLocation class: EntityLocation类:

public class EntityLocation 
{
    public virtual Int64 ID { get; set; }
    public virtual Entity Entity { get; set; }
    public virtual String Location { get; set; }
    // and other properties
}

There are a number of more specific entity types eg Company, Supplier, Employee, etc. To make things more interesting, EntityDescription applies to all specific types, but only some types can be assigned locations (EntityLocation is only applicable to some types). 有许多更具体的实体类型,例如公司,供应商,雇员等。为了使事情变得更加有趣,EntityDescription适用于所有特定类型,但是只能为某些类型分配位置(EntityLocation仅适用于某些类型)。

How do I map these classes in Fluent-NHibernate so that EntityLocation list is only exposed on some specific classes that can have locations (eg Company and Supplier), and not on the generic Entity class? 如何在Fluent-NHibernate中映射这些类,以使EntityLocation列表仅在某些可以具有位置的特定类(例如,公司和供应商)上公开,而不在通用Entity类上公开?

// This property can exist in Entity
public virtual IList<EntityDescription> Descriptions { get; set; }

// I want this property to only exist in some specific types, not in Entity
public virtual IList<EntityLocation > Locations { get; set; }

Any help appreciated. 任何帮助表示赞赏。 Thanks in advance. 提前致谢。

I would say, that what we need is a ShouldMap setting. 我要说的是,我们需要的是ShouldMap设置。 Check this answer: 检查以下答案:

FluentNHibernate - Automapping ignore property FluentNHibernate-自动映射忽略属性

So, we should introduce this configuration with different default handling of the Locations property: 因此,我们应该以不同的Locations属性默认处理方式引入此配置:

public class AutoMapConfiguration : DefaultAutomappingConfiguration
{
    private static readonly IList<string> IgnoredMembers = 
                        new List<string> { "Locations"}; // ... could be more...

    public override bool ShouldMap(Member member)
    {
        var shouldNotMap = IgnoredMembers.Contains(member.Name);

        return base.ShouldMap(member) && !shouldNotMap;
    }
}

That will make the Locations property by default NOT mapped. 默认情况下,这将使Locations属性被映射。 Next step, is to use explicit mapping where needed... 下一步是在需要的地方使用显式映射...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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