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).
Purpose¶
- Enforces the unit-of-work transaction lifecycle:
Begin()starts a transaction (rejecting a secondBegin()while one is active — BR-001);Commit()requires a priorBegin()(BR-002);Rollback()unconditionally disposes the session after rolling back any active transaction. - Provides
GetRepository<TEntity>(), which always constructs a newRepository<TEntity>bound to this unit of work'sSession/SessionHelper, per BR-005 — every repository obtained from the same unit of work shares the same transaction. - Exposes
Session(the underlyingISessionStrategy) andSessionHelper(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. |
| 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. |
| Rollback | public void Rollback() |
If the transaction IsActive, calls transaction.Rollback(); then always calls Session.Dispose() regardless of whether a transaction was active. |
| GetRepository<TEntity> | 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 anyRollback()call as terminal for the unit of work.- All repositories obtained from a given
NHibernateUnitOfWorkinstance (viaGetRepository<TEntity>()) participate in the same transaction — the NHibernate-stack analogue of a BOF-style transaction-wrapping pattern per thenhibernate-data-accessskill.*
Constraints and usage notes¶
- A single
NHibernateUnitOfWorkinstance must not haveBegin()called twice without an interveningCommit()/Rollback()(BR-001). Commit()must always be preceded byBegin()on the same instance (BR-002).
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.csRepositories/Repository.cs