Skip to content

Core Data Types

Purpose

Core .NET library providing the Virtufin data model: Entity-Attribute-Value framework, Variant discriminated union, FlatBuffers serialization, and reactive observables for NATS, WebSocket, and Valkey data sources.

Requirements

Requirement: Entity-Attribute-Value Model

The library SHALL provide a registry-based EAV framework where entities own named attributes whose values are provided by pluggable value providers.

Scenario: Entity lifecycle

  • WHEN an entity is created with a unique identifier
  • THEN it SHALL be registerable in a global registry and support adding attributes of any type

Scenario: Value observation

  • WHEN an attribute value changes
  • THEN value providers SHALL notify subscribers reactively

Requirement: Variant Discriminated Union

The library SHALL provide a sealed Variant type representing a value that is exactly one of: None, Bool, Byte, Int16, Int32, Int64, Float, Double, String, a typed array of any of the preceding primitive kinds, a nested array of Variant (ArrayVariant), or Dictionary. It SHALL support FlatBuffers serialization and implicit conversions from C# primitives.

Scenario: Type-safe construction

  • WHEN a Variant is constructed from a C# primitive (int, string, bool, etc.)
  • THEN the type SHALL be determined at construction and retrievable via pattern matching

Scenario: FlatBuffers serialization

  • WHEN a Variant is serialized to FlatBuffers binary
  • THEN it SHALL deserialize to an equivalent Variant with the same value and type

Scenario: Implicit conversions

  • WHEN a Variant is used where a string or int is expected
  • THEN the implicit conversion SHALL succeed if the underlying type matches, or throw if it does not

Requirement: Reactive Observables

The library SHALL provide IObservable<T> implementations for NATS, WebSocket, and Valkey data sources. Observables SHALL auto-reconnect on connection loss.

Scenario: NATS subscription

  • WHEN a NATS observable is subscribed to with a subject pattern
  • THEN each message published on matching subjects SHALL be delivered to all subscribers

Scenario: Connection lifecycle

  • WHEN the underlying connection to a data source is lost
  • THEN the observable SHALL reconnect automatically and resume message delivery

Requirement: Financial Domain Types

The library SHALL provide financial domain primitives including Currency (the full ISO 4217 currency code set, not limited to any subset) and Cash.

Scenario: Cash arithmetic

  • WHEN two Cash values with the same currency are added
  • THEN the result SHALL be a Cash with the summed amount and same currency

Requirement: Currency Mismatch Rejection

Operations on mismatched currencies SHALL be rejected.

Note (2026-07-26): Cash currently has no arithmetic operators at all (+, -, equality) — the mismatch-rejection behavior described by "Operations on mismatched currencies SHALL be rejected" has no implementation yet to test against. This requirement describes the intended contract, not current behavior.

Scenario: Currency mismatch

  • WHEN two Cash values with different currencies are combined
  • THEN the operation SHALL reject with a clear error

Requirement: FlatBuffers Wire Format

The library SHALL define a FlatBuffers schema for the Variant type and generate serialization code at build time. All language SDKs SHOULD use this schema for cross-language data interchange.

Scenario: Cross-language compatibility

  • WHEN a Variant is serialized by a C# producer
  • THEN a Python or TypeScript consumer using the same FlatBuffers schema SHALL be able to deserialize it correctly

Scenario: Build-time code generation

  • WHEN the project is built
  • THEN the FlatBuffers compiler (flatc) SHALL generate C# serialization code before compilation begins

Requirement: Decimal Wire Type

Every payload schema SHALL carry a decimal value as lib's DecimalAmount in cardinal form, value = cardinal / 10^precision with a precision of 0 to 18: protobuf virtufin.core.DecimalAmount { sint64 cardinal; uint32 precision; } or its FlatBuffers twin. A quantity with a unit SHALL be virtufin.core.DecimalQuantity { DecimalAmount amount; string unit; }.

Scenario: Exact round trip

  • WHEN a DecimalAmount is written to a payload and read back
  • THEN the value and its precision SHALL be the same, without rounding

Requirement: Shared Precision in a Message

A message whose decimal values share one precision SHALL either carry each value as a DecimalAmount or carry the precision once, as its own field, and the values as bare cardinals; the devkit owning the schema chooses.

Scenario: An order book's prices

  • WHEN many values of a message have one precision (the prices of an order book)
  • THEN the message MAY carry the precision once and each value as its cardinal

Requirement: No Floating Point or Text Decimals

No payload schema SHALL carry money, prices or quantities as floating point or as decimal text.

Scenario: A new schema with a price

  • WHEN a devkit defines a payload schema with a price
  • THEN the price SHALL be a DecimalAmount (or a cardinal under a shared precision), never a double or a string

Requirement: Decimal Amount in Every Language

lib SHALL provide DecimalAmount for C#, Python and TypeScript, each converting to and from the decimal wire type and to the language's exact decimal (C# decimal and DecimalAmount, Python decimal.Decimal, TypeScript a bigint cardinal with its precision). Shared conformance vectors SHALL pin the same cardinal, precision and text for the same value in all three languages.

Scenario: Same value everywhere

  • WHEN the conformance vectors run in C#, Python and TypeScript
  • THEN every vector SHALL give the same cardinal, precision and text in each language

Requirement: Combining Amounts

A consumer that adds or compares amounts that arrived at different precisions (a venue's prices, carried at the venue's own precision) SHALL first bring each to one working precision without losing a digit, with lib's DecimalAmount lossless rescale (C# AtPrecision, Python at_precision, TypeScript atPrecision). An amount with a non-zero digit beyond the working precision SHALL fail visibly, never be rounded silently.

Scenario: Closes at two precisions

  • WHEN a moving average receives closes 42000.5 and 42000.55 and works at 8 decimals
  • THEN it SHALL add 42000.50000000 and 42000.55000000

Scenario: More decimals than the consumer works at

  • WHEN a consumer working at 8 decimals receives 0.123456789
  • THEN the event SHALL fail with an error naming the amount, and nothing is rounded