Skip to content

Commit

Permalink
implement IDictionaryEnumerator to make sure that it can be serialize…
Browse files Browse the repository at this point in the history
…d/deserialized properly
  • Loading branch information
testn authored and lanwin committed Oct 25, 2010
1 parent 3a3d3c4 commit 84ab5fa
Showing 1 changed file with 80 additions and 28 deletions.
108 changes: 80 additions & 28 deletions source/MongoDB/Document.cs
Expand Up @@ -22,16 +22,16 @@ public class Document : IDictionary<string,object>, IDictionary, IXmlSerializabl
/// <summary>
/// Initializes a new instance of the <see cref="Document"/> class.
/// </summary>
public Document(){
public Document(){
_dictionary = new Dictionary<string, object>();
_orderedKeys = new List<String>();
}

/// <summary>
/// Initialize a new instance of the <see cref="Document"/> class with an optional key sorter.

/// <summary>
/// Initialize a new instance of the <see cref="Document"/> class with an optional key sorter.
/// </summary>
public Document(IComparer<string> keyComparer)
:this()
public Document(IComparer<string> keyComparer)
:this()
{
if(keyComparer == null)
throw new ArgumentNullException("keyComparer");
Expand Down Expand Up @@ -431,17 +431,17 @@ void IDictionary.Remove(object key)
/// Copies to items to destinationDocument.
/// </summary>
/// <param name="destinationDocument">The destination document.</param>
public void CopyTo(Document destinationDocument){
if(destinationDocument == null)
throw new ArgumentNullException("destinationDocument");

public void CopyTo(Document destinationDocument){
if(destinationDocument == null)
throw new ArgumentNullException("destinationDocument");

//Todo: Fix any accidental reordering issues.

foreach(var key in _orderedKeys){
if(destinationDocument.ContainsKey(key))
destinationDocument.Remove(key);
destinationDocument[key] = this[key];
}
foreach(var key in _orderedKeys){
if(destinationDocument.ContainsKey(key))
destinationDocument.Remove(key);
destinationDocument[key] = this[key];
}
}
/// <summary>
/// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
Expand Down Expand Up @@ -614,25 +614,77 @@ bool IDictionary.IsFixedSize
}
return hash;
}

/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
IEnumerator IEnumerable.GetEnumerator(){
return GetEnumerator();
}

/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
IEnumerator IEnumerable.GetEnumerator(){
return GetEnumerator();
}

/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
/// </returns>
public IEnumerator<KeyValuePair<string, object>> GetEnumerator(){
return _orderedKeys.Select(orderedKey => new KeyValuePair<string, object>(orderedKey, _dictionary[orderedKey])).GetEnumerator();
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
return new Enumerator(this);
}

private class Enumerator : IEnumerator<KeyValuePair<string, object>>, IDictionaryEnumerator
{
private readonly Document _doc;
private readonly IEnumerator<string> _keysEnum;
public Enumerator(Document doc)
{
_doc = doc;
_keysEnum = doc._orderedKeys.GetEnumerator();
}

public void Dispose()
{
_keysEnum.Dispose();
}

public bool MoveNext()
{
return _keysEnum.MoveNext();
}

public void Reset()
{
_keysEnum.Reset();
}

public KeyValuePair<string, object> Current
{
get { return new KeyValuePair<string, object>(_keysEnum.Current, _doc[_keysEnum.Current]); }
}

object IEnumerator.Current
{
get { return Current; }
}

public object Key
{
get { return _keysEnum.Current; }
}

public object Value
{
get { return _doc[_keysEnum.Current]; }
}

public DictionaryEntry Entry
{
get { return new DictionaryEntry(_keysEnum.Current, _doc[_keysEnum.Current]); }
}
}

/// <summary>
Expand Down Expand Up @@ -723,4 +775,4 @@ void IXmlSerializable.WriteXml(XmlWriter writer)
}
}
}
}
}

0 comments on commit 84ab5fa

Please sign in to comment.