Class ReadModelCollectionsNeverNullConvention
- Namespace
- Cratis.Arc.MongoDB
- Assembly
- Cratis.Arc.MongoDB.dll
Represents a MongoDB.Bson.Serialization.Conventions.IMemberMapConvention that materializes a read model's declared non-nullable collection members as empty collections rather than null.
public class ReadModelCollectionsNeverNullConvention : ConventionBase, IMemberMapConvention, IConvention
- Inheritance
-
ConventionBaseReadModelCollectionsNeverNullConvention
- Implements
-
IMemberMapConventionIConvention
- Inherited Members
-
ConventionBase.Name
- Extension Methods
Remarks
A read model that declares a child collection as a non-nullable IEnumerable<T> promises the type system
that the value is there. A store is free to disagree: Chronicle's read model sink writes no field at all for a child
collection that has never had an element, and can write an explicit null for one whose last element went away.
Either way the driver hands back null for a member nullable analysis has already concluded can
never be null, so an unguarded .Any() or .Select() throws where nothing warned it might.
Chronicle closes this for its own reader through a JsonTypeInfo modifier on the client's serializer options.
That fix is confined to Chronicle's serialization boundary and never reaches the MongoDB driver — which is the
sanctioned path for reading a read model by anything other than its key, and the one Arc's own server-side paging
runs on. This convention is the same guarantee, restated where the driver can honor it.
Only members the declaration says cannot be null are touched. A member declared IEnumerable<T>? keeps
the distinction between "no collection" and "an empty collection", because that model asked for it. Dictionaries are
left alone — an absent map is not the same defect — and string is excluded despite being an
IEnumerable. A member that carries its own MongoDB.Bson.Serialization.Attributes.BsonDefaultValueAttribute has stated what it
wants and is skipped entirely.
The convention is registered under ReadModelCollectionsNeverNull and scoped to
[ReadModel] types, so it can be turned off per type with
[IgnoreConventions(ConventionPacks.ReadModelCollectionsNeverNull)].
Constructors
ReadModelCollectionsNeverNullConvention()
public ReadModelCollectionsNeverNullConvention()
Methods
Apply(BsonMemberMap)
Applies the convention to a member map, giving a declared non-nullable collection an empty value for both an absent element and a stored null.
public void Apply(BsonMemberMap memberMap)
Parameters
memberMapBsonMemberMapThe MongoDB.Bson.Serialization.BsonMemberMap to apply to.