使用Mongo C#驱动程序序列化不可变值类型 [英] Serializing Immutable Value types with Mongo C# Driver

查看:75
本文介绍了使用Mongo C#驱动程序序列化不可变值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多不可变的值类型类,例如EmailAddress,它们确保任何非null实例都是有效的.当我使用MongoDB C#驱动程序持久化时,我想将这些类型的对象的序列化控制为标准字符串表示形式("123@abc.com").

I have many immutable value type classes, for example EmailAddress, which ensure any non null instance is valid. I would like to control the serialization of these types of objects to be just the standard string representation ("123@abc.com") when persisted using MongoDB C# Driver.

我尝试实现IBsonSerilizer,但是它仅允许在根级别使用对象或数组.我能够使用Json.NET实施适当的Json Serilization,我应该采取其他方法吗?

I have tried implementing the IBsonSerilizer however it will only allow for objects or arrays at the root level. I was able to implement proper Json Serilization with Json.NET, is there a different approach I should be taking?

推荐答案

我假设您是指类似以下电子邮件地址类:

I assume you mean an EmailAddress class something like this:

[BsonSerializer(typeof(EmailAddressSerializer))]
public class EmailAddress
{
    private string _value;

    public EmailAddress(string value)
    {
        _value = value;
    }

    public string Value
    {
        get { return _value; }
    }
}

我使用了一个属性将EmailAddress类链接到自定义序列化程序,该序列化程序可以这样实现:

I've used an attribute to link the EmailAddress class to a custom serializer, which could be implemented like this:

public class EmailAddressSerializer : BsonBaseSerializer
{
    public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
    {
        if (bsonReader.GetCurrentBsonType() == BsonType.Null)
        {
            bsonReader.ReadNull();
            return null;
        }
        else
        {
            var value = bsonReader.ReadString();
            return new EmailAddress(value);
        }
    }

    public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
    {
        if (value == null)
        {
            bsonWriter.WriteNull();
        }
        else
        {
            var emailAddress = (EmailAddress)value;
            bsonWriter.WriteString(emailAddress.Value);
        }
    }
}

您不能将EmailAddress序列化为根文档(因为它不是文档...).但是您可以使用嵌入在其他一些文档中的EmailAddress.例如:

You can't serialize an EmailAddress as the root document (because it's not a document...). But you could use an EmailAddress embedded in some other document. For example:

public class Person
{
    public int Id { get; set; }
    public EmailAddress EmailAddress { get; set; }
}

您可以使用以下代码进行测试:

Which you could test using code like the following:

var person = new Person { Id = 1, EmailAddress = new EmailAddress("joe@xyz.com") };
var json = person.ToJson();
var rehyrdated = BsonSerializer.Deserialize<Person>(json);

生成的JSON/BSON文档为:

The resulting JSON/BSON document is:

{ "_id" : 1, "EmailAddress" : "joe@xyz.com" }

这篇关于使用Mongo C#驱动程序序列化不可变值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆