---
title: RepositoryBase&lt;TEntity&gt;
summary: API reference for RepositoryBase<TEntity>.
status: draft
owner: Framework
last_reviewed: 2026-09-15
library: SAIF.Framework.Data.NHibernate
reference_type: package
---

# RepositoryBase&lt;TEntity&gt;

`RepositoryBase<TEntity>` is the abstract generic CRUD/query surface shared by every repository in this library. It wraps an `ISessionStrategy` (stateful or stateless, per [BR-003](../../business-rules.md#br-003-stateless-updates-require-explicit-persistence)) and translates repository calls into the corresponding NHibernate session operation, diverging in behavior depending on which strategy the owning `NHibernateUnitOfWork` was built with. It is classified as a framework-inherited type because it implements the external `IRepository<TEntity>` contract, which could not be located in this repository (see [Dependencies](../../dependencies.md)).

## Purpose
- Provides `Add`, `Update`, `Delete`, `Query`, `Get`, `Load` generically for any mapped entity type `TEntity`, delegating to the `ISessionStrategy` obtained from the constructing `NHibernateUnitOfWork`.
- Exposes `SystemDate`, sourced from `ISessionHelper.SystemDate`, which runs an engine-specific "now" SQL query with a local-clock fallback (see [BR-004](../../business-rules.md#br-004-systemdate-falls-back-to-local-clock)).
- Is the base class every repository, including the concrete `Repository<TEntity>`, derives from — implements the external `IRepository<TEntity>` contract.

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

## Public members
| Member | Signature | Purpose |
| --- | --- | --- |
| Add | `public void Add(TEntity entity)` | Delegates to `Session.Add(entity)`: stateful strategy calls `ISession.SaveOrUpdate(entity)`; stateless strategy calls `IStatelessSession.Insert(entity)`. |
| Update | `public void Update(TEntity entity)` | Delegates to `Session.Update(entity)`: under the stateful strategy this is a no-op (relies on NHibernate dirty-checking flushed at `Commit()`); under the stateless strategy this issues an explicit `IStatelessSession.Update(entity)` call. See [BR-003](../../business-rules.md#br-003-stateless-updates-require-explicit-persistence). |
| Delete | `public void Delete(TEntity entity)` | Delegates to `Session.Delete(entity)` in both session strategies. |
| Query | `public IQueryable<TEntity> Query()` | Delegates to `Session.Query<TEntity>()` (LINQ to NHibernate) in both session strategies. |
| Get | `public TEntity Get(object id)` | Stateful: `ISession.Get<TEntity>(id)`. Stateless: `IStatelessSession.Get<TEntity>(id)`. |
| Load | `public TEntity Load(object id)` | Stateful: `ISession.Load<TEntity>(id)` (lazy proxy). Stateless: also delegates to `IStatelessSession.Get<TEntity>(id)` — no distinct lazy-proxy path exists for the stateless strategy. |
| SystemDate | `public DateTime SystemDate { get; }` | Reads `SessionHelper.SystemDate`, which runs the engine-specific date SQL (`select sysdate from dual` for Oracle, `Select GetDate()` for SQL Server) and falls back to `DateTime.Now` if the result is null, `DateTime.MinValue`, or `DateTime.MaxValue`. See [BR-004](../../business-rules.md#br-004-systemdate-falls-back-to-local-clock). |

## Protected members
| Member | Signature | Purpose |
| --- | --- | --- |
| Session | `protected ISessionStrategy Session` | The session strategy (stateful or stateless) this repository was constructed with; used by all CRUD/query members above. |
| SessionHelper | `protected internal ISessionHelper SessionHelper` | The engine-specific session helper (`OracleSessionHelper`/`SqlServerSessionHelper`) used to compute `SystemDate`. |

## Behavioral notes
- The divergence between stateful `Update` (no-op) and stateless `Update` (explicit write) is the single most consequential behavioral difference a caller must be aware of when switching between `OracleUnitOfWorkFactory`/`SqlServerUnitOfWorkFactory` (stateful) and `OracleStatelessUnitOfWorkFactory` (stateless) — see [BR-003](../../business-rules.md#br-003-stateless-updates-require-explicit-persistence).
- `Load` under the stateless strategy does not provide lazy-loading proxies, unlike the stateful strategy's `ISession.Load` — callers expecting a proxy object will instead get a fully materialized entity.

## Constraints and usage notes
- This class is `abstract`; it is never instantiated directly — `Repository<TEntity>` is the concrete subclass consumers use.
- A `RepositoryBase<TEntity>` instance is permanently bound to the `ISessionStrategy`/session it was constructed with; it cannot be moved to a different unit of work (see [BR-005](../../business-rules.md#br-005-repository-is-scoped-to-one-unit-of-work)).

## Sources
- `Repositories/RepositoryBase.cs`
- `Sessions/StatefulSessionStrategy.cs`
- `Sessions/StatelessSessionStrategy.cs`
- `SessionHelper/SessionHelperBase.cs`
