Class ChangeSetComputor

Namespace
Cratis.Arc.Queries
Assembly
Cratis.Arc.Core.dll

Computes ChangeSet deltas between consecutive snapshots of observable collection queries.

public class ChangeSetComputor
Inheritance
ChangeSetComputor
Inherited Members
Extension Methods

Remarks

When a collection snapshot is received, this computor compares it with the previous snapshot to determine which items were Added, Replaced, or Removed since the last update.

Identity-based delta (including Replaced detection) is used when the item type exposes a property conventionally named Id (case-insensitive). Without an identity property, a JSON-hash fallback is used that only surfaces Added and Removed.

Constructors

ChangeSetComputor(JsonSerializerOptions)

Computes ChangeSet deltas between consecutive snapshots of observable collection queries.

public ChangeSetComputor(JsonSerializerOptions serializerOptions)

Parameters

serializerOptions JsonSerializerOptions

The JsonSerializerOptions used for item comparison.

Remarks

When a collection snapshot is received, this computor compares it with the previous snapshot to determine which items were Added, Replaced, or Removed since the last update.

Identity-based delta (including Replaced detection) is used when the item type exposes a property conventionally named Id (case-insensitive). Without an identity property, a JSON-hash fallback is used that only surfaces Added and Removed.

Methods

Compute(IEnumerable<object>?, IEnumerable<object>)

Computes the delta between two consecutive collection snapshots.

public ChangeSet Compute(IEnumerable<object>? previous, IEnumerable<object> current)

Parameters

previous IEnumerable<object>

The previous snapshot, or null for the first update.

current IEnumerable<object>

The current snapshot.

Returns

ChangeSet

A ChangeSet describing what changed.

Remarks

On the first call (when previous is null), all current items are reported as Added.

ComputeByJson(object[], object[])

Computes the delta using JSON-hash comparison (fallback when no identity property is found).

public ChangeSet ComputeByJson(object[] previousItems, object[] currentItems)

Parameters

previousItems object[]

The previous snapshot items.

currentItems object[]

The current snapshot items.

Returns

ChangeSet

A ChangeSet describing what changed.

Remarks

Only surfaces Added and Removed; Replaced is never emitted by this method.

ComputeFromKnownChanges(IReadOnlyList<CollectionChange>, IEnumerable<object>, IEnumerable<object>?)

Builds the delta an emission already stated, rather than rediscovering it by comparison.

public ChangeSet ComputeFromKnownChanges(IReadOnlyList<CollectionChange> changes, IEnumerable<object> current, IEnumerable<object>? previous)

Parameters

changes IReadOnlyList<CollectionChange>

The changes the emission carried.

current IEnumerable<object>

The current snapshot, used to resolve the items the changes name.

previous IEnumerable<object>

The previous snapshot, used to resolve removed items which are no longer in current.

Returns

ChangeSet

A ChangeSet describing what changed.

Remarks

An item named by a change but present in neither snapshot is skipped rather than reported as a change to nothing - it is better to under-report a delta than to emit an entry a client cannot act on.

ComputeWithIdentity(object[], object[], PropertyInfo)

Computes the delta using a discovered identity property.

public ChangeSet ComputeWithIdentity(object[] previousItems, object[] currentItems, PropertyInfo idProperty)

Parameters

previousItems object[]

The previous snapshot items.

currentItems object[]

The current snapshot items.

idProperty PropertyInfo

The property used to extract the item identity key.

Returns

ChangeSet

A ChangeSet describing what changed.

Remarks

Items with a new identity key are Added; items whose key is gone are Removed; items with the same key but a different JSON representation are Replaced.

FindIdentityProperty(Type)

Discovers the property that represents the identity of an item.

public static PropertyInfo? FindIdentityProperty(Type type)

Parameters

type Type

The item type to inspect.

Returns

PropertyInfo

The identity PropertyInfo, or null if not found.

Remarks

Looks for a property conventionally named Id (case-insensitive), including inherited interface properties. The result is cached per type, so the reflection runs once per item type rather than on every emission. The cache is cleared when Hot Reload updates types, so an identity property added while the application runs is found.