Performance implications on using too many convention packs


In my current project there is a need to store many different data types as string within a MongoDB Atlas document. As an example, the DateTime, Integer, Enums, Guids all should be stored as their string representations within the document itself.

I found the solution using ConventionBase and then adding a bunch of custom conventions, like Integer to String, Date Time to String; using out of the box conventions like EnumRepresentationConvention and using the GuidSerializer.

Here is a sample of my code which is working well:

 //Represent Enum values as string in MongoDB document
            var pack = new ConventionPack
                new EnumRepresentationConvention(BsonType.String)

            //Represent integer values as string in MongoDB Document
            //Without this the values will show up as "PropertyName":{"$numberInt":"PropertyIntValue"}, eg. "Age":{"$numberInt":"77"}

            var intToStringConventionPack = new ConventionPack
                new IntegerToStringRepresentationConvention(BsonType.String)

            ConventionRegistry.Register("EnumStringConvention", pack, t => true);
            ConventionRegistry.Register("IntStringConvention", intToStringConventionPack, t => true);

            BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));

However, I am worried that this might slow the interaction with database down.

Is there a way to understand how much slowness this extra customization could bring or, are there any guidelines on how many convention packs/ serializers could be used safely?

Any suggestion would be greatly appreciated.


Hi, @UB_K,

I would not expect it to have an appreciable effect since conventions are used internally as well. For example, the DefaultConventionPack and AttributeConventionPack are automatically registered in the ConventionRegistry. Unless your custom convention pack is doing something expensive when iterating conventions (e.g. making network requests to determine which conventions to apply - which would be odd), I wouldn’t expect having multiple convention packs to have a measurable impact. As with anything performance related, the best recommendation is to benchmark your particular use case.