Skip to content

Persistence defect patterns

This page lists defects in ConnectSoft persistence code: the domain state that services store, the migrations and seeds that build the schema, and the NHibernate sessions, cascades and queries that read and write it. Each pattern names the symptom, the cause, what to look for in a review, and how to prove the code is sound. The rules these patterns break are on the persistence and migrations page, with related rules on the domain service shape and error handling pages.

Domain state and history

CS-DP-PER-001: Record shows when it was entered, not when it took effect

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Symptom

  • A transition recorded late leaves a gap, because the successor record starts at the time of entry.
  • A history row shows the moment someone typed it, while a period on the same screen shows the effective date.
  • A future-dated entry stays at the top of a "latest events" list.

Cause

The command's effective date is applied to one of the records it writes only, or the entity has no effective-date field and the creation time stands in for it. Lists sort planned and actual dates together.

Check

  • Commands with an effective date apply it to every record they create or close.
  • Lists of past events sort on an occurred-at value that is never in the future.

Test

Record a transition dated a week ago, and assert that both sides and the history row carry that date.

CS-PER-010, CS-TIM-001

CS-DP-PER-002: Undo path recreates the state the forward guards refuse

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Symptom

Reversing a step leaves two conflicting open records.

Cause

The invariant guards only the forward commands. The cancel or reopen path reopens a record without checking it.

Check

For each invariant, every path that can reach the guarded state, including cancel, reopen, import and seed.

Test

Perform the forward action, then the reversal, and assert the invariant's refusal.

CS-SVC-006

Migrations and start-up

CS-DP-PER-003: Seed migration duplicates a lookup row the enumeration already seeds

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Symptom

Every fresh database fails migration with a primary-key violation on a lookup table.

Cause

The baseline migration seeds lookup tables from the members of the enumeration, so adding a member already inserts its row. A separate incremental migration inserts the same row again.

Check

A new migration that inserts lookup rows for a member just added to an enumeration.

Test

A test that migrates a fresh, empty database up to the latest version.

CS-PER-004, CS-PER-002

CS-DP-PER-004: Edited baseline migration never runs again on an existing database

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Symptom

A schema change folded into the baseline works on fresh databases but not on existing ones, and its test passes without proving anything.

Cause

A database that has already applied version 1 never runs migration 1 again. A test catalog that still holds the object from the old incremental migration satisfies the new assertion.

Check

  • Edits to the baseline migration without a test that migrates from an empty database.
  • Deleted incremental migrations without a recreate of the databases that recorded them.

Test

Migrate a fresh, empty database and assert that the object exists, after confirming that the version table shows only the baseline.

CS-PER-002, CS-PER-009

CS-DP-PER-005: Retry policy handles an exception type the library wraps

Severity: major · Applies to: service, library, template · Enforced by: manual · Status: active

Symptom

A database login failure on a cold start is fatal in CI, although the code retries.

Cause

The retry policy handles SqlException, but the migration library wraps connection failures in its own exception type, so the retries never run.

Check

Retry policies keyed on a low-level exception type around a library call that may wrap it.

Test

A unit test that gives the policy the exception the library actually throws, and asserts a retry.

CS-ERR-009, CS-ERR-006

Sessions, cascades and queries

CS-DP-PER-006: Test session bypasses the unit of work and turns an insert into an update

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Symptom

A persistence test fails with StaleStateException: actual row count: 0, which looks like a production bug.

Cause

For assigned ids, an interceptor that only the unit-of-work factory adds decides between insert and update. A session opened directly, or a cascading Save instead of the Merge production uses, falls back to the unsaved-value check and issues an UPDATE for a new row.

Check

Persistence tests that open sessions directly, or that call Save where production goes through the unit of work or Merge.

Test

Drive object-graph and save tests through the same unit-of-work path that production uses.

CS-PER-009, CS-PER-006

CS-DP-PER-007: Remove-then-add re-parent deletes the child

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Symptom

Moving a child to another parent returns 500 or loses the child.

Cause

With AllDeleteOrphan on an inverse collection, removing the child from the old collection marks it as an orphan, and NHibernate deletes it, even though the same unit of work adds it to another parent.

Check

Remove from one parent's collection followed by an add to another's, under a delete-orphan cascade.

Test

An API-level scenario that moves a child and reads it back under the new parent.

CS-PER-008

CS-DP-PER-008: Multi-word search matched per column never finds a full name

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Symptom

Searching for "Jane Smith" returns nothing, while "Jane" or "Smith" alone finds the customer.

Cause

The search predicate tests the whole string against each column separately, and a full name spans two columns.

Check

Predicates of the form First.Contains(text) || Last.Contains(text) || … in search specifications. There are often twin specifications, for example with and without a key filter, that must change together.

Test

A search test for each input the search box label promises, such as a full name in both orders, a single word and an id, using a record read from the data first.

CS-PER-011, CS-UI-019

CS-DP-PER-009: SQL echo and lazy collections make list reads linear per row

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Symptom

A list endpoint's latency grows linearly with the page size while the database is idle.

Cause

The mapping touches several lazy collections for each row, which issues one query per row and collection, and show_sql writes every statement synchronously to the console.

Check

  • show_sql or format_sql set to true in an ORM configuration that a deployed environment loads.
  • Mappings to view models that walk lazy collections without batch fetching.

Test

A performance test that asserts the number of queries per page stays constant as the page size grows.

CS-PER-012, CS-CFG-004