Can RavenDB handle dictionaries with complex keys/values?

571 views
Skip to first unread message

Andrew Stephens

unread,
Nov 15, 2013, 4:22:21 AM11/15/13
to rav...@googlegroups.com

I'm using properties of type Dictionary<,> in a number of classes within my document hierarchy. The dictionaries use complex types for the key, the value, or sometimes both (these types exist elsewhere in the document hierarchy, and I also mark them with "[JsonObject(IsReference=True)]").

I'm able to save the document fine, but when I try to load it I get a JsonSerializationException, which points to these properties being the problem (they also don't look "right" when I look at the raw JSON). Is RavenDB unable to serialize such Dictionary types?

Here is an example of where I might use such a dictionary:-

public class Experiment
{
public string Id {get;set;
public Collection<Chemical> Chemicals {get; set;}
public Collection<Sample> Samples {get; set;}
...
}

public class Chemical
{
...
}

public class Sample
{
public Dictionary<Chemical, double> Results {get; set;}
...
}


"Experiment" is the document root, and is used to store the results of chemical analysis of samples. An Experiment defines one or more Samples (the test tubes), and one or more Chemicals to look for in those samples. The Sample's results are stored in a dictionary, keyed on the Chemical object.

I used Dictionaries to make it faster (and easier) to retrieve a given Chemical's result (in the real application's document, there are many other classes and collections involved, resulting in many tens of thousands of results that have to be processed very quickly).

As an alternative to a Dictionary, what about:-

public class Sample
{
public List<SampleResult> Results {get; set;}
...
}

public class SampleResult
{
public Chemical Chemical {get; set;}
public double Result {get; set;}
}


Is this a more preferable solution? Obviously it's going to be slower to locate a particular Chemical's results as I'll have to use a LINQ query.

Oren Eini (Ayende Rahien)

unread,
Nov 15, 2013, 5:48:01 AM11/15/13
to ravendb
No, there is not way for us to represent a complex type as a dictionary key.

Oren Eini
CEO
Hibernating Rhinos
Office:    +972-4-674-7811
Fax:       +972-153-4622-7811





--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Greg Fiske

unread,
Jan 3, 2014, 12:49:16 PM1/3/14
to rav...@googlegroups.com
I actually was able to do this by adding the  [JsonObjectAttribute] and [TypeConverter(typeof(MyConverter))] attributes to my "Key" class and creating the MyConverter class which converts the class to/from a string (I was able to define a human-readable standard for that).  The keys stored in Raven are strings and they hydrate properly in my model. 

Chris Marisic

unread,
Jan 6, 2014, 8:55:30 AM1/6/14
to rav...@googlegroups.com
FWIW you can also consider List<KeyValuePair<x,y>>

Oren Eini (Ayende Rahien)

unread,
Jan 6, 2014, 12:50:34 PM1/6/14
to ravendb
that would be a more jsony approach to it, yes.

Oren Eini
CEO
Hibernating Rhinos
Office:    +972-4-674-7811
Fax:       +972-153-4622-7811





Greg Fiske

unread,
Jan 6, 2014, 1:49:45 PM1/6/14
to rav...@googlegroups.com
Sure, which is basically the original poster's alternate solution but then keys are not implicitly unique in the list (which was why I cared to make a dictionary object).

Greg


--
You received this message because you are subscribed to a topic in the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ravendb/j2AE4sAWlp8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ravendb+u...@googlegroups.com.

Lee

unread,
Jan 6, 2014, 6:20:35 PM1/6/14
to rav...@googlegroups.com
Another alternative is to override ToString() and implement an implicit string conversion operator.

Oren Eini (Ayende Rahien)

unread,
Jan 7, 2014, 6:07:27 AM1/7/14
to ravendb
It is rarely a good idea to try to do uniqueness on value types.

Oren Eini
CEO
Hibernating Rhinos
Cellular: +972-52-548-6969
Office:    +972-4-674-7811
Fax:       +972-153-4622-7811





Reply all
Reply to author
Forward
0 new messages