BsonIgnoreIfEmpty - Property Attribute
Like BsonIgnoreIfDefault and BsonIgnoreIfNull, we can add BsonIgnoreIfEmpty.
It can be added on IEnumerables (which we basically know they cannot have infinite values since it is inserted to db after all..) and if the IEnumerable is empty, it wont serialize it to the db BUT will know to put default value (an empty list for example) in said property.
They will decrease below boilerplate code for IEnumerable<T>:
Serialization:
Field = otherField?.count > 0 ? otherFIeld : null;
Deserialization:
otherField = Field ?? new List<T>;
Obviously, we can improve this code to also support Dictionary<Tkey, Tvalue>.
3
votes
Ori
shared this idea