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
[BsonRepresentation(BsonType.String)], [BsonGuidRepresentation] or [BsonDateTimeOptions]
among them, reaches that serializer as it would on the primitive, through 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 CreateUnchecked, for a collection the application alone writes. Either way, a BSON
null is refused with 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 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: 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
valueSerializer is null.
Properties
Representation
Gets the BSON type the serializer of the underlying value writes, or
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
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
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
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
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
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
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
true for Value, the one member stored.