序列化 MongoDb 上的只读属性
Serialize get-only properties on MongoDb
使用 C# 6 我可以写:
public class Person
{
public Guid Id { get; }
public string Name { get; }
public Person(Guid id, string name)
{
Id = id;
Name = name;
}
}
不幸的是,像这样的 class 没有被 MongoDb 驱动程序正确序列化,属性没有被序列化。
MongoDb 仅在默认情况下使用 getter 和 setter 序列化属性。我知道您可以手动更改 class 映射并将序列化程序告诉 include get-only properties 但我一直在寻找一种通用方法来避免自定义每个映射。
我想创建一个类似于 ReadWriteMemberFinderConvention 但没有 CanWrite
检查的自定义约定。
还有其他解决办法吗?构造函数会被自动调用还是我需要一些其他的定制?
更新:MongoDB.Bson 2.10 版现在带有 ImmutableTypeClassMapConvention
我试图通过创建一个约定来解决这个问题,该约定映射所有与构造函数匹配的只读属性以及匹配的构造函数。
假设您有一个不可变的 class,例如:
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;
}
}
您可以使用以下方式注册我:
ConventionRegistry.Register(
nameof(ImmutablePocoConvention),
new ConventionPack { new ImmutablePocoConvention() },
_ => true);
如果您不想序列化所有 read-only 属性,您可以添加一个 public 设置什么都不做(如果适用),只需注意 属性 将是re-evaluated 当你的 class 是 de-serialized。
public class Person
{
public string FirstName { get; }
public string LastName { get; }
public string FullName
{
get
{
return FirstName + LastName;
}
set
{
//this will switch on the serialization
}
}
}
我遇到了同样的问题,发现接受的答案过于复杂。
相反,您可以简单地将 BsonRepresentation 属性添加到要序列化的 read-only 属性:
public class Person
{
public string FirstName { get; }
public string LastName { get; }
[BsonRepresentation(BsonType.String)]
public string FullName => $"{FirstName} {LastName}";
}
假设您有一个不可变的 class,例如:
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}";
}
使用 C# 6 我可以写:
public class Person
{
public Guid Id { get; }
public string Name { get; }
public Person(Guid id, string name)
{
Id = id;
Name = name;
}
}
不幸的是,像这样的 class 没有被 MongoDb 驱动程序正确序列化,属性没有被序列化。
MongoDb 仅在默认情况下使用 getter 和 setter 序列化属性。我知道您可以手动更改 class 映射并将序列化程序告诉 include get-only properties 但我一直在寻找一种通用方法来避免自定义每个映射。
我想创建一个类似于 ReadWriteMemberFinderConvention 但没有 CanWrite
检查的自定义约定。
还有其他解决办法吗?构造函数会被自动调用还是我需要一些其他的定制?
更新:MongoDB.Bson 2.10 版现在带有 ImmutableTypeClassMapConvention
我试图通过创建一个约定来解决这个问题,该约定映射所有与构造函数匹配的只读属性以及匹配的构造函数。
假设您有一个不可变的 class,例如:
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;
}
}
您可以使用以下方式注册我:
ConventionRegistry.Register(
nameof(ImmutablePocoConvention),
new ConventionPack { new ImmutablePocoConvention() },
_ => true);
如果您不想序列化所有 read-only 属性,您可以添加一个 public 设置什么都不做(如果适用),只需注意 属性 将是re-evaluated 当你的 class 是 de-serialized。
public class Person
{
public string FirstName { get; }
public string LastName { get; }
public string FullName
{
get
{
return FirstName + LastName;
}
set
{
//this will switch on the serialization
}
}
}
我遇到了同样的问题,发现接受的答案过于复杂。
相反,您可以简单地将 BsonRepresentation 属性添加到要序列化的 read-only 属性:
public class Person
{
public string FirstName { get; }
public string LastName { get; }
[BsonRepresentation(BsonType.String)]
public string FullName => $"{FirstName} {LastName}";
}
假设您有一个不可变的 class,例如:
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}";
}