The dynamic dictionary is just a ExpandoObject with a Dictionary in it. So it can still be accessed like a dictionary.
For example, in MVC you access Form properties like so:
var name = Request["name"];
or
var name = Request.Form["name"];
When a request comes into Nancy you can access it via the dot notation. Or via the class indexer.
var name = parameters.name;
var name = parameters["name"];
This is handy when you're sending query string or form names that have values that cannot be used in dot notation.
var firstName = parameters["first-name"];
The values are also dynamic, so it could be made up of nested objects. This allows you to do stuff like:
var firstName = parameters.contact.firstname;
So if you're passing a JSON payload to the request then you can access the entire structure using dot notation.
However you will probably find most developers using Nancy only ever access Route values or QueryString values using this method.
Get["/products/{id:int}/"] = parameters => {
int id = parameters.id;
};
So back to the original question:
Is there a blog post or any doco: Nope.
Why does it exist: For sugar syntax.
Can I use it for what I want: Yes absolutely!
Can you tell me how to use it: Nope, however it shouldn't be hard. Just look the model binding in Nancy to figure it out. It's not too hard.
Just an edit based on the answer by the OP.
When you access the dot notation, continued dot notation will only work on further dynamic types.
This means using var
will cause an exception because of the way var
and dynamic
are handled by the compiler.
When you do:
var person = parameters.person;
var name = person.name;
parameters
is currently dynamic
and implements TryGetMember
, this internally looks up a dictionary of values and attempts to return the value.
When you define the object as var
for the person
variable. The compiler assumes that anything after that exists on the object, so it looks for name
on the person
variable.
Since name
does not exist as a member of person
it will throw.
To resolve this, the variable must be assigned as dynamic
. So the example becomes:
dynamic person = parameters.person;
var name = person.name;
This will work.