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

# ISessionStrategy

`ISessionStrategy` is the seam contract abstracting stateful (`ISession`) vs. stateless (`IStatelessSession`) NHibernate session semantics behind a single interface. `RepositoryBase<TEntity>` and `NHibernateUnitOfWork` code against this interface only, so the same repository/unit-of-work code works unchanged whether it is backed by `StatefulSessionStrategy` or `StatelessSessionStrategy` (both `internal`, not part of the public surface).

## Purpose
- Defines the common CRUD/query/transaction operations (`Clear`, `BeginTransaction`, `Transaction`, `CreateSqlQuery`, `Delete`, `Add`, `Update`, `Query<TEntity>`, `GetNamedQuery`, `Get<TEntity>`, `Load<TEntity>`, `Dispose`) that both session kinds must support.
- Is the type through which the [Repository CRUD and query operations (stateful vs stateless divergence)](../../operations-processes.md#repository-crud-and-query-operations-stateful-vs-stateless-divergence) operation is expressed: implementations differ chiefly in `Update` (no-op vs. explicit write, see [BR-003](../../business-rules.md#br-003-stateless-updates-require-explicit-persistence)) and `Load` (lazy proxy vs. eager fetch).

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

## Public members
| Member | Signature | Purpose |
| --- | --- | --- |
| Clear | `void Clear()` | Clears the session's first-level cache/tracked state. Called by `NHibernateUnitOfWork.Begin()` before starting a new transaction. |
| BeginTransaction | `ITransaction BeginTransaction(IsolationLevel isolationLevel)` | Starts a new transaction at the given isolation level. Called by `NHibernateUnitOfWork.Begin()` with `IsolationLevel.ReadCommitted`. |
| Transaction | `ITransaction Transaction()` | Returns the current (possibly inactive) transaction, used by `NHibernateUnitOfWork.Commit()`/`Rollback()` to check `IsActive`/`WasCommitted`/`WasRolledBack`. |
| CreateSqlQuery | `ISQLQuery CreateSqlQuery(string sql)` | Direct SQL escape hatch used by `ImageRepositoryExtensions` for Oracle sequence `NEXTVAL` queries; bypasses entity mapping entirely. |
| Delete | `void Delete(object entity)` | Deletes `entity` from the session in both stateful and stateless strategies. |
| Add | `void Add(object entity)` | Stateful: `ISession.SaveOrUpdate(entity)`. Stateless: `IStatelessSession.Insert(entity)`. |
| Update | `void Update(object entity)` | Stateful: no-op (relies on dirty-checking flush at commit). Stateless: explicit `IStatelessSession.Update(entity)`. See [BR-003](../../business-rules.md#br-003-stateless-updates-require-explicit-persistence). |
| Query&lt;TEntity&gt; | `IQueryable<TEntity> Query<TEntity>()` | Returns `Session.Query<TEntity>()` (LINQ to NHibernate) in both strategies. |
| GetNamedQuery | `IQuery GetNamedQuery(string name)` | Retrieves a named HQL/SQL query configured in the mapping set. |
| Get&lt;TEntity&gt; | `TEntity Get<TEntity>(object id)` | Stateful: `ISession.Get<TEntity>(id)`. Stateless: `IStatelessSession.Get<TEntity>(id)`. |
| Load&lt;TEntity&gt; | `TEntity Load<TEntity>(object id)` | Stateful: `ISession.Load<TEntity>(id)` (lazy proxy). Stateless: delegates to `Get<TEntity>(id)` — no distinct lazy path. |
| Dispose | `void Dispose()` | Disposes the underlying `ISession`/`IStatelessSession`. |

## Behavioral notes
- The two internal implementations, `StatefulSessionStrategy` and `StatelessSessionStrategy`, are the sole source of the stateful/stateless divergence documented throughout this library's business rules and operations; consumers of `ISessionStrategy` itself never see which concrete implementation they hold.

## Constraints and usage notes
- This interface is implemented only by the two `internal` classes in `Sessions/`; external consumers cannot supply a custom implementation without also having those types be `internal`-visible (i.e., in practice, only code within this assembly can implement `ISessionStrategy`).

## Sources
- `Interfaces/ISessionStrategy.cs`
- `Sessions/StatefulSessionStrategy.cs`
- `Sessions/StatelessSessionStrategy.cs`
