Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does storing a Nancy.DynamicDictionary in RavenDB only save the property-names and not the property-values?
    primarykey
    data
    text
    <p>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 <code>Request.Form</code>. </p> <p>If I save a straightforward instance of a <code>dynamic</code> object (with test properties and values) then everything works and both the property names and values are saved. However, if I use Nancy's <code>Request.Form</code> then only the dynamic property names are saved.</p> <p>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. </p> <p>Here is the entire test request and code:</p> <p><strong>Fiddler Request (PUT)</strong> <img src="https://i.stack.imgur.com/T9TQI.png" alt="enter image description here"></p> <p><strong>Nancy Module</strong></p> <pre><code>Put["/report/{name}/add"] = parameters =&gt; { reportService.AddTestDynamic(Db, parameters.name, Request.Form); return HttpStatusCode.Created; }; </code></pre> <p><strong>Service</strong></p> <pre><code>public void AddTestDynamic(IDocumentSession db, string name, dynamic data) { var testDynamic = new TestDynamic { Name = name, Data = data }; db.Store(testDynamic); db.SaveChanges(); } </code></pre> <p><strong>TestDynamic Class</strong></p> <pre><code>public class TestDynamic { public string Name; public dynamic Data; } </code></pre> <p><strong>Dynamic contents of Request.Form at runtime</strong> <img src="https://i.stack.imgur.com/csbjn.png" alt="enter image description here"></p> <p><strong>Resulting RavenDB Document</strong></p> <pre><code>{ "Name": "test", "Data": [ "username", "age" ] } </code></pre> <p><strong>Note</strong>: The type of the Request.Form is <code>Nancy.DynamicDictionary</code>. I think this may be the problem since it inherits from <code>IEnumerable&lt;string&gt;</code> and not the expected <code>IEnumerable&lt;string, object&gt;</code>. I think that RavenDB is enumerating the <code>DynamicDictionary</code> and only getting back the dynamic member-names rather than the member name / value pairs.</p> <p>Can anybody tell me how or whether I can treat the Request.Form as a <code>dynamic</code> object with respect to saving it to RavenDB? If possible I want to avoid any hand-crafted enumeration of <code>DynamicDictionary</code> to build a <code>dynamic</code> instance so that RavenDB can serialise correctly. </p> <p>Thank You</p> <p><strong>Edit 1</strong> @Ayende</p> <p>The DynamicDictionary appears to implement the <code>GetDynamicMemberNames()</code> method:</p> <p><img src="https://i.stack.imgur.com/ybY2r.png" alt="Nancy.DynamicDictionary"></p> <p>Taking a look at the code on GitHub reveals the following implementation:</p> <pre><code>public override IEnumerable&lt;string&gt; GetDynamicMemberNames() { return dictionary.Keys; } </code></pre> <p>Is this what you would expect to see here? </p> <p><strong>Edit 2</strong> @TheCodeJunkie</p> <p>Thanks for the code update. To test this I have:</p> <ol> <li>Created a local clone of the NancyFx/Nancy master branch from GitHub</li> <li>Added the Nancy.csproj to my solution and referenced the project</li> <li>Run the same test as above</li> </ol> <p><strong>RavenDB Document from new DynamicDictionary</strong></p> <pre><code>{ "Name": "test", "Data": { "$type": "Nancy.DynamicDictionary, Nancy", "username": {}, "age": {} } } </code></pre> <p>You can see that the resulting document is an improvement. The <code>DynamicDictionary</code> 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.</p> <p>The image below shows the new look <code>DynamicDictionary</code> 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).</p> <p><strong>Contents of DynamicDictionary at run time</strong> <img src="https://i.stack.imgur.com/f7PP4.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload