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

# UnitOfWorkFactoryBase

`UnitOfWorkFactoryBase` implements `IUnitOfWorkFactory` and owns the connection-string-keyed `ISessionFactory` caching, per-process locking, and `ISessionHelper` construction shared across engines; `OracleUnitOfWorkFactory`, `OracleStatelessUnitOfWorkFactory`, and `SqlServerUnitOfWorkFactory` supply only the engine/session-strategy-specific session-opening logic (`CreateUnitOfWork`).

## Purpose
- Caches `ISessionFactory` instances in a static dictionary keyed by connection string, guarded by a lock, so repeated `GetUnitOfWork` calls for the same connection string reuse the same factory.
- Resolves the `PersistenceConfiguration` for a connection string (via the injected `IConfigurationFactory`) and calls `Configuration.BuildSessionFactory()` when no cached factory exists.
- Builds the engine-specific `ISessionHelper` (`OracleSessionHelper`/`SqlServerSessionHelper`) to pair with the opened session.

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

## Public members
| Member | Signature | Purpose |
| --- | --- | --- |
| GetUnitOfWork | `NHibernateUnitOfWork GetUnitOfWork(string connectionString, int batchSize, bool showSql)` | Locks the static `SessionFactories` dictionary, gets-or-adds an `ISessionFactory` via `BuildSessionFactory`, calls the subclass's `CreateUnitOfWork(factory)` override, builds the matching `ISessionHelper`, and returns a new `NHibernateUnitOfWork`. |

## Protected members
| Member | Signature | Purpose |
| --- | --- | --- |
| BuildSessionFactory | `protected ISessionFactory BuildSessionFactory(string connectionString, int batchSize, bool showSql)` | Resolves the `PersistenceConfiguration` via the injected `IConfigurationFactory` and calls `Configuration.BuildSessionFactory()`. |
| CreateUnitOfWork | `protected abstract ISessionStrategy CreateUnitOfWork(ISessionFactory factory)` | Engine/session-strategy-specific: opens a stateful `ISession` (`CreateSessionUnitOfWork`) or stateless `IStatelessSession` (`CreateStatelessSessionUnitOfWork`) and wraps it in the matching `ISessionStrategy` implementation. |
| BuildSessionHelper | `protected abstract ISessionHelper BuildSessionHelper()` | Engine-specific: builds `OracleSessionHelper` or `SqlServerSessionHelper`. |

## Behavioral notes
- The `SessionFactories` dictionary and its lock are static and shared per-process across all connection strings passed to any instance of this class or its subclasses.
- A new `NHibernateUnitOfWork` (and a new underlying session) is returned on every call — only the `ISessionFactory` is cached, not the session itself.

## Constraints and usage notes
- Subclasses must implement `CreateUnitOfWork` and `BuildSessionHelper`; `UnitOfWorkFactoryBase` itself cannot be instantiated directly.

## Sources
- `Factories/UnitOfWorkFactoryBase.cs`
