MongoDB C#将嵌入式对象序列化为扁平对象

时间:2019-06-17 07:23:13

标签: c# mongodb

我打算将所有实体保存在提供实际对象的通用元数据的相同“容器”对象中。例如:

public class Entity<T> where T : class
{
  public Guid Id { get; set; }
  public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
  //... and others...
  public T Value { get; set; }
}

public class Foo
{
  public string Name { get; set; }
  public int Age { get; set; }
  // ...and so on...
}

将按如下方式保存实体:

{
  "_id": UUID("xxxxx...xxxx"),
  "createdTime": "2019-01-01T00:00:00.000z",
  "value": {
    "name": "foo",
    "age": 42,
  }
}

我想像这样序列化(/反序列化):

{
  "_id": UUID("xxxxx...xxxx"),
  "_createdTime": "2019-01-01T00:00:00.000z",
  "name": "foo",
  "age": 42,
}

createdTime也更改为_createdTime

我不确定是否可以使用IBsonSerializerIBsonDocumentSerializer来做到这一点。

0 个答案:

没有答案