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つのパラメーターが表示されます。
- :PortalId
- :LocaleId
これらは、次の呼び出しを行うことによって設定されます。
.SetInt32("PortalId", portalId)
.SetInt32("LocaleId", localeId)
次に、を呼び出すと.List<ProductCategoryNavigation>()
IListが提供されます。これにより、LINQを使用して必要なものを投影できます。この場合、NavigationViewModelのリストを取得しています。これは、現在ProductCategoryNavigationと同じですが、必要に応じてエンティティとは関係なく変更できます。
これがNHibernateを初めて使用する他の開発者に役立つことを願っています!