Currently we have a specification object which contain a property like “TrimName”. TrimName is another object typed “NormalizedString” which contains two properties like below.
public class NormalizedString
{
public string Value { get; init; }
public string NormalizedValue { get; init; }
}
We are trying to filter with the “Value” property by using IQueryable like so →
System.NotSupportedException: Serializer for Domain.ValueObjects.NormalizedString does not represent members as fields.
at MongoDB.Driver.Linq.Linq3Implementation.Misc.DocumentSerializerHelper.GetMemberSerializationInfo(IBsonSerializer serializer, String memberName)
Note that we are also using a custom serializer for our NormalizedString class. The serialize or Deserialize method doesn’t get trigger at this stage but it is correctly registered using IBsonSerializer? GetSerializer(Type type) method
Is this some kind of limitation or we are missing something?
Thank you!
When writing a custom serializer that stores types as subdocuments, then you must implement IBsonDocumentSerializer as a well as IBsonDocument<T>. IBsonDocumentSerializer provides a single method TryGetMemberSerializationInfo, which allows the lookup of a serializer from a property name. In this case, it is responsible for returning the StringSerializer for NormalizedString.Value.
Note that if you never accessed NormalizedString.Value and treated NormalizedString as a scalar type (e.g. x => x.TrimValue == new NormalizedString("something"), then your serializer would not need to implement IBsonDocumentSerializer. It is only because you are accessing properties of NormalizedString within your lambda expression that the driver requires a way to locate BsonSerializationInfo for its properties.