---
title: NHibernateUnitOfWork
summary: API reference for NHibernateUnitOfWork.
status: draft
owner: Framework
last_reviewed: 2026-09-15
library: SAIF.Framework.Data.NHibernate
reference_type: package
---

# NHibernateUnitOfWork

`NHibernateUnitOfWork` owns the transaction boundary for a single NHibernate session and hands out `Repository<TEntity>` instances bound to that session. It is returned by `IUnitOfWorkFactory.GetUnitOfWork` and is the type application code interacts with directly to begin/commit/roll back work and obtain repositories. It is classified as a framework-inherited type because it implements the external `IUnitOfWork` contract, which could not be located in this repository (see [Dependencies](../../dependencies.md)).

## Purpose
- Enforces the unit-of-work transaction lifecycle: `Begin()` starts a transaction (rejecting a second `Begin()` while one is active — [BR-001](../../business-rules.md#br-001-transaction-already-in-progress)); `Commit()` requires a prior `Begin()` ([BR-002](../../business-rules.md#br-002-commit-requires-a-prior-begin)); `Rollback()` unconditionally disposes the session after rolling back any active transaction.
- Provides `GetRepository<TEntity>()`, which always constructs a new `Repository<TEntity>` bound to this unit of work's `Session`/`SessionHelper`, per [BR-005](../../business-rules.md#br-005-repository-is-scoped-to-one-unit-of-work) — every repository obtained from the same unit of work shares the same transaction.
- Exposes `Session` (the underlying `ISessionStrategy`) and `SessionHelper` (the engine-specific date/ID helper) for repository construction.

## Context
- Namespace: `SAIF.Data.NHibernate.UnitOfWork`
- Assembly/package: `SAIF.Framework.Data.NHibernate`

## Public members
| Member | Signature | Purpose |
| --- | --- | --- |
| Session | `public ISessionStrategy Session { get; }` | The session strategy (stateful or stateless) this unit of work wraps; shared by every repository obtained from `GetRepository<TEntity>()`. |
| SessionHelper | `public ISessionHelper SessionHelper { get; }` | The engine-specific session helper (`OracleSessionHelper`/`SqlServerSessionHelper`) used for `SystemDate` and related engine-specific behavior. |
| Begin | `public void Begin()` | Checks `IsTransactionInProgress()` (transaction `IsActive`, `WasCommitted`, or `WasRolledBack`); throws `InvalidOperationException("There is a transaction already in progress.")` if so. Otherwise calls `Session.Clear()` then `Session.BeginTransaction(IsolationLevel.ReadCommitted)`. See [BR-001](../../business-rules.md#br-001-transaction-already-in-progress). |
| Commit | `public void Commit()` | Gets the current `ITransaction`; throws `InvalidOperationException("You must call Begin() on the unit of work before you can commit.")` if it is not active. Otherwise calls `transaction.Commit()` then `Session.Clear()`. See [BR-002](../../business-rules.md#br-002-commit-requires-a-prior-begin). |
| Rollback | `public void Rollback()` | If the transaction `IsActive`, calls `transaction.Rollback()`; then always calls `Session.Dispose()` regardless of whether a transaction was active. |
| GetRepository&lt;TEntity&gt; | `public Repository<TEntity> GetRepository<TEntity>() where TEntity : IDataEntity` | Constructs and returns a new `Repository<TEntity>` bound to `this` unit of work's session — every call returns a fresh repository instance sharing the same underlying session/transaction. |
| Dispose | `public void Dispose()` | Standard dispose pattern: if not already disposed and `Session` is not null, calls `Session.Dispose()`. |

## Behavioral notes
- `Rollback()` disposes the session even when no transaction was active — callers should treat any `Rollback()` call as terminal for the unit of work.
- All repositories obtained from a given `NHibernateUnitOfWork` instance (via `GetRepository<TEntity>()`) participate in the same transaction — the NHibernate-stack analogue of a BOF-style transaction-wrapping pattern per the `nhibernate-data-access` skill.*

## Constraints and usage notes
- A single `NHibernateUnitOfWork` instance must not have `Begin()` called twice without an intervening `Commit()`/`Rollback()` ([BR-001](../../business-rules.md#br-001-transaction-already-in-progress)).
- `Commit()` must always be preceded by `Begin()` on the same instance ([BR-002](../../business-rules.md#br-002-commit-requires-a-prior-begin)).

> **Note:** Statements marked with * were inferred from code structure, naming conventions, or
> contextual evidence where direct confirmation was not available in source artifacts.

## Sources
- `UnitOfWork/NHibernateUnitOfWork.cs`
- `Repositories/Repository.cs`
