Search code examples
nhibernatenhibernate-mapping

NHibernate: Returning extra properties from stored procedure not in model


Is there a way when executing a stored procedure with NHibernate to return an extra field that is not in the original model?

What I'm doing is creating a sproc to return search results of a Resource. It returns all of the normal fields of my Resource class, but also an extra field called Rank. Is there a way to map that on to the current Resource class (or even create a class that inherits from Resource and just adds that one property)?

The execution of my sproc is:

<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>

And my class:

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;
    }
}

Ideally I'd like to just make a class like this to extend Resource

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

But I can't get that to work unless I duplicate the xml mapping of the Resource class. I've looked at subclasses for nhibernate, but they don't seem to fit what I'm trying to do.


Solution

  • I couldn't find any suitable solution that worked for me, so I unfortunately just had to map another class specifically for the return result of the stored proc.