I was able to find a good solution to solve the json serialization issue š
In this solution, we do not need to store type of application context in other classes.
ā What is the solution?
In fact, instead of registering serializers in the startup, I register them in mongodb context, But in a very clean form :))
I am registering a class called BsonSerializationOptions with MongoDBContext:
public static IServiceCollection AddMongo<TMongoDbContext>(this IServiceCollection services, string connectionString, bool dropExistDatabase = false)
where TMongoDbContext : class, IMongoDBContext
{
// Other Code
services.AddSingleton(provider =>
{
var appContextType = provider.CreateScope().ServiceProvider.GetRequiredService<IApplicationContext>().GetType();
return new BsonSerializationOptions
{
Serializers = new Dictionary<Type, IBsonSerializer>
{
{ typeof(Guid), new GuidSerializer(GuidRepresentation.Standard) },
{ typeof(decimal), new DecimalSerializer(BsonType.Decimal128) },
{ typeof(decimal?), new NullableSerializer<decimal>(new DecimalSerializer(BsonType.Decimal128)) },
{ typeof(IApplicationContext), new AppContextBsonSerializer(appContextType) }
},
GuidRepresentationMode = GuidRepresentationMode.V3
};
});
return services;
}
I was able to find a good solution to solve the json serialization issue š In this solution, we do not need to store type of application context in other classes.
ā What is the solution? In fact, instead of registering serializers in the startup, I register them in mongodb context, But in a very clean form :)) I am registering a class called BsonSerializationOptions with MongoDBContext:
And then in MongoDBContext, I configure it:
I'm really a genius š