Some useful extensions for NHibernate used in my projects.
-
Extended PostgreSQL driver NpgsqlDriver for NHibernate, with support of:
- Type of
json
andjsonb
- Array type of
boolean
,double
,float (real)
,int
,short (small int)
,long (big int)
andstring (character)
; - Add
array_contains
andarray_intersects
function to hql query; - Add
ArrayContains
andArrayIntersects
extension to linq query;
- Type of
-
Extended PostgreSQL dialect NpgsqlDialect for schema export of supported types.
Refer ArrTestEntity for mapping, and ArrTest for sample query usage;
NHibernate driver for the Microsoft.Data.Sqlite.Core data provider for .NET.
In order to use this driver, you must have the nuget package Microsoft.Data.Sqlite.Core and SQLitePCLRaw.bundle_e_sqlite3 installed for NHibernate to load.
It's a .NET Standard 2.0 Driver, Can run on any platform that dotnet core support.
Extensions for NHibernate when used in dotnet core:
- Use
Microsoft.Extensions.Logging.ILoggerFactory
as NHibernate's logger factory; - Service Collection Extension of
AddHibernate
method for integration withIServiceProvider
; OrderBy(string propertyName)
,OrderByDescending(string propertyName)
andAddOrderBy(string propertyName, bool isAsc)
extension methods toIQueryable<T>
, make fun with dynamic order by.
Example usage:
public void ConfigureServices(IServiceCollection services) {
// where is your hibernate.config path
var path = System.IO.Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"hibernate.config"
);
// add NHibernate services;
services.AddHibernate(path);
}
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
Microsoft.Extensions.Logging.ILoggerFactory loggerFactory
) {
// Use loggerFactory as NHibernate logger factory.
loggerFactory.UseAsHibernateLoggerFactory();
/* other code goes here */
}
And then use ISessionFactory
in your controller:
[Route("api/[controller]")]
public class SamplesController : Controller {
private ISessionFactory sessionFactory;
public SamplesController(ISessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected override void Dispose(bool disposing) {
if (disposing) {
sessionFactory = null;
}
}
[HttpGet("")]
public async Task<IList<Sample>> GetAll() {
try {
using (var session = sessionFactory.OpenSession()) {
var result = await session.Query<Sample>().ToListAsync();
return result;
}
}
catch (Exception ex) {
return StatusCode(500, ex.Message);
}
}
}
Or just use ISession
in your controller:
[Route("api/[controller]")]
public class SamplesController : Controller {
private ISession session;
public SamplesController(ISession session) {
this.session = session;
}
protected override void Dispose(bool disposing) {
if (disposing) {
session = null;
}
}
[HttpGet("")]
public async Task<IList<Sample>> GetAll() {
try {
var result = await session.Query<Sample>().ToListAsync();
return result;
}
catch (Exception ex) {
return StatusCode(500, ex.Message);
}
}
}
ISessionFactory
is registerd as a singleton service, you should not dispose it;ISession
is registerd as a scoped service, so you do not need to dispose it by hand;