为什么在RavenDB中存储Nancy.DynamicDictionary仅保存属性名称而不保存属性值? [英] Why does storing a Nancy.DynamicDictionary in RavenDB only save the property-names and not the property-values?

查看:103
本文介绍了为什么在RavenDB中存储Nancy.DynamicDictionary仅保存属性名称而不保存属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图保存(RavenDB build 960)通过其内置的Request.Form传递到Nancy Module中的表单数据项的名称和值.

I am trying to save (RavenDB build 960) the names and values of form data items passed into a Nancy Module via its built in Request.Form.

如果我保存一个dynamic对象的简单实例(具有测试属性和值),则一切正常,并且属性名称和值均被保存.但是,如果我使用Nancy的Request.Form,则仅保存动态属性名称.

If I save a straightforward instance of a dynamic object (with test properties and values) then everything works and both the property names and values are saved. However, if I use Nancy's Request.Form then only the dynamic property names are saved.

我知道在检索动态数据(RavenJObjects等)时,我将不得不处理与还原正确类型有关的其他问题,但现在,我想解决的是在第一个中保存动态名称/值的问题.地方.

I understand that I will have to deal with further issues to do with restoring the correct types when retrieving the dynamic data (RavenJObjects etc) but for now, I want to solve the problem of saving the dynamic names / values in the first place.

这是整个测试请求和代码:

Here is the entire test request and code:

提琴手请求(PUT)

南希模块

Put["/report/{name}/add"] = parameters =>
    {
        reportService.AddTestDynamic(Db, parameters.name, Request.Form);
        return HttpStatusCode.Created;
    };

服务

public void AddTestDynamic(IDocumentSession db, string name, dynamic data)
{
    var testDynamic = new TestDynamic
    {
            Name = name,
            Data = data
    };
    db.Store(testDynamic);
    db.SaveChanges();
}

TestDynamic类

public class TestDynamic
{
    public string Name;
    public dynamic Data;
}

运行时Request.Form的动态内容

生成的RavenDB文档

{
  "Name": "test",
  "Data": [
    "username",
    "age"
  ]
}

注意:Request.Form的类型为Nancy.DynamicDictionary.我认为这可能是问题所在,因为它继承自IEnumerable<string>,而不是预期的IEnumerable<string, object>.我认为RavenDB正在枚举DynamicDictionary,并且仅获取动态成员名称,而不是成员名称/值对.

Note: The type of the Request.Form is Nancy.DynamicDictionary. I think this may be the problem since it inherits from IEnumerable<string> and not the expected IEnumerable<string, object>. I think that RavenDB is enumerating the DynamicDictionary and only getting back the dynamic member-names rather than the member name / value pairs.

有人可以告诉我如何或是否可以将Request.Form视为将其保存到RavenDB的dynamic对象吗?如果可能的话,我想避免使用DynamicDictionary的任何手工枚举来构建dynamic实例,以便RavenDB可以正确地序列化.

Can anybody tell me how or whether I can treat the Request.Form as a dynamic object with respect to saving it to RavenDB? If possible I want to avoid any hand-crafted enumeration of DynamicDictionary to build a dynamic instance so that RavenDB can serialise correctly.

谢谢

编辑1 @Ayende

DynamicDictionary似乎实现了GetDynamicMemberNames()方法:

The DynamicDictionary appears to implement the GetDynamicMemberNames() method:

看看GitHub上的代码,就会发现以下实现:

Taking a look at the code on GitHub reveals the following implementation:

public override IEnumerable<string> GetDynamicMemberNames()
{
    return dictionary.Keys;
}

这是您期望在这里看到的吗?

Is this what you would expect to see here?

编辑2 @TheCodeJunkie

Edit 2 @TheCodeJunkie

感谢代码更新.要测试这个,我有:

Thanks for the code update. To test this I have:

  1. 从以下位置创建NancyFx/Nancy master分支的本地克隆 GitHub
  2. 将Nancy.csproj添加到我的解决方案中,并引用了该项目
  3. 运行与上述相同的测试
  1. Created a local clone of the NancyFx/Nancy master branch from GitHub
  2. Added the Nancy.csproj to my solution and referenced the project
  3. Run the same test as above

新DynamicDictionary中的RavenDB文档

{
  "Name": "test",
  "Data": {
    "$type": "Nancy.DynamicDictionary, Nancy",
    "username": {},
    "age": {}
  }
}

您可以看到生成的文档是一项改进.现在,RavenDB可以正确地获取DynamicDictionary类型信息,并且可以正确地序列化动态属性名称,但是不幸的是,动态属性值却不能.

You can see that the resulting document is an improvement. The DynamicDictionary type information is now being correctly picked up by RavenDB and whilst the dynamic property-names are correctly serialized, unfortunately the dynamic property-values are not.

下图显示了运行中的新外观DynamicDictionary.在我看来,这一切都很好,新的Dictionary界面清晰可见.我唯一注意到的是调试器中的动态结果视图"(与动态视图"相对)仅显示属性名称,而不显示其值. 动态视图"与以前一样显示(请参见上图).

The image below shows the new look DynamicDictionary in action. It all looks fine to me, the new Dictionary interface is clearly visible. The only thing I noticed was that the dynamic 'Results view' (as opposed to the 'Dynamic view') in the debugger, shows just the property-names and not their values. The 'Dynamic view' shows both as before (see image above).

运行时DynamicDictionary的内容

推荐答案

生物分形, 问题是DynamicDictionary,在JSON中,类型可以是对象或列表,但不能同时是两者. 对于动态对象序列化,我们依靠GetDynamicMemberNames()的实现来获取属性,而我认为那是不存在的.

biofractal, The problem is the DynamicDictionary, in JSON, types can be either objects or lists ,they can't be both. And for dynamic object serialization, we rely on the implementation of GetDynamicMemberNames() to get the properties, and I assume that is isn't there.

这篇关于为什么在RavenDB中存储Nancy.DynamicDictionary仅保存属性名称而不保存属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆