[英]MongoDB Custom Serializer not setting properties
Whenever using a custom serializer it sets the property value correctly in the database but the property on the object remains the default value.每当使用自定义序列化程序时,它都会在数据库中正确设置属性值,但对象上的属性保持默认值。
Custom Serializer:自定义序列化程序:
public class RngSerializer : SerializerBase<int>
{
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, int value)
{
var rng = new Random().Next(0, 100);
Console.WriteLine($"Generated rng {rng}");
context.Writer.WriteInt32(rng);
}
}
The object:物体:
public class Entity
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonSerializer(typeof(RngSerializer))]
public int RngProp { get; set; }
}
The code that does the inserting:执行插入的代码:
var entity = new Entity();
collection.InsertOne(entity);
Console.WriteLine($"Inserted Id {entity.Id}, Rng {entity.RngProp}");
You can see that the serializer is called corrected, the value is also set when I check the object in the database.可以看到调用了serializer,这个值也是在我检查数据库中的对象的时候设置的。 However, the driver doesn't seem to correctly set the property.但是,驱动程序似乎没有正确设置该属性。
Generated rng 32生成的 rng 32
Inserted Id 5e4ade582c509931f4467e38, Rng 0插入的 ID 5e4ade582c509931f4467e38,Rng 0
I found a solution by triggering the Serializer before the Insert happens like this;我通过在插入发生之前触发序列化程序找到了解决方案;
var bson = entity.ToBson();
var hydrated = BsonSerializer.Deserialize<Entity>(bson);
collection.InsertOne(hydrated);
hydrated.Id = entity.Id;
And changing the Serializer accordingly to;并相应地更改序列化程序;
public class RngSerializer : SerializerBase<int>
{
#region Overrides of SerializerBase<int>
public override int Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
return context.Reader.ReadInt32();
}
#endregion
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, int value)
{
//This part is important otherwise it will call the rng twice, once when the ToBson() is called and once when inserting.
if (value == 0)
{
var rng = new Random().Next(0, 100);
Console.WriteLine($"Generated rng {rng}");
context.Writer.WriteInt32(rng);
}
else
{
context.Writer.WriteInt32(value);
}
}
}
It might not be the most elegant way with the double serialization, but it gives me the expected results.它可能不是双序列化最优雅的方式,但它给了我预期的结果。
UPDATE: pass down the value to the serializer encapsulated in a reference type.更新:将值传递给封装在引用类型中的序列化程序。
public class MyInt
{
public int RngProp { get; set; }
}
public class Entity
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonSerializer(typeof(RngSerializer))]
public MyInt MyProp { get; set; } = new MyInt();
}
public class RngSerializer : SerializerBase<MyInt>
{
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, MyInt value)
{
var rnd = new Random().Next(0, 100);
value.RngProp = rnd;
Console.WriteLine($"written rng {rnd}");
context.Writer.WriteInt32(rnd);
}
public override MyInt Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
return new MyInt
{
RngProp = context.Reader.ReadInt32()
};
}
}
var entity = new Entity();
collection.InsertOne(entity);
Console.WriteLine($"Inserted Id {entity.Id}, Rng {entity.MyProp.RngProp}");
var result = collection.FindSync(e => e.Id == entity.Id).Single();
Console.WriteLine($"Retrieved Id {entity.Id}, Rng {result.MyProp.RngProp}");
Console.ReadLine();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.