Sphere10.Framework.Data.NHibernate 3.2.0

dotnet add package Sphere10.Framework.Data.NHibernate --version 3.2.0
                    
NuGet\Install-Package Sphere10.Framework.Data.NHibernate -Version 3.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sphere10.Framework.Data.NHibernate" Version="3.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sphere10.Framework.Data.NHibernate" Version="3.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Sphere10.Framework.Data.NHibernate" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sphere10.Framework.Data.NHibernate --version 3.2.0
                    
#r "nuget: Sphere10.Framework.Data.NHibernate, 3.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Sphere10.Framework.Data.NHibernate@3.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sphere10.Framework.Data.NHibernate&version=3.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Sphere10.Framework.Data.NHibernate&version=3.2.0
                    
Install as a Cake Tool

πŸ’Ύ Sphere10.Framework.Data.NHibernate

Sphere10.Framework.Data.NHibernate adds NHibernate and FluentNHibernate helpers to Sphere10.Framework.Data. Use it when an application maps its entities with FluentNHibernate and wants the shared database-manager contract to create its schema, seed data and open session factories.

The package supplies a database-manager base class, a data-generator base class, FluentNHibernate conventions, JSON column types, an enum type-table pattern, an auditing listener and SQL Server dialect and driver extensions. It has no session-factory builder of its own: configure NHibernate with FluentNHibernate's Fluently.Configure().

πŸ“¦ Installation

dotnet add package Sphere10.Framework.Data.NHibernate

The package depends on NHibernate, FluentNHibernate, Sphere10.Framework.Data, Sphere10.Framework.Data.MSSQL and Sphere10.Framework. The project targets net8.0, net9.0 and net10.0, and a build with a .NET 11 SDK also targets net11.0. To build against source instead, reference src/Sphere10.Framework.Data.NHibernate/Sphere10.Framework.Data.NHibernate.csproj with a ProjectReference.

⚑ Create a database and open sessions

Derive a manager from NHDatabaseManagerBase. It decorates a provider's IDatabaseManager, which creates, tests and drops the empty database, and adds the NHibernate configuration that creates the schema:

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using Sphere10.Framework.Data;
using Sphere10.Framework.Data.NHibernate;

public class AppDatabaseManager : NHDatabaseManagerBase {

	public AppDatabaseManager()
		: base(new MSSQLDatabaseManager()) {
	}

	protected override FluentConfiguration GetFluentConfig(string connectionString)
		=> Fluently
			.Configure()
			.Database(MsSqlConfiguration.MsSql2012.ConnectionString(connectionString))
			.Mappings(m => m.FluentMappings
				.AddFromAssemblyOf<AppDatabaseManager>()
				.Conventions.Add(new CoreConventions(), new ForeignKeyConventions(), new EnumConvention(), new StringColumnLengthConvention()))
			.AddBusinessObjectAuditor();

	protected override IDataGenerator CreateDataGenerator(ISessionFactory sessionFactory, string databaseName, DatabaseGenerationDataPolicy policy)
		=> new AppDataGenerator(sessionFactory);
}

Use it through the shared contract:

var password = Environment.GetEnvironmentVariable("APPDB_PASSWORD");
var manager = new AppDatabaseManager();
var connectionString = manager.GenerateConnectionString("localhost", "AppDb", "appuser", password, null);
manager.CreateEmptyDatabase(connectionString);
manager.CreateApplicationDatabase(connectionString, DatabaseGenerationDataPolicy.PrimingData, "AppDb");

using var sessionFactory = manager.OpenDatabase(connectionString);
using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction()) {
	session.Save(new Customer { Name = "Alice" });
	transaction.Commit();
}

Customer and AppDataGenerator are application types, which the entity, mapping and seed-data sections below define. The session, transaction, query and mapping APIs are NHibernate's and FluentNHibernate's own.

Domains

Every folder of Sphere10.Framework.Data.NHibernate is a domain with its own README, which lists all of its public types.

Domain What it provides Source
ClientDrivers Provides an NHibernate SQL Server client driver that widens unsigned and byte parameter types to types SQL Server can store. source
Conventions Provides FluentNHibernate conventions for table naming, foreign-key naming, enum mapping and string and binary column sizes. source
DataPatterns Provides the enum type-table pattern, an entity and FluentNHibernate map that store one row per enum value. source
Database Provides the NHibernate database-manager contract and base classes that create a schema, seed it and open session factories. source
Dialects Provides an NHibernate SQL Server dialect that maps unsigned and byte CLR types to wider signed column types. source
Extensions Provides extension methods that add the auditing listener to a FluentNHibernate configuration and attach detached entities to a session. source
Json Provides NHibernate IUserType implementations that store an object as a JSON string column, optionally encrypted. source
Listeners Holds the NHibernate event listener behind the auditing extension and a checksum helper for business-object buffers. source
Scopes Provides DataAccessScope, a nestable transactional scope that shares one NHibernate session and transaction across nested scopes. source
Types Reads decimal columns through provider conversions while preserving their SQL definitions. source

Project root

Types declared in the project folder itself:

Type Kind Purpose
Tools.NHibernate tools Holds NVarCharMaxValue (4001), a string length that NHibernate maps to an unbounded string column, used by the JSON column types

🧩 Public API

The types are in Sphere10.Framework.Data.NHibernate, except Tools.NHibernate, which is in the global Tools namespace.

API Purpose
INHDatabaseManager An IDatabaseManager that also opens databases as ISessionFactory instances.
NHDatabaseManagerBase A DatabaseManagerDecorator implementing INHDatabaseManager. Subclasses supply GetFluentConfig(connectionString) and CreateDataGenerator(...).
NHDataGeneratorBase An IDataGenerator whose Populate() saves the objects from CreateData() with SaveOrUpdate in one session and transaction. CreateTypeTable<TEnum>() and CreateTypeTable(enumType) create one type-table row per enum value.
TypeTable, TypeTable<TEnum>, TypeTableMap<TEnum> Entity and FluentNHibernate map for a lookup table of an enum's values, with an assigned ID, a unique Name and an optional Description from the enum value's description. The table is named after the enum unless a subclass overrides TableName, and UniqueName can turn off the unique constraint.
JsonColumnType<T> An IUserType that stores an object as a JSON string through Sphere10FrameworkJsonSerializer. An empty or blank value reads as a new T.
EncryptedJsonColumnTypeBase<T> The same column type with EncryptedJsonSerializer. Derive a type with a parameterless constructor that passes the password, because NHibernate constructs custom types itself.
JsonColumnTypeBase<T> The base for JSON column types that use another IJsonSerializer.
FluentConfiguration.AddBusinessObjectAuditor() Adds pre-insert and pre-update listeners. On insert they set CreatedOnUtc and LastUpdatedOnUtc to the current UTC time and Active to true, and on update they set LastUpdatedOnUtc. Each property is set only when the entity has a property with that name.
ISession.Attach(entity) Adds a detached entity to the session's persistence context as loaded, without reading it from the database. The mode argument is ignored.
ExtendedMssqlDialect, ExtendedSql2008ClientDriver SQL Server 2008 dialect and driver that store byte, ushort, uint and ulong values as SMALLINT, INT, BIGINT and DECIMAL(28).
Tools.NHibernate.NVarCharMaxValue 4001, a string length that NHibernate maps to an unbounded string column such as NVARCHAR(MAX). The JSON column types use it.

πŸ—‚οΈ Schema creation and data policies

CreateApplicationDatabase(connectionString, dataPolicy, databaseName) builds a session factory from GetFluentConfig(connectionString). While it builds, SetCreateDatabaseConfiguration runs NHibernate's SchemaExport.Create, which drops and then creates the mapped tables. Override SetCreateDatabaseConfiguration to change that step. The database must already exist, so call CreateEmptyDatabase first.

After the schema exists, the manager calls its OnDatabaseSchemasCreated hook. DatabaseGenerationDataPolicy.NoData then adds no rows, while PrimingData and DemoData call CreateDataGenerator and run the returned generator's Populate(). The session factory is disposed when the method returns, and OpenDatabase(connectionString) builds a new one from the same configuration.

GenerateConnectionString, DatabaseExists, CreateEmptyDatabase and DropDatabase pass through to the decorated provider manager, such as MSSQLDatabaseManager or SqliteDatabaseManager.

πŸ—ΊοΈ Entities and mappings

FluentNHibernate maps the virtual properties of plain classes, which lets NHibernate create lazy-loading proxies. The quick start's Customer belongs to this model, and the remaining examples use it:

using System;
using System.Collections.Generic;
using FluentNHibernate.Mapping;
using Sphere10.Framework.Data.NHibernate;

public enum OrderStatus {
	Pending = 1,
	Shipped = 2,
	Cancelled = 3
}

public class CustomerPreferences {
	public string Language { get; set; }
	public bool Newsletter { get; set; }
}

public class Customer {
	public virtual int ID { get; set; }
	public virtual string Name { get; set; }
	public virtual string Email { get; set; }
	public virtual CustomerPreferences Preferences { get; set; }
	public virtual DateTime CreatedOnUtc { get; set; }
	public virtual DateTime LastUpdatedOnUtc { get; set; }
	public virtual bool Active { get; set; }
	public virtual IList<CustomerOrder> Orders { get; set; } = new List<CustomerOrder>();
}

public class Product {
	public virtual int ID { get; set; }
	public virtual string Name { get; set; }
	public virtual decimal Price { get; set; }
}

public class CustomerOrder {
	public virtual int ID { get; set; }
	public virtual Customer Customer { get; set; }
	public virtual Product Product { get; set; }
	public virtual int Quantity { get; set; }
	public virtual OrderStatus Status { get; set; }
	public virtual DateTime OrderedOnUtc { get; set; }
}

public class CustomerMap : ClassMap<Customer> {
	public CustomerMap() {
		Id(x => x.ID).GeneratedBy.Native();
		Map(x => x.Name).Not.Nullable();
		Map(x => x.Email);
		Map(x => x.Preferences).CustomType<JsonColumnType<CustomerPreferences>>();
		Map(x => x.CreatedOnUtc);
		Map(x => x.LastUpdatedOnUtc);
		Map(x => x.Active);
		HasMany(x => x.Orders).Inverse().Cascade.AllDeleteOrphan();
	}
}

public class ProductMap : ClassMap<Product> {
	public ProductMap() {
		Id(x => x.ID).GeneratedBy.Native();
		Map(x => x.Name).Not.Nullable();
		Map(x => x.Price).Precision(10).Scale(2);
	}
}

public class CustomerOrderMap : ClassMap<CustomerOrder> {
	public CustomerOrderMap() {
		Id(x => x.ID).GeneratedBy.Native();
		References(x => x.Customer).Not.Nullable();
		References(x => x.Product).Not.Nullable();
		Map(x => x.Quantity);
		Map(x => x.Status);
		Map(x => x.OrderedOnUtc);
	}
}

// One row per OrderStatus value, in a table named OrderStatus.
public class OrderStatusMap : TypeTableMap<OrderStatus> {
}

With the quick start's conventions, ForeignKeyConventions names the reference columns CustomerID and ProductID, and EnumConvention stores Status as its number. AddBusinessObjectAuditor() fills CreatedOnUtc, LastUpdatedOnUtc and Active, because Customer has properties with those names. The order entity is called CustomerOrder because ORDER is a reserved word in SQL.

JsonColumnType<T> stores Preferences as JSON text. To encrypt such a column, derive a column type from EncryptedJsonColumnTypeBase<T> and map the property with CustomType<CustomerNotesColumnType>():

public class CustomerNotes {
	public string Text { get; set; }
}

// NHibernate constructs custom types itself, so the password comes from a parameterless constructor.
public class CustomerNotesColumnType : EncryptedJsonColumnTypeBase<CustomerNotes> {
	public CustomerNotesColumnType()
		: base(Environment.GetEnvironmentVariable("APP_NOTES_KEY")) {
	}
}

🌱 Seed data

CreateApplicationDatabase calls CreateDataGenerator for PrimingData and DemoData. The quick start's AppDataGenerator writes one type-table row per OrderStatus value and two products:

public class AppDataGenerator : NHDataGeneratorBase {

	public AppDataGenerator(ISessionFactory sessionFactory)
		: base(sessionFactory) {
	}

	protected override IEnumerable<object> CreateData() {
		foreach (var status in CreateTypeTable<OrderStatus>())
			yield return status;

		yield return new Product { Name = "Laptop", Price = 999.99m };
		yield return new Product { Name = "Mouse", Price = 29.99m };
	}
}

Populate() saves the objects with SaveOrUpdate in one session and transaction, in the order that CreateData() returns them. Use the policy argument of CreateDataGenerator to choose between priming data and demo data.

πŸ”Œ Other databases

The manager decorates any provider IDatabaseManager. For SQLite, decorate SqliteDatabaseManager from Sphere10.Framework.Data.Sqlite and configure FluentNHibernate's SQLiteConfiguration:

using System.IO;

public class SqliteAppDatabaseManager : NHDatabaseManagerBase {

	public SqliteAppDatabaseManager()
		: base(new SqliteDatabaseManager()) {
	}

	protected override FluentConfiguration GetFluentConfig(string connectionString)
		=> Fluently
			.Configure()
			.Database(SQLiteConfiguration.Standard.ConnectionString(connectionString))
			.Mappings(m => m.FluentMappings
				.AddFromAssemblyOf<SqliteAppDatabaseManager>()
				.Conventions.Add(new CoreConventions(), new ForeignKeyConventions(), new EnumConvention()))
			.AddBusinessObjectAuditor();

	protected override IDataGenerator CreateDataGenerator(ISessionFactory sessionFactory, string databaseName, DatabaseGenerationDataPolicy policy)
		=> new AppDataGenerator(sessionFactory);
}

Create, seed and open the database file through it:

var manager = new SqliteAppDatabaseManager();
var connectionString = manager.GenerateConnectionString(null, Path.Combine(Path.GetTempPath(), "app.sqlite"), null, null, null);
if (!manager.DatabaseExists(connectionString))
	manager.CreateEmptyDatabase(connectionString);
manager.CreateApplicationDatabase(connectionString, DatabaseGenerationDataPolicy.DemoData, "app");

using var sessionFactory = manager.OpenDatabase(connectionString);

For SQL Server columns of unsigned types, select the extended dialect and its client driver:

var configuration = Fluently
	.Configure()
	.Database(MsSqlConfiguration.MsSql2008
		.ConnectionString(connectionString)
		.Dialect<ExtendedMssqlDialect>()
		.Driver<ExtendedSql2008ClientDriver>());

πŸ”Ž LINQ and HQL queries

Open sessions from the factory that OpenDatabase returns. Query<T>() is NHibernate's LINQ provider, and Fetch comes from NHibernate.Linq:

using System.Linq;
using NHibernate.Linq;

using (var session = sessionFactory.OpenSession()) {
	var customersNamedA = session.Query<Customer>()
		.Where(c => c.Name.StartsWith("A"))
		.OrderBy(c => c.Name)
		.ToList();

	var since = DateTime.UtcNow.AddDays(-7);
	var recentOrders = session.Query<CustomerOrder>()
		.Where(o => o.OrderedOnUtc > since)
		.OrderByDescending(o => o.OrderedOnUtc)
		.Take(10)
		.Fetch(o => o.Product)
		.ToList();

	var expensiveProducts = session.Query<Product>()
		.Where(p => p.Price > 100m)
		.Select(p => new { p.Name, p.Price })
		.ToList();

	var averagePrice = session.Query<Product>().Average(p => p.Price);
	var maxPrice = session.Query<Product>().Max(p => p.Price);

	var totalsByCustomer = session.Query<CustomerOrder>()
		.GroupBy(o => o.Customer.Name)
		.Select(g => new { Customer = g.Key, OrderCount = g.Count(), Total = g.Sum(o => o.Quantity * o.Product.Price) })
		.ToList();

	var customersLikeA = session.CreateQuery("from Customer c where c.Name like :name order by c.Name")
		.SetParameter("name", "A%")
		.List<Customer>();

	var customerCount = session.CreateQuery("select count(c) from Customer c")
		.UniqueResult<long>();
}

HQL names entities and properties rather than tables and columns, and its count returns a long. Fetch loads each order's product in the same query, which avoids one extra query per order when code reads order.Product. Keep sessions short-lived, and dispose each one when its work completes.

πŸ”— Relationships and cascading

The first session saves a customer with two orders, and the second removes one of them:

int customerID;
using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction()) {
	var laptop = new Product { Name = "Laptop", Price = 999.99m };
	var mouse = new Product { Name = "Mouse", Price = 29.99m };
	session.Save(laptop);
	session.Save(mouse);

	var customer = new Customer { Name = "Bob", Email = "bob@example.com" };
	customer.Orders.Add(new CustomerOrder { Customer = customer, Product = laptop, Quantity = 1, Status = OrderStatus.Pending, OrderedOnUtc = DateTime.UtcNow });
	customer.Orders.Add(new CustomerOrder { Customer = customer, Product = mouse, Quantity = 2, Status = OrderStatus.Pending, OrderedOnUtc = DateTime.UtcNow });

	// The cascade on Orders saves both orders with the customer.
	customerID = (int)session.Save(customer);
	transaction.Commit();
}

using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction()) {
	var customer = session.Get<Customer>(customerID);

	// Orders loads on first access, while the session is open.
	var firstOrder = customer.Orders.First();

	// Removing an order from the collection deletes the orphaned row on commit.
	customer.Orders.Remove(firstOrder);
	transaction.Commit();
}

Cascade.AllDeleteOrphan() saves, updates and deletes orders with their customer. Inverse() leaves the CustomerID column to each order's Customer reference, so set both sides when adding an order. Lazy collections and proxies load only while their session is open.

πŸ“¦ Batches and bulk statements

The example inserts many entities through one session, runs HQL bulk statements and inserts through a stateless session:

using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction()) {
	for (var i = 1; i <= 1000; i++) {
		session.Save(new Customer { Name = $"Customer {i}", Email = $"customer{i}@example.com" });

		// Write pending inserts and empty the session every 100 entities to bound its memory.
		if (i % 100 == 0) {
			session.Flush();
			session.Clear();
		}
	}
	transaction.Commit();
}

using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction()) {
	var deactivated = session.CreateQuery("update Customer c set c.Active = :active where c.Name like :pattern")
		.SetParameter("active", false)
		.SetParameter("pattern", "Customer 1%")
		.ExecuteUpdate();

	var deleted = session.CreateQuery("delete from CustomerOrder o where o.Status = :status")
		.SetParameter("status", OrderStatus.Cancelled)
		.ExecuteUpdate();

	Console.WriteLine($"Deactivated {deactivated} customers and deleted {deleted} orders");
	transaction.Commit();
}

using (var session = sessionFactory.OpenStatelessSession())
using (var transaction = session.BeginTransaction()) {
	foreach (var name in new[] { "Keyboard", "Monitor", "Webcam" })
		session.Insert(new Product { Name = name, Price = 49m });
	transaction.Commit();
}

Flush writes pending changes, and Clear detaches the saved entities so that the session stops growing. HQL update and delete statements run in the database and bypass the session's cache, cascades and listeners. A stateless session has no cache, change tracking, cascades or event listeners, so the auditor does not run for its inserts.

πŸ”„ Share a session with DataAccessScope

DataAccessScope opens one session for the outermost scope of a database. Nested scopes for the same DBMSType and connection string reuse that session and its transaction:

using (var scope = new DataAccessScope(manager, DBMSType.SQLServer, connectionString)) {
	scope.BeginTransaction();
	var customer = scope.Session.Query<Customer>().Single(c => c.Email == "alice@example.com");
	Rename(manager, connectionString, customer.ID, "Alice Smith");
	scope.Commit();
}

static void Rename(INHDatabaseManager manager, string connectionString, int customerID, string name) {
	// A nested scope for the same database shares the outer scope's session and transaction.
	using var scope = new DataAccessScope(manager, DBMSType.SQLServer, connectionString);
	scope.Session.Get<Customer>(customerID).Name = name;
}

The outermost scope flushes, closes and disposes the session when it ends. Each outermost scope calls OpenDatabase, which builds a new session factory, so open outermost scopes for units of work rather than for single queries.

πŸ“Ž Attach a detached entity

Attach adds an entity that another session loaded, such as one kept in a cache, without reading it again. The session takes the entity's current values as its database state, so only changes made after Attach are written:

using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction()) {
	session.Attach(cachedCustomer);
	cachedCustomer.Name = "Alice Smith";
	transaction.Commit();
}

πŸ”§ FluentNHibernate conventions

Add the conventions that suit the application's schema to FluentMappings.Conventions:

Convention Effect
CoreConventions Names a many-to-many table after the owning entity and the plural of the other entity, such as CustomerOrders.
ForeignKeyConventions Names a foreign-key column after its property or type with an ID suffix, such as CustomerID.
EnumConvention Maps enum and nullable enum properties through their enum type, so NHibernate stores the numeric value.
StringColumnLengthConvention Gives string properties without an explicit length a length of 4000.
AnsiStringConvention Maps every string property as AnsiString, which is a non-Unicode column.
BinaryColumnLengthConvention Maps byte[] properties as SQL Server varbinary(MAX).

From the repository root:

dotnet build src/Sphere10.Framework.Data.NHibernate/Sphere10.Framework.Data.NHibernate.csproj -c Release

πŸ“„ License & Author

License: Refer to repository LICENSE
Author: Herman Schoenfeld (sphere10.com)
Copyright: Β© 2018-Present Herman Schoenfeld. All rights reserved.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.2.0 0 11/24/2026
3.2.0-ci.173.45375449609.1 0 11/24/2026
3.2.0-ci.171.45374063975.1 0 11/24/2026
3.2.0-ci.169.45350717665.1 0 11/24/2026
3.2.0-ci.168.45166918016.1 44 11/23/2026
3.2.0-ci.163.43664782937.1 60 11/15/2026
3.2.0-ci.160.43660265392.1 64 11/15/2026
3.2.0-ci.159.43537748063.1 64 11/14/2026
3.2.0-ci.158.43536665157.1 63 11/14/2026
3.2.0-ci.157.43350632870.1 73 11/13/2026
3.2.0-ci.156.43233095581.1 65 11/12/2026
3.1.2 162 9/9/2026
3.1.1 129 9/8/2026
3.1.0 113 9/7/2026
3.0.3 181 1/6/2026
3.0.2 151 1/2/2026
3.0.1 155 1/2/2026
3.0.0 150 1/2/2026