使用Mongo C# Driver序列化不可变值类型

4
我有许多不可变值类型的类,例如EmailAddress,它们确保任何非空实例都是有效的。我想控制这些类型的对象序列化,使其在使用MongoDB C# Driver持久化时只是标准的字符串表示形式("123@abc.com")。
我尝试实现IBsonSerilizer,但它只允许在根级别使用对象或数组。我能够使用Json.NET实现正确的Json序列化,是否应该采取不同的方法?
3个回答

6
我假设您指的是像这样的EmailAddress类:

我假设您指的是像这样的EmailAddress类:

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

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

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

我使用了一个属性来将EmailAddress类与自定义序列化程序联系起来,它可以实现如下所示:

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。例如:
public class Person
{
    public int Id { get; set; }
    public EmailAddress EmailAddress { get; set; }
}

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

以下是参考代码:

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

生成的JSON/BSON文档如下:
{ "_id" : 1, "EmailAddress" : "joe@xyz.com" }

1
感谢您花时间纠正我。您是正确的,在我的单元测试中,我试图直接序列化它,这就是为什么我遇到了这个错误,我已经调整了测试,将一个具有此类型属性的父对象进行序列化。 - Gent
这个可以用序列化来实现,但是如何使用Linq查找具有特定电子邮件的人?Mongo在“Value”属性上抛出异常,因为它不存在。 - Marcos Junior

3

我阅读了@Davide Icardi的答案后,发现MongoDB内置了约定来使用不可变对象。

只需注册该约定即可。

ConventionRegistry.Register(nameof(ImmutableTypeClassMapConvention), 
new ConventionPack { new ImmutableTypeClassMapConvention()}, type => true);

2
区别在于ImmutableTypeClassMapConvention只有在构造函数中实际存在所有只读属性时才起作用。如果您有任何未作为构造函数参数出现的属性,例如Davide示例中的“FullName”属性,则它将无法工作。 - adhominem

2

我尝试通过创建一个公约来解决这个问题,该公约映射所有匹配构造函数和只读属性。

假设你有一个类像这样:

public class Person
{
    public string FirstName { get; }
    public string LastName { get; }
    public string FullName => FirstName + LastName;
    public ImmutablePocoSample(string lastName)
    {
        LastName = lastName;
    }

    public ImmutablePocoSample(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
}

这里是惯例的代码:
/// <summary>
/// A convention that map all read only properties for which a matching constructor is found.
/// Also matching constructors are mapped.
/// </summary>
public class ImmutablePocoConvention : ConventionBase, IClassMapConvention
{
    private readonly BindingFlags _bindingFlags;

    public ImmutablePocoConvention()
            : this(BindingFlags.Instance | BindingFlags.Public)
    { }

    public ImmutablePocoConvention(BindingFlags bindingFlags)
    {
        _bindingFlags = bindingFlags | BindingFlags.DeclaredOnly;
    }

    public void Apply(BsonClassMap classMap)
    {
        var readOnlyProperties = classMap.ClassType.GetTypeInfo()
            .GetProperties(_bindingFlags)
            .Where(p => IsReadOnlyProperty(classMap, p))
            .ToList();

        foreach (var constructor in classMap.ClassType.GetConstructors())
        {
            // If we found a matching constructor then we map it and all the readonly properties
            var matchProperties = GetMatchingProperties(constructor, readOnlyProperties);
            if (matchProperties.Any())
            {
                // Map constructor
                classMap.MapConstructor(constructor);

                // Map properties
                foreach (var p in matchProperties)
                    classMap.MapMember(p);
            }
        }
    }

    private static List<PropertyInfo> GetMatchingProperties(ConstructorInfo constructor, List<PropertyInfo> properties)
    {
        var matchProperties = new List<PropertyInfo>();

        var ctorParameters = constructor.GetParameters();
        foreach (var ctorParameter in ctorParameters)
        {
            var matchProperty = properties.FirstOrDefault(p => ParameterMatchProperty(ctorParameter, p));
            if (matchProperty == null)
                return new List<PropertyInfo>();

            matchProperties.Add(matchProperty);
        }

        return matchProperties;
    }


    private static bool ParameterMatchProperty(ParameterInfo parameter, PropertyInfo property)
    {
        return string.Equals(property.Name, parameter.Name, System.StringComparison.InvariantCultureIgnoreCase)
               && parameter.ParameterType == property.PropertyType;
    }

    private static bool IsReadOnlyProperty(BsonClassMap classMap, PropertyInfo propertyInfo)
    {
        // we can't read 
        if (!propertyInfo.CanRead)
            return false;

        // we can write (already handled by the default convention...)
        if (propertyInfo.CanWrite)
            return false;

        // skip indexers
        if (propertyInfo.GetIndexParameters().Length != 0)
            return false;

        // skip overridden properties (they are already included by the base class)
        var getMethodInfo = propertyInfo.GetMethod;
        if (getMethodInfo.IsVirtual && getMethodInfo.GetBaseDefinition().DeclaringType != classMap.ClassType)
            return false;

        return true;
    }
}

您可以使用以下方式注册 i :
ConventionRegistry.Register(
    nameof(ImmutablePocoConvention),
    new ConventionPack { new ImmutablePocoConvention() },
    _ => true);

太棒了!按照预期工作,这应该是被接受的答案! - alsami

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接