简体   繁体   English

MongoDB 自定义序列化程序未设置属性

[英]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.

相关问题 MongoDb 自定义集合序列化器 - MongoDb custom collection serializer 为MongoDB自定义序列化器添加序列化信息 - Adding serialization information for MongoDB custom serializer 设置自定义控件属性 - Setting custom control properties PUN 2 获取和设置自定义属性 - PUN 2 Getting and Setting Custom Properties 定制Json Serializer,通过忽略类属性来序列化和反序列化所有属性 - Custom Json Serializer to serialize and deserialize all properties by ignoring the class attributes MongoDB驱动程序无法在通过ID过滤期间将自定义序列化程序转换为IBsonSerializer - MongoDB driver unable cast custom serializer to IBsonSerializer during filter by Id 如何结合使用LINQ和自定义序列化器? (MONGODB C#) - How to use/enable LINQ combined with custom serializer? (MONGODB C#) 自定义序列化程序,可为MongoDb处理可为空和不能为空的类型 - Custom serializer that processes both nullable and not nullable types for MongoDb 在 MongoDB 中为嵌套的 object 使用自定义序列化程序会干扰使用该 object 的过滤器 - Using a custom serializer in MongoDB for a nested object interferes with filters using that object 从样式XAML设置自定义控件属性 - Setting custom control properties from Style XAML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM