I’m designing a domain model where some entities evolve over time, and I need to keep a historical record of their state that can be retrieved later.
A simplified example:
A Credit Line represents a long-term financing contract.
Each Credit Line has related entities such as Financial Entities that participate in the credit line.
The state of a Credit Line at a given date is represented by a Statement, which includes its conditions, participating entities, and their contributions (all of that can change over time).
Statements are periodically uploaded from files, and the application must allow users to view the Credit Line state as of any date.
I can’t use SQL temporal tables because the statement date comes from the uploaded file, not from the database change time.
I’m unsure how to best model and persist these time-dependent entities. Possible approaches:
Option A — Keep only the Snapshots
Persist only `CreditLineSnapshot` (Statements).
Each snapshot contains all attributes of a Credit Line at a given date. No data is repeated.
Disadvantages:
- No persistent `CreditLine` entity: must group snapshots by ID to get the list of credit lines for example.
- All relationship with other entities are duplicated in every snapshot, like the owner of a credit line, even if it does not change over time.
Option B — Persist Entities and Snapshots
Persist both CreditLine and its Statement (which contains a CreditLineSnapshot).
Disadvantages:
- Attributes and data duplication: current state exists in both the entity and its last statement (as all statements should be saved)
Option C — Persist Entities with stable attributes + Snapshots with variable data
Persist `CreditLine` with only stable attributes (e.g. Id), and store all variable data in its `Statements`.
Credit Line contains all methods but just ID attribute and its statements. It delegates in the proper statement to get the rest of the data and make all the calculus.
This is my favorite option.
What’s the best modeling strategy or common practice for this kind of domains, where entities have evolving state over time but must also keep historical versions?