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.