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

Class ValueObjectBson

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

Registers the MongoDB.Driver serializers of value objects.

public static class ValueObjectBson

Inheritance​

object ← ValueObjectBson

Inherited Members​

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

Remarks​

MongoDB.Driver keeps its serializers in a process-wide registry that never forgets one, and maps a type it has no serializer for through a class map. A value object has no member a class map can set, so without these serializers it is written as an empty sub-document, {}, read back as its default instance, and a query over it compares {}, matching every document or none. Call once at start-up, after registering any serializer of the application's own, the first, and before anything is serialized or any class map is built.

Methods​

Register(params Assembly[])​

Registers, once, the provider that hands the driver a strict serializer for every value object.

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

Parameters​

assemblies Assembly[]

Assemblies declaring value objects, whose generated registration runs first, so that their value objects are checked.

Remarks​

Strict, as with trusted: false is: every value read goes through the value object's rules, since a collection has no schema and is often written by other services, scripts or tools.

Exceptions​

ArgumentNullException

assemblies is null.

InvalidOperationException

The provider is already registered with trusted: true; a value object over is registered and the serializer of has no representation; or the driver has already mapped a registered value object through a class map.

Register(bool, params Assembly[])​

Registers, once, the provider that hands the driver a serializer for every value object, trusted or strict.

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

Parameters​

trusted bool

Whether reads skip validation, through CreateUnchecked, for a collection the application alone writes; false reads through TryCreate, and refuses a value the value object rejects.

assemblies Assembly[]

Assemblies declaring value objects, whose generated registration runs first, so that their value objects are checked.

Remarks​

The provider answers every value object the driver meets, whichever assembly declares it, with a over the serializer the registry holds for its underlying type, closed at compile time through the type arguments the descriptor hands back to a visitor (). It is registered once: a second call with the same trust registers the assemblies given and repeats the checks below, one with the other trust throws. A value object that needs the other trust, or a serializer of the application's own, is given it per type with or , or per member, on its class map, before the driver first asks for it.

Two mistakes are refused here rather than on the first document. A value object over , with the driver's default , whose representation is unspecified, would throw on the first write: register new GuidSerializer(GuidRepresentation.Standard) before calling this method. And a value object the driver has already serialized, before this call, keeps the class map it was given, which writes {}, for the rest of the process: the exception names the value objects concerned, and the registration has to move before whatever serialized them, a construction of a generic value object and a value object written by hand included. The check covers the value objects the registry holds when the method runs; the provider repeats it for one it meets later, when it writes one and when it reads a binary value.

A value object over or , for which MongoDB.Bson has no serializer, is refused with a when it is written or read, unless the application registered a serializer of its own for the underlying type, which it then goes through.

A construction of a generic value object, a value object written by hand, or one in an assembly whose registration has not run, that nothing registered, is described by reflection the first time the driver asks for it (), which only the JIT can run; registers one without reflection. Locating the generated registration of an assembly given by name reads its metadata, which trimming may remove. MongoDB.Driver itself maps documents through class maps it builds by reflection and is not compatible with trimming or native AOT.

Exceptions​

ArgumentNullException

assemblies is null.

InvalidOperationException

The provider is already registered with the other trust; a value object over is registered and the serializer of has no representation; or the driver has already mapped a registered value object through a class map.

Register<TSelf, TValue>(bool)​

Registers the serializer of one value object, such as a construction of a generic value object.

public static void Register<TSelf, TValue>(bool trusted = false) where TSelf : struct, IValueObject<TSelf, TValue>

Parameters​

trusted bool

Whether reads skip validation, for a collection the application alone writes; false, the default, reads through the value object's rules.

Type Parameters​

TSelf

Value object type, Code<Order> for instance.

TValue

Underlying value type.

Remarks​

The serializer is the one hands the driver, registered with , so a second call with the same trust, or a call for a value object the provider already served with that trust, does nothing. Closed at compile time, it needs no reflection and no dynamic code to be built.

Exceptions​

NotSupportedException

TValue is or , MongoDB.Bson has no serializer for it, and the application registered none of its own.

InvalidOperationException

TValue is and the serializer of has no representation.

BsonSerializationException

The driver already holds a different serializer for TSelf: one of the other trust, one of the application's own, or the class map it gave a value object it serialized before any registration.