使用 Fluent NHibernate 设置类映射

发布于 2024-12-22 16:44:30 字数 8 浏览 5 评论 0原文

continue

I'm new to using NHibernate and I struggled to find clear examples online of how to create a ClassMap for a stored procedure without using XML for the mappings. I recently got this working using the Fluent interfaces and wanted to share what I've learned.

The stored procedure in question returns an object like this:

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

So, how do I create a ClassMap that NHibernate will use to map the result of a stored procedure to this object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柒七 2024-12-29 16:44:30

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

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

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

AddEntity 调用说明将结果映射到哪个实体类,它将使用上面定义的 ProductCategoryNavigationMap:

.AddEntity(typeof(ProductCategoryNavigation))

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

  1. :PortalId
  2. :LocaleId

这些参数是通过调用来设置的:

.SetInt32("PortalId", portalId)
.SetInt32("LocaleId", localeId)

然后调用 .List() 为我们提供了IList,它允许我们使用 LINQ 来投影我们想要的任何内容。在本例中,我得到了一个 NavigationViewModel 列表,它当前与 ProductCategoryNavigation 相同,但可以根据需要独立于实体进行更改。

我希望这可以帮助其他刚接触 NHibernate 的开发人员!

The ClassMap looks like this:

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

The call to the stored procedure looks like this:

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

The AddEntity calls says what Entity class to map the results to, which will use the ProductCategoryNavigationMap defined above:

.AddEntity(typeof(ProductCategoryNavigation))

If you look carefully at the value of the "sql" variable, you'll see two parameters:

  1. :PortalId
  2. :LocaleId

Those are set by making calls to:

.SetInt32("PortalId", portalId)
.SetInt32("LocaleId", localeId)

Then the call to .List<ProductCategoryNavigation>() provides us with an IList, which allows us to use LINQ to project whatever we want. In this case I'm getting a List of NavigationViewModel, which is currently the same as ProductCategoryNavigation but can change independently of the entity as needed.

I hope this helps other developers new to NHibernate!

差↓一点笑了 2024-12-29 16:44:30

假设您已正确安装 NHibernate,您将在存储类映射的位置创建一个新类。

创建一个类,如下所示:

public class PcnMap : ClassMap<ProductCategoryNavigation>
{
   Table("TableName");
   Id( x => x.CategoryId );
   Map( model => model.CategoryNodeId );
   // more like this for all your properties
}

设置完成后,您可以根据需要使用存储库。

请记住,这只是一个基本设置。数据库结构越复杂,类映射就越复杂。

Assuming you have NHibernate properly installed, you would create a new class where ever your are storing your class maps.

Create a class like:

public class PcnMap : ClassMap<ProductCategoryNavigation>
{
   Table("TableName");
   Id( x => x.CategoryId );
   Map( model => model.CategoryNodeId );
   // more like this for all your properties
}

Once you have that set up, you use your repositories as needed.

Keep in mind that is only a basic set up. The more complicated your database structure is, the more complicated your class map will get.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文