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

Class ValueObjectBsonSerializer<TSelf, TValue>

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

Stores a value object as the bare value the serializer of its underlying type writes, and reads it back through the value object's rules.

public sealed class ValueObjectBsonSerializer<TSelf, TValue> : SerializerBase<TSelf>, IBsonSerializer<TSelf>, IBsonDocumentSerializer, IBsonSerializer, IChildSerializerConfigurable, IHasRepresentationSerializer where TSelf : struct, IValueObject<TSelf, TValue>

Type Parameters​

TSelf

Value object type.

TValue

Underlying value type.

Inheritance​

object ← SerializerBase<TSelf> ← ValueObjectBsonSerializer<TSelf, TValue>

Implements​

IBsonSerializer<TSelf>, IBsonDocumentSerializer, IBsonSerializer, IChildSerializerConfigurable, IHasRepresentationSerializer

Inherited Members​

SerializerBase<TSelf>.Deserialize(BsonDeserializationContext, BsonDeserializationArgs), SerializerBase<TSelf>.Equals(object), SerializerBase<TSelf>.GetHashCode(), SerializerBase<TSelf>.Serialize(BsonSerializationContext, BsonSerializationArgs, TSelf), SerializerBase<TSelf>.ValueType, object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks​

The value goes through the serializer of TValue the serializer is built over, which takes from the registry, so a value object writes exactly what the primitive it replaces writes, under the application's conventions for , or : documents and indexes written before the type replaced the primitive stay readable. A serialization option on a member, [BsonRepresentation(BsonType.String)], [BsonGuidRepresentation] or [BsonDateTimeOptions] among them, reaches that serializer as it would on the primitive, through , on a TSelf? member and on the elements of a collection too.

A read is strict unless the serializer is trusted: the value read goes through TryCreate, which normalizes it, and one the value object refuses throws a naming the type and the rule, never the value, with the rule's code in under , where finds it through the exception the driver wraps it in. A trusted serializer reads through CreateUnchecked, for a collection the application alone writes. Either way, a BSON null is refused with , since a value object cannot hold one: a member that can be missing is a TSelf?, which the driver reads as null without asking this serializer, and a value the serializer of TValue cannot read, a BSON value of another type, one out of its range, text it does not parse, with .

A write refuses, with a carrying the rule's code the same way, an uninitialized instance, equal to the default, whose value the value object rejects: validation tells a valid zero from a refused one. Any other instance went through Create, or was read by a trusted serializer, and is written as it is, without being validated again. The driver serializes a query constant, in LINQ or Builders, and the value of an update through the same serializer, so a default instance the type rejects is refused there too. An update through Value, Set(x => x.Page.Value, 0), hands its value to the serializer of TValue alone, as a filter through Value does, and writes it unchecked.

LINQ translates x.Iban.Value as it translates x.Iban: answers Value with the field itself and the serializer of TValue, so that a method of the underlying type, StartsWith, Length, ToLower, an arithmetic operator, translates as it would on the primitive.

Constructors​

ValueObjectBsonSerializer(IBsonSerializer<TValue>, bool)​

Initializes the serializer over the serializer of the underlying value.

public ValueObjectBsonSerializer(IBsonSerializer<TValue> valueSerializer, bool trusted = false)

Parameters​

valueSerializer IBsonSerializer<TValue>

Serializer of TValue, which writes and reads the value.

trusted bool

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

Exceptions​

ArgumentNullException

valueSerializer is null.

Properties​

Representation​

Gets the BSON type the serializer of the underlying value writes, or when that serializer does not say.

public BsonType Representation { get; }

Property Value​

BsonType

Remarks​

LINQ reads it to translate a dictionary keyed by the value object, x.Totals[currency] and ContainsKey, which it accepts only for a key written as text.

Methods​

Deserialize(BsonDeserializationContext, BsonDeserializationArgs)​

Deserializes a value.

public override TSelf Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)

Parameters​

context BsonDeserializationContext

The deserialization context.

args BsonDeserializationArgs

The deserialization args.

Returns​

TSelf

A deserialized value.

Exceptions​

FormatException

The value read is a BSON null, which a value object cannot hold, a value the serializer of the underlying value cannot read, or, unless the serializer is trusted, a value the value object refuses.

BsonSerializationException

The value object is over , the value read is binary, and the serializer of has no representation to read it with.

Equals(object?)​

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)

Parameters​

obj object?

The object to compare with the current object.

Returns​

bool

true if the specified object is equal to the current object; otherwise, false.

Remarks​

Equal for the same trust over an equal serializer of the underlying value, which is how tells a second registration of the same serializer, which it ignores, from a different one, which it refuses.

GetHashCode()​

Serves as the default hash function.

public override int GetHashCode()

Returns​

int

A hash code for the current object.

Remarks​

Read off the trust alone: the driver's serializers, the serializer of the underlying value among them, all hash to the same value.

Serialize(BsonSerializationContext, BsonSerializationArgs, TSelf)​

Serializes a value.

public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TSelf value)

Parameters​

context BsonSerializationContext

The serialization context.

args BsonSerializationArgs

The serialization args.

value TSelf

The value.

Exceptions​

BsonSerializationException

value equals default(TSelf) and holds a value the value object rejects, or the value object is over and the serializer of has no representation.

TryGetMemberSerializationInfo(string, out BsonSerializationInfo)​

Answers Value with the field itself, so that LINQ translates x.Iban.Value as it does x.Iban.

public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)

Parameters​

memberName string

The member a query names.

serializationInfo BsonSerializationInfo

For Value, an empty element path, which leaves the query on the value object's own field, with the serializer of the underlying value; null otherwise.

Returns​

bool

true for Value, the one member stored.