使用Fluent NHibernate进行类映射设置

Mr *_*ode 3 stored-procedures class map fluent-nhibernate fluent-nhibernate-mapping

我是使用NHibernate的新手,我努力在网上找到如何为存储过程创建ClassMap的明确示例,而不使用XML作为映射.我最近使用Fluent接口工作,想分享我学到的东西.

有问题的存储过程返回如下对象:

public class ProductCategoryNavigation
{
    public virtual int CategoryId { get; protected set; }
    public virtual int CategoryNodeId { get; set; }
    public virtual int ParentCategoryNodeId { get; set; }
    public virtual string Name { get; set; }
    public virtual string Title { get; set; }
    public virtual string SeoUrl { get; set; }
    public virtual bool IsActive { get; set; }
    public virtual int DisplayOrder { get; set; }
    public virtual int ProductCount { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

那么,我如何创建一个NHibernate将用于将存储过程的结果映射到此对象的ClassMap?

Mr *_*ode 8

ClassMap看起来像这样:

public sealed class ProductCategoryNavigationMap : ClassMap<ProductCategoryNavigation>
{
    public ProductCategoryNavigationMap()
    {
        ReadOnly();

        // Set "CategoryId" property as the ID column. Without this, 
        // OpenSession() threw an exception that the configuration was invalid
        Id(x => x.CategoryId);
        Map(x => x.CategoryNodeId);
        Map(x => x.ParentCategoryNodeId);
        Map(x => x.Name);
        Map(x => x.Title);
        Map(x => x.SeoUrl);
        // The column name returned from the sproc is "VisibleInd", 
        // so this is here to map it to the "IsActive" property
        Map(x => x.IsActive).Column("VisibleInd"); 
        Map(x => x.DisplayOrder);
        Map(x => x.ProductCount);
    }
}
Run Code Online (Sandbox Code Playgroud)

对存储过程的调用如下所示:

public List<NavigationViewModel> GetNavigationViewModel(int portalId, int localeId)
{
    const string sql = "EXEC [dbo].[Stored_Procedure_Name] @PortalId=:PortalId, @LocaleId=:LocaleId";
    return _session.CreateSQLQuery(sql)
                .AddEntity(typeof(ProductCategoryNavigation))
                .SetInt32("PortalId", portalId)
                .SetInt32("LocaleId", localeId)
                .List<ProductCategoryNavigation>()
                .Select(x => new NavigationViewModel
                                 {
                                     CategoryId = x.CategoryId,
                                     CategoryNodeId = x.CategoryNodeId,
                                     ParentCategoryNodeId = x.ParentCategoryNodeId,
                                     Name = x.Name,
                                     Title = x.Title,
                                     SeoUrl = x.SeoUrl,
                                     IsActive = x.IsActive,
                                     DisplayOrder = x.DisplayOrder,
                                     ProductCount = x.ProductCount
                                 })
                .ToList();
}
Run Code Online (Sandbox Code Playgroud)

AddEntity调用说明要将结果映射到的Entity类,它将使用上面定义的ProductCategoryNavigationMap:

.AddEntity(typeof(ProductCategoryNavigation))
Run Code Online (Sandbox Code Playgroud)

如果仔细查看"sql"变量的值,您将看到两个参数:

  1. :PortalId
  2. :的LocaleID

这些是通过拨打电话来设定的:

.SetInt32("PortalId", portalId)
.SetInt32("LocaleId", localeId)
Run Code Online (Sandbox Code Playgroud)

然后调用.List<ProductCategoryNavigation>()为我们提供了一个IList,它允许我们使用LINQ来投影任何我们想要的东西.在这种情况下,我得到一个NavigationViewModel列表,它当前与ProductCategoryNavigation相同,但可以根据需要独立于实体进行更改.

我希望这有助于其他新兴NHibernate的开发人员!