序列化MongoDb上的get-only属性

ct3nt3jp  于 2023-01-16  发布在  Go
关注(0)|答案(5)|浏览(112)

用C# 6我可以写:

public class Person
{
    public Guid Id { get; }
    public string Name { get; }
    public Person(Guid id, string name)
    {
        Id = id;
        Name = name;
    }
}

不幸的是,MongoDb驱动程序无法正确序列化此类,属性无法序列化。
MongoDb只使用getter和setter序列化默认属性,我知道你可以手动改变类Map,告诉序列化器包含get-only属性,但是我一直在寻找一种通用的方法来避免定制每个Map。
我想创建一个类似于ReadWriteMemberFinderConvention的自定义约定,但不使用CanWrite检查。
有其他的解决方案吗?构造函数将被自动调用还是我需要一些其他的定制?

x0fgdtte

x0fgdtte1#

更新:MongoDB.bson2.10版现在提供了一个不可变的TypeClassMapConvention
我试图通过创建一个约定来解决这个问题,该约定Map所有与构造函数匹配的只读属性以及匹配的构造函数。
假设您有一个不可变的类,如下所示:

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

下面是大会的代码:

using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

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

yhuiod9q2#

我也遇到了同样的问题,发现公认的答案过于复杂。
相反,您只需将BsonRepresentation特性添加到要序列化的只读属性:

public class Person
{
    public string FirstName { get; }

    public string LastName { get; }

    [BsonRepresentation(BsonType.String)]
    public string FullName => $"{FirstName} {LastName}";
}
ajsxfq5m

ajsxfq5m3#

如果你不希望所有的只读属性都被序列化,你可以添加一个公共集合,不做任何事情(如果适用的话),只要注意当你的类被反序列化时,这个属性将被重新计算。

public class Person
{
    public string FirstName { get; }
    public string LastName { get; }
    public string FullName
    {
        get
        {
            return FirstName + LastName;
        }
        [Obsolete("Reminder to not use set method")]
        internal set
        {
           //this will switch on the serialization
        }
    }
}
ubby3x7f

ubby3x7f4#

假设您有一个不可变的类,如下所示:

public class Person
{
    public string FirstName { get; }

    public string LastName { get; }

    [BsonRepresentation(BsonType.String)]
    public string FullName => $"{FirstName} {LastName}";
}

只需添加[BsonElement]即可

public class Person
{
    [BsonElement]
    public string FirstName { get; }

    [BsonElement]
    public string LastName { get; }

    public string FullName => $"{FirstName} {LastName}";
}
krugob8w

krugob8w5#

这是对上述注册公约后的简化。
应制定这一公约,

public class ReadOnlyMemberFinderConvention : ConventionBase, IClassMapConvention
{
    public void Apply(BsonClassMap classMap)
    {
        var readOnlyProperties = classMap.ClassType.GetTypeInfo()
            .GetProperties()
            .Where(p => p.CanRead && !p.CanWrite)
            .ToList();
        
        readOnlyProperties.ForEach(p => classMap.MapProperty(p.Name));
    }
}

然后注册

ConventionRegistry.Register(
            "ReadOnlyMemberFinderConvention",
            new ConventionPack { new ReadOnlyMemberFinderConvention() },
            _ => true);

相关问题