Skip to main content
Version: 0.3.x (0.3.0)

Class EntityIdBson

Namespace: AdCodicem.ValueObjects.Identifiers.MongoDB
Assembly: AdCodicem.ValueObjects.Identifiers.MongoDB.dll

Registers the id generators of entity identifiers with MongoDB.Driver, so that a document inserted without its identifier is given a new one.

public static class EntityIdBson

Inheritance​

object ← EntityIdBson

Inherited Members​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks​

Call it at start-up, beside ValueObjectBson.Register, which the identifier's serializer still comes from, and before any class map of a document holding an identifier is built: the driver gives a class map's id member the generator registered for its type when it builds the class map, and never looks again.

BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
ValueObjectBson.Register(typeof(AccountId).Assembly);
EntityIdBson.Register(typeof(AccountId).Assembly);

await accounts.InsertOneAsync(new Account { Name = "Ada" }); // account.Id is now acc_…

Methods​

Register(params Assembly[])​

Registers the generator of every entity identifier registered, those the assemblies declare included, unless the driver holds one for it already.

[RequiresUnreferencedCode("Locates the generated registration of each assembly given by its metadata.")]
public static void Register(params Assembly[] assemblies)

Parameters​

assemblies Assembly[]

Assemblies declaring entity identifiers, whose generated registration runs first, so that their identifiers are registered.

Remarks​

Each generator is closed over its identifier at compile time, through the type the descriptor hands back to a visitor (). A generator of the application's own, registered first, is kept; the checker the driver gives a type that has none, under or , is not one. Locating the generated registration of an assembly given by name reads its metadata, which trimming may remove.

Exceptions​

ArgumentNullException

assemblies is null.

Register<TId>()​

Registers the generator of one entity identifier, unless the driver holds one for it already.

public static void Register<TId>() where TId : struct, IEntityId<TId>

Type Parameters​

TId

The identifier type.

Remarks​

A second call does nothing, and a generator of the application's own, registered first, is kept. The checker the driver gives a type that has no generator, under or , is not one of the application's: it would refuse, or let through, the default instance rather than mint an identifier, so it is replaced. Closed at compile time, the registration needs no reflection.