Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/beginor/nhibernate-extensions
NHibernate Extensions for .NET Core
https://github.com/beginor/nhibernate-extensions
Last synced: 8 days ago
JSON representation
NHibernate Extensions for .NET Core
- Host: GitHub
- URL: https://github.com/beginor/nhibernate-extensions
- Owner: beginor
- License: lgpl-2.1
- Created: 2018-06-01T02:47:20.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-09-14T03:26:24.000Z (about 2 months ago)
- Last Synced: 2024-09-15T14:06:15.977Z (about 2 months ago)
- Language: C#
- Size: 788 KB
- Stars: 18
- Watchers: 5
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Some useful extensions for NHibernate
Some useful extensions for NHibernate used in my projects.
## NHibernate.Extensions.Npgsql
- Extended PostgreSQL driver [NpgsqlDriver](https://github.com/beginor/nhibernate-extensions/blob/master/src/NHibernate.Extensions.Npgsql/NpgsqlDriver.cs) for NHibernate, with support of:
- Type of `json` and `jsonb`
- Array type of `boolean`, `double`, `float (real)`, `int`, `short (small int)`, `long (big int)` and `string (character)`;
- Add `array_contains` and `array_intersects` function to hql query;
- Add `ArrayContains` and `ArrayIntersects` extension to linq query;- Extended PostgreSQL dialect [NpgsqlDialect](https://github.com/beginor/nhibernate-extensions/blob/master/src/NHibernate.Extensions.Npgsql/NpgsqlDialect.cs) for schema export of supported types.
Refer [ArrTestEntity](./test/NHibernate.Extensions.UnitTest/TestDb/ArrTestEntity.cs) for mapping, and [ArrTest](./test/NHibernate.Extensions.UnitTest/ArrTest.cs) for sample query usage;
## NHibernate.Extensions.Sqlite
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](https://www.nuget.org/packages/Microsoft.Data.Sqlite.Core/) and [SQLitePCLRaw.bundle_e_sqlite3](https://www.nuget.org/packages/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.
## NHibernate.NetCore
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 with `IServiceProvider`;
- `OrderBy(string propertyName)`, `OrderByDescending(string propertyName)` and
`AddOrderBy(string propertyName, bool isAsc)` extension methods to `IQueryable`,
make fun with dynamic order by.Example usage:
```cs
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:
```cs
[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> GetAll() {
try {
using (var session = sessionFactory.OpenSession()) {
var result = await session.Query().ToListAsync();
return result;
}
}
catch (Exception ex) {
return StatusCode(500, ex.Message);
}
}
}
```Or just use `ISession` in your controller:
```cs
[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> GetAll() {
try {
var result = await session.Query().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;