forked from samus/mongodb-csharp
/
GenericSortedListDictionaryAdapter.cs
68 lines (57 loc) · 1.85 KB
/
GenericSortedListDictionaryAdapter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using MongoDB.Configuration.Mapping.Util;
namespace MongoDB.Configuration.DictionaryAdapters
{
/// <summary>
///
/// </summary>
public class GenericSortedListDictionaryAdapter<TKey, TValue> : IDictionaryAdapter
{
/// <summary>
/// Gets the type of the key.
/// </summary>
/// <value>The type of the key.</value>
public Type KeyType
{
get { return typeof(TKey); }
}
/// <summary>
/// Gets the type of the value.
/// </summary>
/// <value>The type of the value.</value>
public Type ValueType
{
get { return typeof(TValue); }
}
/// <summary>
/// Creates the dictionary.
/// </summary>
/// <param name="document">The document.</param>
/// <returns></returns>
public object CreateDictionary(Document document)
{
if(document == null)
return null;
var list = new SortedList<TKey, TValue>();
foreach(var pair in document)
list.Add((TKey)ValueConverter.Convert(pair.Key, typeof(TKey)), (TValue)pair.Value);
return list;
}
/// <summary>
/// Gets the pairs.
/// </summary>
/// <param name="dictionary">The collection.</param>
/// <returns></returns>
public Document GetDocument(object dictionary)
{
var instance = dictionary as IDictionary<TKey, TValue>;
if (instance == null)
return null;
var doc = new Document();
foreach (var e in instance)
doc.Add(ValueConverter.ConvertKey(e.Key), e.Value);
return doc;
}
}
}