简体   繁体   English

C#Fluent NHibernate概述映射

[英]C# Fluent NHibernate Overview Mapping

I have three classes: 我有三节课:

public class BaseEntity
{
    public int Id { get; set; }
}

public class DocumentOverview : BaseEntity
{
    public string Name { get; set; }
}

public class Document : DocumentOverview
{
    public byte[] Data{ get; set; }
}

In my application there is a tree and I want to load document names and list them there. 在我的应用程序中有一棵树,我想加载文档名称并在其中列出它们。 Only when an TreeItem is selected I'm loading the Document entity to save traffic. 仅当选择TreeItem时,我才加载Document实体以节省流量。

I don't want to have redundant code in my mapping , so my first attempt was like this: 我不想在映射中有多余的代码 ,所以我的第一次尝试是这样的:

public class DocumentOverviewMaps<T> : ClassMap<T> where T : DocumentOverview
{
    public DocumentOverviewMaps()
    {
        Table("Documents");
        Id(x => x.Id);
        Map(x => x.Name).Not.Nullable();
    }
}

public class DocumentMaps : DocumentOverviewMaps<Document>
{
    public DocumentMaps()
    {
        Map(x => x.Data).CustomType<BinaryBlobType>().Nullable();
    }
}

This does not work I always get the big Document entity even when loading overviews. 这是行不通的,即使加载概述,我也总是得到大型Document实体。

I found out that you can use SubclassMap<> but that doesn't work because it is for loading from different tables. 我发现您可以使用SubclassMap <>,但这不起作用,因为它是用于从不同的表加载的。

Is there any way to make this run without redundant code? 没有冗余代码,有什么方法可以使它运行吗?

How about: 怎么样:

public class DocumentBaseMap : ClassMap<T> where T : DocumentOverview
{
    public DocumentBaseMap()
    {
        Table("Documents");
        Id(x => x.Id);
        Map(x => x.Name).Not.Nullable();
    }
}

public class DocumentOverviewMap : DocumentBaseMap<DocumentOverview>
{
}

public class DocumentMap : DocumentBaseMap<Document>
{
    public DocumentMap() 
        : base()
    {
        Map(x => x.Data).CustomType<BinaryBlobType>().Nullable();
    }
}

I'm wondering if the lack of the type parameter in your base class map is the reason why you only get the Document type? 我想知道是否在基类映射中缺少type参数是为什么只获得Document类型的原因? The empty DocumentOverviewMap class is a bit smelly. 空的DocumentOverviewMap类有点臭。 If it's the need for defined maps of the Document and DocumentOverview types that's the problem, then there's probably a better way to do this. 如果问题在于需要DocumentDocumentOverview类型的已定义映射,则可能有更好的方法来实现。

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

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