3

私はNHibernateを初めて使用し、マッピングにXMLを使用せずにストアドプロシージャのClassMapを作成する方法の明確な例をオンラインで見つけるのに苦労しました。私は最近、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; }
}

では、NHibernate がストアド プロシージャの結果をこのオブジェクトにマップするために使用する ClassMap を作成するにはどうすればよいでしょうか。

4

2 に答える 2

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

ストアドプロシージャの呼び出しは次のようになります。

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」変数の値を注意深く見ると、次の2つのパラメーターが表示されます。

  1. :PortalId
  2. :LocaleId

これらは、次の呼び出しを行うことによって設定されます。

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

次に、を呼び出すと.List<ProductCategoryNavigation>()IListが提供されます。これにより、LINQを使用して必要なものを投影できます。この場合、NavigationViewModelのリストを取得しています。これは、現在ProductCategoryNavigationと同じですが、必要に応じてエンティティとは関係なく変更できます。

これがNHibernateを初めて使用する他の開発者に役立つことを願っています!

于 2011-12-24T02:47:57.973 に答える
0

NHibernate が適切にインストールされていると仮定すると、クラス マップを格納している場所に新しいクラスを作成します。

次のようなクラスを作成します。

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

セットアップが完了したら、必要に応じてリポジトリを使用します。

これは基本的な設定にすぎないことに注意してください。データベース構造が複雑になるほど、クラス マップも複雑になります。

于 2011-12-23T18:49:52.620 に答える