Class IdentityBindResult
- Namespace
- Excalibur.Data.IdentityMap
- Assembly
- Excalibur.Data.IdentityMap.dll
Represents the result of a conditional identity bind operation.
public sealed record IdentityBindResult : IEquatable<IdentityBindResult>
- Inheritance
-
IdentityBindResult
- Implements
- Inherited Members
Remarks
A binding is a RESERVATION of an identity, never an assertion that the aggregate exists. The mapping and the aggregate's event stream are two writes to two stores, and nothing makes them atomic. So MappingWasCreated answers "did I win the race to reserve this identity", which is a question about this call. It does not answer "does a stream exist for this aggregate", which is a question about a different store at a different time.
Do not use it as the insert-versus-update discriminator. That reading is wrong in one direction
and it is permanent. If anything interrupts between the bind and the append — cancellation at a
processing deadline, a crash, an eviction, a transport fault — the mapping is committed and the stream
never arrives. Every later call then returns MappingWasCreated = false for that external
id, an update path that loads the aggregate finds nothing, and the record fails forever. It cannot
self-heal, because the only branch that would have created the stream is the one the flag no longer
selects.
Branch on whether the STREAM exists. Load the aggregate and treat "bound but absent" as the insert case — that makes the sequence correct at every interruption point and needs no compensating delete, which could not be relied upon anyway: an unbind in a failure path only runs if the process survives to run it, and it races a concurrent writer that has already resolved the same mapping. MappingWasCreated remains useful for what it actually reports — telling a first sighting from a repeat, emitting a created-versus-updated metric, logging.
var bind = await identityMap.TryBindAsync(system, externalId, "Client", NewId(), cancellationToken);
var id = Guid.Parse(bind.AggregateId);
// The stream, not the flag, decides. A reservation whose append never landed lands here and recovers.
var client = await repository.GetByIdAsync(id, cancellationToken);
if (client is null)
{
await InsertAsync(id, data, cancellationToken);
}
else
{
await UpdateAsync(client, data, cancellationToken);
}
Constructors
IdentityBindResult(string, bool)
Represents the result of a conditional identity bind operation.
public IdentityBindResult(string AggregateId, bool MappingWasCreated)
Parameters
AggregateIdstringThe aggregate ID (existing or newly bound).
MappingWasCreatedbooltrue if a new MAPPING was created; false if an existing mapping was returned. This describes the mapping and nothing else — see the remarks before branching on it.
Remarks
A binding is a RESERVATION of an identity, never an assertion that the aggregate exists. The mapping and the aggregate's event stream are two writes to two stores, and nothing makes them atomic. So MappingWasCreated answers "did I win the race to reserve this identity", which is a question about this call. It does not answer "does a stream exist for this aggregate", which is a question about a different store at a different time.
Do not use it as the insert-versus-update discriminator. That reading is wrong in one direction
and it is permanent. If anything interrupts between the bind and the append — cancellation at a
processing deadline, a crash, an eviction, a transport fault — the mapping is committed and the stream
never arrives. Every later call then returns MappingWasCreated = false for that external
id, an update path that loads the aggregate finds nothing, and the record fails forever. It cannot
self-heal, because the only branch that would have created the stream is the one the flag no longer
selects.
Branch on whether the STREAM exists. Load the aggregate and treat "bound but absent" as the insert case — that makes the sequence correct at every interruption point and needs no compensating delete, which could not be relied upon anyway: an unbind in a failure path only runs if the process survives to run it, and it races a concurrent writer that has already resolved the same mapping. MappingWasCreated remains useful for what it actually reports — telling a first sighting from a repeat, emitting a created-versus-updated metric, logging.
var bind = await identityMap.TryBindAsync(system, externalId, "Client", NewId(), cancellationToken);
var id = Guid.Parse(bind.AggregateId);
// The stream, not the flag, decides. A reservation whose append never landed lands here and recovers.
var client = await repository.GetByIdAsync(id, cancellationToken);
if (client is null)
{
await InsertAsync(id, data, cancellationToken);
}
else
{
await UpdateAsync(client, data, cancellationToken);
}
Properties
AggregateId
The aggregate ID (existing or newly bound).
public string AggregateId { get; init; }
Property Value
MappingWasCreated
true if a new MAPPING was created; false if an existing mapping was returned. This describes the mapping and nothing else — see the remarks before branching on it.
public bool MappingWasCreated { get; init; }