Skip to content

Commit

Permalink
added nhibernate paging methods and asp.net core samples
Browse files Browse the repository at this point in the history
  • Loading branch information
gpeipman committed May 10, 2019
1 parent e7ff776 commit ed0f44d
Show file tree
Hide file tree
Showing 17 changed files with 494 additions and 15 deletions.
Expand Up @@ -10,6 +10,7 @@ namespace DotNetPaging.AspNetCore.Controllers
public class HomeController : Controller
{
private readonly DotNetPagingDbContext _dataContext;


public HomeController(DotNetPagingDbContext dataContext)
{
Expand Down
@@ -0,0 +1,42 @@
using System.Linq;
using System.Threading.Tasks;
using DotNetPaging.NHibernate;
using Microsoft.AspNetCore.Mvc;
using NHibernate;

namespace DotNetPaging.AspNetCore.Controllers
{
public class NHibernateController : Controller
{
private readonly ISession _nhibernateSession;

public NHibernateController(ISession nhibernateSession)
{
_nhibernateSession = nhibernateSession;
}

public async Task<IActionResult> Index(int page = 1)
{
var releases = await _nhibernateSession.Query<PressRelease>()
.OrderByDescending(p => p.ReleaseDate)
.GetPagedAsync(page, 10);
return View(releases);
}

public async Task<IActionResult> ICriteria(int page = 1)
{
var releases = await _nhibernateSession.CreateCriteria<PressRelease>()
.GetPagedAsync<PressRelease>(page, 10);

return View(releases);
}

public async Task<IActionResult> QueryOver(int page = 1)
{
var releases = await _nhibernateSession.QueryOver<PressRelease>()
.GetPagedAsync<PressRelease>(page, 10);

return View(releases);
}
}
}
Expand Up @@ -17,6 +17,7 @@
<ItemGroup>
<ProjectReference Include="..\DotNetPaging.EFCore\DotNetPaging.EFCore.csproj" />
<ProjectReference Include="..\DotNetPaging.Infrastructure\DotNetPaging.Infrastructure.csproj" />
<ProjectReference Include="..\DotNetPaging.NHibernate\DotNetPaging.NHibernate.csproj" />
</ItemGroup>

</Project>
9 changes: 1 addition & 8 deletions DotNetPaging/DotNetPaging.AspNetCore/Program.cs
@@ -1,12 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace DotNetPaging.AspNetCore
{
Expand Down
4 changes: 3 additions & 1 deletion DotNetPaging/DotNetPaging.AspNetCore/Startup.cs
Expand Up @@ -26,9 +26,11 @@ public void ConfigureServices(IServiceCollection services)
services.AddDbContext<DotNetPagingDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection"),
sqlServerOptions => sqlServerOptions.UseRowNumberForPaging()) //nedded only for SQL Server versions prior to SQL Server 2012
sqlServerOptions => sqlServerOptions.UseRowNumberForPaging())
//needed only for SQL Server versions prior to SQL Server 2012
);

services.AddNHibernate(Configuration.GetConnectionString("DefaultConnection"));
services.AddHttpContextAccessor();
services.AddAutoMapper(GetType().Assembly);

Expand Down
@@ -0,0 +1,30 @@
@model PagedResult<DotNetPaging.NHibernate.PressRelease>
@{
ViewData["Title"] = "Home";
}

<h2>NHibernate ICriteria</h2>

<p>Results on this page are queried using asynchronous method calls of NHibernate.</p>

<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Company</th>
<th>Title</th>
</tr>
</thead>
<tbody>
@foreach (var release in Model.Results)
{
<tr>
<td>@release.ReleaseDate.ToShortDateString()</td>
<td>@release.Company</td>
<td>@release.Title</td>
</tr>
}
</tbody>
</table>

@(await Component.InvokeAsync<PagerViewComponent>(Model))
30 changes: 30 additions & 0 deletions DotNetPaging/DotNetPaging.AspNetCore/Views/NHibernate/Index.cshtml
@@ -0,0 +1,30 @@
@model PagedResult<DotNetPaging.NHibernate.PressRelease>
@{
ViewData["Title"] = "Home";
}

<h2>NHibernate LINQ</h2>

<p>Results on this page are queried using asynchronous method calls of NHibernate.</p>

<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Company</th>
<th>Title</th>
</tr>
</thead>
<tbody>
@foreach (var release in Model.Results)
{
<tr>
<td>@release.ReleaseDate.ToShortDateString()</td>
<td>@release.Company</td>
<td>@release.Title</td>
</tr>
}
</tbody>
</table>

@(await Component.InvokeAsync<PagerViewComponent>(Model))
@@ -0,0 +1,30 @@
@model PagedResult<DotNetPaging.NHibernate.PressRelease>
@{
ViewData["Title"] = "Home";
}

<h2>NHibernate QueryOver</h2>

<p>Results on this page are queried using asynchronous method calls of NHibernate.</p>

<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Company</th>
<th>Title</th>
</tr>
</thead>
<tbody>
@foreach (var release in Model.Results)
{
<tr>
<td>@release.ReleaseDate.ToShortDateString()</td>
<td>@release.Company</td>
<td>@release.Title</td>
</tr>
}
</tbody>
</table>

@(await Component.InvokeAsync<PagerViewComponent>(Model))
Expand Up @@ -24,10 +24,15 @@
</div>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-item nav-link" style="color:black">EF Core:</a>
<a asp-area="" asp-controller="Home" asp-action="Index" class="nav-item nav-link">Home</a>
<a asp-area="" asp-controller="Home" asp-action="Async" class="nav-item nav-link">Async</a>
<a asp-area="" asp-controller="Home" asp-action="AutoMapper" class="nav-item nav-link">AutoMapper</a>
<a asp-area="" asp-controller="Home" asp-action="TagHelper" class="nav-item nav-link">TagHelper</a>
<a class="nav-item nav-link" style="color:black">NHibernate:</a>
<a asp-area="" asp-controller="NHibernate" asp-action="Index" class="nav-item nav-link">Linq</a>
<a asp-area="" asp-controller="NHibernate" asp-action="ICriteria" class="nav-item nav-link">ICriteria</a>
<a asp-area="" asp-controller="NHibernate" asp-action="QueryOver" class="nav-item nav-link">QueryOver</a>
</div>
</div>
</div>
Expand Down
38 changes: 37 additions & 1 deletion DotNetPaging/DotNetPaging.EFCore/PagedResultEFCoreExtensions.cs
Expand Up @@ -4,10 +4,28 @@
using AutoMapper.QueryableExtensions;
using Microsoft.EntityFrameworkCore;

namespace DotNetPaging
namespace DotNetPaging.EFCore
{
public static class PagedResultEFCoreExtensions
{
public static PagedResult<T> GetPaged<T>(this IQueryable<T> query, int page, int pageSize)
{
var result = new PagedResult<T>
{
CurrentPage = page,
PageSize = pageSize,
RowCount = query.Count()
};

var pageCount = (double)result.RowCount / pageSize;
result.PageCount = (int)Math.Ceiling(pageCount);

var skip = (page - 1) * pageSize;
result.Results = query.Skip(skip).Take(pageSize).ToList();

return result;
}

public static async Task<PagedResult<T>> GetPagedAsync<T>(this IQueryable<T> query, int page, int pageSize)
{
var result = new PagedResult<T>
Expand All @@ -26,6 +44,24 @@ public static async Task<PagedResult<T>> GetPagedAsync<T>(this IQueryable<T> que
return result;
}

public static PagedResult<U> GetPaged<T, U>(this IQueryable<T> query, int page, int pageSize) where U : class
{
var result = new PagedResult<U>();
result.CurrentPage = page;
result.PageSize = pageSize;
result.RowCount = query.Count();

var pageCount = (double)result.RowCount / pageSize;
result.PageCount = (int)Math.Ceiling(pageCount);

var skip = (page - 1) * pageSize;
result.Results = query.Skip(skip)
.Take(pageSize)
.ProjectTo<U>()
.ToList();
return result;
}

public static async Task<PagedResult<U>> GetPagedAsync<T, U>(this IQueryable<T> query, int page, int pageSize) where U : class
{
var result = new PagedResult<U>();
Expand Down
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="8.1.0" />
<PackageReference Include="NHibernate" Version="5.2.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DotNetPaging.Infrastructure\DotNetPaging.Infrastructure.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions">
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.dependencyinjection.abstractions\2.2.0\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions DotNetPaging/DotNetPaging.NHibernate/NHibernateExtensions.cs
@@ -0,0 +1,36 @@
using Microsoft.Extensions.DependencyInjection;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Mapping.ByCode;

namespace DotNetPaging
{
public static class NHibernateExtensions
{
public static IServiceCollection AddNHibernate(this IServiceCollection services, string connectionString)
{
var mapper = new ModelMapper();
mapper.AddMappings(typeof(NHibernateExtensions).Assembly.ExportedTypes);
var domainMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

var configuration = new Configuration();
configuration.DataBaseIntegration(c =>
{
c.Dialect<MsSql2012Dialect>();
c.ConnectionString = connectionString;
c.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
c.SchemaAction = SchemaAutoAction.Validate;
c.LogFormattedSql = true;
c.LogSqlInConsole = true;
});
configuration.AddMapping(domainMapping);

var sessionFactory = configuration.BuildSessionFactory();

services.AddScoped(factory => sessionFactory.OpenSession());

return services;
}
}
}

0 comments on commit ed0f44d

Please sign in to comment.