NHibernate:从存储过程中返回额外的属性,而不是在模型中

时间:2011-04-08 20:49:38

标签: nhibernate nhibernate-mapping

使用NHibernate执行存储过程是否有办法返回原始模型中不存在的额外字段?

我正在做的是创建一个sproc来返回Resource的搜索结果。它返回我的Resource类的所有普通字段,但也返回一个名为Rank的额外字段。有没有办法将它映射到当前的Resource类(甚至创建一个继承自Resource的类,只添加一个属性)?

我的sproc的执行是:

<sql-query name="GetRelatedResources">
    <return alias="item" class="ResourceRanked">
        <return-property column="Rank" name="Rank" />
        <return-property column="ResourceId" name="Id" />
        <return-property column="Name" name="Name" />
        <return-property column="Description" name="Description" />
        <return-property column="Filename" name="Filename" />
        <return-property column="Filetype" name="Filetype" />
        <return-property column="IsPublic" name="IsPublic" />
        <return-property column="IsFeatured" name="IsFeatured" />
        <return-property column="VideoEmbedCode" name="VideoEmbedCode" />
        <return-property column="VideoId" name="VideoId" />
        <return-property column="VideoPlayerId" name="VideoPlayerId" />
        <return-property column="VideoPlayerKey" name="VideoPlayerKey" />
        <return-property column="VideoHeight" name="VideoHeight" />
        <return-property column="VideoWidth" name="VideoWidth" />
        <return-property column="IsDeleted" name="IsDeleted" />
        <return-property column="CreatedOn" name="CreatedOn" />
        <return-property column="ModifiedOn" name="ModifiedOn" />
        <return-property column="CreatedBy" name="CreatedBy" />
        <return-property column="ModifiedBy" name="ModifiedBy" />
        <return-property column="CreatedByName" name="CreatedByName" />
        <return-property column="ModifiedByName" name="ModifiedByName" />
    </return>
    exec dbo.gbi_sp_GetRelatedResources :pageSize, :pageIndex, :resourceId
</sql-query>

我的班级:

public class Resource : DomainEntity
{
    [Required(ErrorMessage = "Please enter a name"), StringLength(100, ErrorMessage = "Name length can not exceed 100 characters")]
    public virtual string Name { get; set; }

    [StringLength(200, ErrorMessage = "Description length can not exceed 200 characters")]
    public virtual string Description { get; set; }

    public virtual string Filename { get; set; }
    public virtual string Filetype { get; set; }

    public virtual bool IsPublic { get; set; }
    public virtual bool IsFeatured { get; set; }

    [StringLength(500, ErrorMessage = "Embed Code length can not exceed 500 characters")]
    public virtual string VideoEmbedCode { get; set; }
    public virtual long? VideoId { get; set; }
    public virtual long? VideoPlayerId { get; set; }
    [StringLength(100, ErrorMessage = "Player Key length can not exceed 100 characters")]
    public virtual string VideoPlayerKey { get; set; }
    public virtual int? VideoHeight { get; set; }
    public virtual int? VideoWidth { get; set; }

    //public virtual int Rank { get; set; }

    public virtual string Format { 
        get
        {
            if (ResourceFileType == ResourceFileType.EmbeddedVideo || ResourceFileType == ResourceFileType.VideoPlayer)
                return "Video";

            switch (Filetype)
            {
                case "pdf":
                    return "Adobe Acrobat";
                case "docx":
                case "doc":
                    return "Microsoft Word";
                case "ppt":
                case "pptx":
                    return "Microsoft PowerPoint";
                case "xls":
                case "xlsx":
                    return "Microsoft Excel";
                default:
                    return Filetype.ToUpper();
            }
        }
    }

    public virtual ResourceFileType ResourceFileType
    {
        get
        {
            if (VideoId.HasValue)
                return ResourceFileType.VideoPlayer;
            if (!VideoEmbedCode.IsNullOrEmpty() || VideoId.HasValue)
                return ResourceFileType.EmbeddedVideo;
            return ResourceFileType.Document;
        }
    }

    public virtual IEnumerable<Market> Markets { get; set; }
    public virtual IEnumerable<Workstream> Workstreams { get; set; }
    public virtual IEnumerable<Tag> Tags { get; set; }
    public virtual IEnumerable<Topic> Topics { get; set; }
    public virtual IEnumerable<ResourceType> Types { get; set; }

    public override string ToString()
    {
        return Id + " - " + Name;
    }
}

理想情况下,我想创建一个这样的类来扩展Resource

public class ResourceRanked : Resource
{
    public virtual int Rank { get; set; }
}

但除非我复制Resource类的xml映射,否则我无法工作。我已经查看了nhibernate的子类,但它们似乎不符合我的目的。

2 个答案:

答案 0 :(得分:0)

我找不到适合我的合适解决方案,所以不幸的是我只需要专门为存储过程的返回结果映射另一个类。

答案 1 :(得分:0)

更好的方法是在需要连接结果集的表上创建一个视图,并在存储过程中查询该视图并在.hbm文件中映射该视图。您可以在链接http://sachinpachori10.blogspot.in/2013/06/nhibernate-returning-extra-properties.html

中查看详细信息