MongoDB:反序列化复杂对象返回NULL

时间:2017-12-04 18:48:51

标签: c# mongodb

使用C#Mongo DB Driver版本,2.4.4,我尝试反序列化一个复杂的类。它已经正确存储在MongoDB中,但是在反序列化时它是null。

这是我的数据模型:

public abstract class Document
{
    public DocumentId Id { get; private set; }

    public DocumentProperty TestProperty { get; private set; }

    protected Document() { }

    public void SetId(string value)
    {
        if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("value");
        Id = new DocumentId(value);
    }

    public void AddProperty(DocumentProperty property)
    {
        if (property == null) throw new ArgumentNullException("property");
        TestProperty = property;
    }
}

public class DocumentId
{
    public string Value { get; }

    public DocumentId(string value)
    {
        Value = value;
    }
}

public abstract class DocumentProperty
{
    public string Value { get; }

    protected DocumentProperty()
    {
    }

    protected DocumentProperty(string value)
    {
        Value = value;
    }
}

public class TitleDocumentProperty : DocumentProperty
{
    public TitleDocumentProperty() { }

    public TitleDocumentProperty(string value) : base(value)
    {
    }
}

这是我的映射代码:

public class MongoClassMapper
{
    public static void InitializeMap()
    {
        BsonClassMap.RegisterClassMap<DocumentProperty>(map =>
        {
            map.MapProperty(property => property.Value).SetDefaultValue("123");
            map.SetIsRootClass(true);
        });

        BsonClassMap.RegisterClassMap<TitleDocumentProperty>(map =>
        {
        });

        BsonClassMap.RegisterClassMap<Document>(map =>
        {
            map.MapProperty(document => document.Id);
            map.MapProperty(document => document.TestProperty);
            map.SetIsRootClass(true);
        });

    }
}

这是我从Mongo添加和检索数据的方法:

public async Task<string> Add(Document document)
    {
        await _documents.InsertOneAsync(document);
        return document.Id.Value;
    }

    public async Task<Document> Get(DocumentId id)
    {
        var mongoDocuments = await _documents.Find(document => document.Id == id)
            .ToListAsync();

        return mongoDocuments.SingleOrDefault();
    }

这是我用于测试的代码:

private static MongoCache _cache;

    static void Main(string[] args)
    {
        MongoClassMapper.InitializeMap();
        _cache = new MongoCache(ConfigurationManager.ConnectionStrings["connName"].ConnectionString, "dbName");
        string id = ItShouldCreateRecord().Result;
        var doc = ItShouldGetRecord(id).Result;
    }

    private static Task<string> ItShouldCreateRecord()
    {
        //Arrange
        Document document = new FakeDocument();
        document.SetId(Guid.NewGuid().ToString());
        document.AddProperty(new TitleDocumentProperty("this is a title"));

        //Act
        string id = _cache.Add(document).Result;

        //Assert
        //Assert.NotEmpty(id);
        return Task.FromResult(id);
    }

    private static Task<Document> ItShouldGetRecord(string id)
    {
        //Arrange

        //Act
        return _cache.Get(new DocumentId(id));

        //Assert
        //Assert.NotNull(doc);
    }
}

[BsonIgnoreExtraElements]
public class FakeDocument : Document
{
}

我预计当我从数据库中检索文档(使用_cache.Get())时,属性TestProperty将具有实际值。目前,它是NULL。

1 个答案:

答案 0 :(得分:1)

问题是Value属性上没有setter会导致MongoDB不反序列化此属性:

public abstract class DocumentProperty
{
    public string Value { get; /* no setter*/ }
}

您可以通过简单地添加setter(使用任何辅助功能设置,甚至私人作品)来解决此问题:

public abstract class DocumentProperty
{
    public string Value { get; /* private if you want */ set; }
}

我已经抓取了JIRA issuepull request来解决问题。

相关问题