【问题标题】:Serializing Immutable Value types with Mongo C# Driver使用 Mongo C# 驱动程序序列化不可变值类型
【发布时间】:2018-05-10 17:14:13
【问题描述】:

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

我已经尝试实现IBsonSerilizer,但是它只允许在根级别使用对象或数组。我能够使用 Json.NET 实现正确的 Json 序列化,我应该采取不同的方法吗?

【问题讨论】:

    标签: c# mongodb serialization bson mongodb-.net-driver


    【解决方案1】:

    我假设您的意思是像这样的 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 序列化为根文档(因为它不是文档...)。但是您可以使用嵌入在其他文档中的电子邮件地址。例如:

    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" }
    

    【讨论】:

    • 感谢您抽出宝贵时间让我明白。你的权利,在我的单元测试中我试图直接序列化它,这就是我遇到这个错误的原因,我调整了测试以序列化具有这种类型属性的父对象。
    • 这适用于序列化,但如何使用 Linq 搜索具有特定电子邮件的人员? Mongo 在“Value”属性上抛出异常,因为它不存在。
    【解决方案2】:

    在阅读了@Davide Icardi的答案后,我发现convention 中内置了使用不可变对象的功能。

    只需注册约定

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

    【讨论】:

    • 不同之处在于 ImmutableTypeClassMapConvention 仅在构造函数中实际存在所有只读属性时才有效。如果您有任何不作为构造函数参数存在的属性,它将不起作用,例如 Davide 示例中的“FullName”属性
    【解决方案3】:

    我试图通过创建一个映射所有与构造函数以及匹配的构造函数匹配的只读属性的约定来解决此问题。

    假设你有这样一个类:

    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);
    

    【讨论】:

    • 太棒了!按需要工作,这应该是公认的答案!
    猜你喜欢
    • 2011-10-05
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多