我正在尝试将protobuf net对象插入到我的mongodb中。对象看起来像这样:
message Measurement
{
optional string _someString1 = 1 [default = ""];
optional string _someString2 = 2 [default = ""];
repeated MyMessage1 _myMessages1 = 3;
repeated MyMessage2 _myMessages2 = 4;
repeated MyMessage3 _myMessages3 = 5;
}
MyMessage1..3
是同一个proto文件中的消息,它们包含一些字符串、双精度数和int64s。我用一些数据填充
Measurement
的实例,并尝试将其插入到我的数据库中,如下所示:var col = MyDatabase.GetCollection<Measurement>("Measurements");
col.Insert(instanceOfMeasurement);
现在,如果我检查数据库的内容,我可以看到
instanceOfMeasurement
已正确添加。但是里面重复的字段没有。在把它塞进MongoDB之前,我需要以某种方式准备一个更复杂的对象吗?好吧,下面是protobuf net创建的:
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Measurement")]
public partial class Measurement : global::ProtoBuf.IExtensible, global::System.ComponentModel.INotifyPropertyChanged
{
public Measurement() {}
private string __someString1;
[global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"_someString1", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string _SomeString1
{
get { return __someString1?? ""; }
set { __someString1 = value; OnPropertyChanged(@"_someString1"); }
}
[global::System.Xml.Serialization.XmlIgnore]
[global::System.ComponentModel.Browsable(false)]
public bool _someString1Specified
{
get { return this.__someString1 != null; }
set { if (value == (this.__someString1== null)) this.__someString1 = value ? this._someString1 : (string)null; }
}
private bool ShouldSerialize_someString1() { return _someString1Specified; }
private void Reset_someString1() { _someString1Specified = false; }
private string __someString2;
[global::ProtoBuf.ProtoMember(2, IsRequired = false, Name=@"_someString2", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string _SomeString2
{
get { return __someString2?? ""; }
set { __someString2 = value; OnPropertyChanged(@"_someString2"); }
}
[global::System.Xml.Serialization.XmlIgnore]
[global::System.ComponentModel.Browsable(false)]
public bool _someString2Specified
{
get { return this.__someString2 != null; }
set { if (value == (this.__someString2== null)) this.__someString2 = value ? this._someString2 : (string)null; }
}
private bool ShouldSerialize_someString2() { return _someString2Specified; }
private void Reset_someString2() { _someString2Specified = false; }
private readonly global::System.Collections.Generic.List<MyMessage1> __myMessages1 = new global::System.Collections.Generic.List<MyMessage1>();
[global::ProtoBuf.ProtoMember(6, Name=@"_myMessages1", DataFormat = global::ProtoBuf.DataFormat.Default)]
public global::System.Collections.Generic.List<MyMessage1> _myMessages1
{
get { return __myMessages1; }
}
private readonly global::System.Collections.Generic.List<MyMessage2> __myMessages2 = new global::System.Collections.Generic.List<MyMessage2>();
[global::ProtoBuf.ProtoMember(7, Name=@"_myMessages2", DataFormat = global::ProtoBuf.DataFormat.Default)]
public global::System.Collections.Generic.List<MyMessage2> _myMessages2
{
get { return __myMessages2; }
}
private readonly global::System.Collections.Generic.List<MyMessage3> __myMessages3 = new global::System.Collections.Generic.List<MyMessage3>();
[global::ProtoBuf.ProtoMember(8, Name=@"_myMessages3", DataFormat = global::ProtoBuf.DataFormat.Default)]
public global::System.Collections.Generic.List<MyMessage3> _myMessages3
{
get { return __myMessages3; }
}
public event global::System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{ if(PropertyChanged != null) PropertyChanged(this, new global::System.ComponentModel.PropertyChangedEventArgs(propertyName)); }
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
最佳答案
默认情况下,驱动程序不会序列化没有setter的属性。显然这包括Measurement
上的消息。
要覆盖默认行为并包含这些属性*,可以使用BsonElement
属性标记它们,或者使用RegisterClassMap
映射它们。在这种情况下,如果类是自动生成的,则使用属性是一个坏主意,因此请使用后者:
BsonClassMap.RegisterClassMap<Measurement>(m =>
{
m.MapProperty(x => x.MyMessage1);
m.MapProperty(x => x.MyMessage2);
m.MapProperty(x => x.MyMessage3);
});
在Serialize Documents with the C# Driver: Opt In
*当readonly属性被序列化时,它的值将被持久化到数据库中,但永远不会被读出。这对于存储“计算”属性很有用