How to create an adapter for the TFS Integration Platform - Appendix 2: SimpleDictionary

Note: This post is part of a series and you can find the rest of the parts in the series index.

For my WI adapter I needed an implementation of Dictionary<T,V> which could be serialised and unfortunately the .NET one can’t. So I threw together a simple implementation of one using two List<T>. It is not perfect for every possible time you may need an alternative to Dictionary<T,V>, for example the only item manipulation I have is to add an item and clear all items, but it is great for my needs in the case:

[XmlRoot("simpleDictionary")]
public class SimpleDictionary<Key, Value> : IEnumerable, IXmlSerializable
{
    private List<Key> keys = new List<Key>();
    private List<Value> values = new List<Value>();

    public List<Key> Keys
    {
        get
        {
            return keys;
        }
    }

    public List<Value> Values
    {
        get
        {
            return values;
        }
    }

    public IEnumerator GetEnumerator()
    {
        return (IEnumerator)new SimpleDictionaryEnumerator(this);
    }

    public void Add(Key key, Value value)
    {
        keys.Add(key);
        values.Add(value);
    }

    public void Add(object o)
    {
        KeyValuePair<Key, Value>? keyValuePair = o as KeyValuePair<Key, Value>?;
        if (keyValuePair != null)
        {
            this.Add(keyValuePair.Value.Key, keyValuePair.Value.Value);
        }
    }

    public void Clear()
    {
        keys.Clear();
        values.Clear();
    }

    #endregion

    private class SimpleDictionaryEnumerator : IEnumerator
    {
        private SimpleDictionary<Key, Value> simpleDictionary;
        private int index = -1;

        public SimpleDictionaryEnumerator(SimpleDictionary<Key, Value> simpleDictionary)
        {
            this.simpleDictionary = simpleDictionary;
        }

        #region IEnumerator Members

        public object Current
        {
            get
            {
                return new KeyValuePair<Key, Value>(simpleDictionary.keys[index], simpleDictionary.values[index]);
            }
        }

        public bool MoveNext()
        {
            index++;
            return !(index >= simpleDictionary.keys.Count);

        }

        public void Reset()
        {
            index = -1;
        }
    }

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        if (reader.IsEmptyElement)
        {
            return;
        }

        XmlSerializer keySerialiser = new XmlSerializer(typeof(Key));
        XmlSerializer valueSerialiser = new XmlSerializer(typeof(Value));

        reader.Read();
        while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
        {
            reader.ReadStartElement("keyValuePair");

            reader.ReadStartElement("key");
            Key key = (Key)keySerialiser.Deserialize(reader);
            reader.ReadEndElement();

            reader.ReadStartElement("value");
            Value value = (Value)valueSerialiser.Deserialize(reader);
            reader.ReadEndElement();

            this.Add(key, value);

            reader.ReadEndElement(); // for keyvaluepair
            reader.MoveToContent();
        }

        reader.ReadEndElement(); // for root
    }

    public void WriteXml(XmlWriter writer)
    {
        XmlSerializer keySerialiser = new XmlSerializer(typeof(Key));
        XmlSerializer valueSerialiser = new XmlSerializer(typeof(Value));

        for (int counter = 0; counter < this.keys.Count; counter++)
        {
            writer.WriteStartElement("keyValuePair");

            writer.WriteStartElement("key");
            keySerialiser.Serialize(writer, this.keys[counter]);
            writer.WriteEndElement();

            writer.WriteStartElement("value");
            valueSerialiser.Serialize(writer, this.values[counter]);
            writer.WriteEndElement();

            writer.WriteEndElement();

        }
    }
}

[...] (part 1, part 2, part 3, part 4, part 5, part 6, part 7, part 8, part 9, part 10, Appendix A, and Appendix B) – This epic series of posts will probably become the must-have resource when using the TFS [...]

[...] How to create an adapter for the TFS Integration Platform - Appendix 2: SimpleDictionary | SADev sadev.co.za/content/how-create-adapter-tfs-integration-platform-appendix-2-simpledictionary – view page – cached Note: This post is part of a series and you can find the rest of the parts in the series index. For my WI adapter I needed an implementation of Dictionary which could be serialised and unfortunately the .NET one can’t. So I threw together a simple implementation of one using two List. It is not perfect for every possible time you may need an alternative to Dictionary, for example the only item... Read moreNote: This post is part of a series and you can find the rest of the parts in the series index. For my WI adapter I needed an implementation of Dictionary which could be serialised and unfortunately the .NET one can’t. So I threw together a simple implementation of one using two List. It is not perfect for every possible time you may need an alternative to Dictionary, for example the only item manipulation I have is to add an item and clear all items, but it is great for my needs in the case: [XmlRoot("simpleDictionary")] public class SimpleDictionary : IEnumerable, IXmlSerializable { private List keys = new List(); private List values = new List(); public List Keys { get { return keys; } } public List Values { get { return values; } } public IEnumerator GetEnumerator() { return (IEnumerator)new SimpleDictionaryEnumerator(this); } public void Add(Key key, Value value) { keys.Add(key); values.Add(value); } public void Add(object o) { KeyValuePair? keyValuePair = o as KeyValuePair?; if (keyValuePair != null) { this.Add(keyValuePair.Value.Key, keyValuePair.Value.Value); } } public void Clear() { keys.Clear(); values.Clear(); } #endregion private class SimpleDictionaryEnumerator : IEnumerator { private SimpleDictionary simpleDictionary; private int index = -1; public SimpleDictionaryEnumerator(SimpleDictionary simpleDictionary) { this.simpleDictionary = simpleDictionary; } #region IEnumerator Members public object Current { get { return new KeyValuePair(simpleDictionary.keys[index], simpleDictionary.values[index]); } } public bool MoveNext() { index++; return !(index >= simpleDictionary.keys.Count); } public void Reset() { index = -1; } } public System.Xml.Schema.XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { if (reader.IsEmptyElement) { return; } XmlSerializer keySerialiser = new XmlSerializer(typeof(Key)); XmlSerializer valueSerialiser = new XmlSerializer(typeof(Value)); reader.Read(); while (reader.NodeType != System.Xml.XmlNodeType.EndElement) { reader.ReadStartElement("keyValuePair"); reader.ReadStartElement("key"); Key key = (Key)keySerialiser.Deserialize(reader); reader.ReadEndElement(); reader.ReadStartElement("value"); Value value = (Value)valueSerialiser.Deserialize(reader); reader.ReadEndElement(); this.Add(key, value); reader.ReadEndElement(); // for keyvaluepair reader.MoveToContent(); } reader.ReadEndElement(); // for root } public void WriteXml(XmlWriter writer) { XmlSerializer keySerialiser = new XmlSerializer(typeof(Key)); XmlSerializer valueSerialiser = new XmlSerializer(typeof(Value)); for (int counter = 0; counter < this.keys.Count; counter++) { writer.WriteStartElement("keyValuePair"); writer.WriteStartElement("key"); keySerialiser.Serialize(writer, this.keys[counter]); writer.WriteEndElement(); writer.WriteStartElement("value"); valueSerialiser.Serialize(writer, this.values[counter]); writer.WriteEndElement(); writer.WriteEndElement(); } } }, How to create an adapter for the TFS Integration Platform - Appendix 2: SimpleDictionary View page Tweets about this link Topsy.Data.Twitter.User['rmaclean'] = {"photo":"http://a3.twimg.com/profile_images/414282781/IMG_A224_normal.png","url":"http://twitter.com/rmaclean","nick":"rmaclean"}; rmaclean: “How to create an adapter for the TFS Integration Platform - Appendix 2: SimpleDictionary http://goo.gl/fb/bWjFJ ” 10 minutes ago retweet Topsy.Data.Twitter.User['rmaclean'] = {"photo":"http://a3.twimg.com/profile_images/414282781/IMG_A224_normal.png","url":"http://twitter.com/rmaclean","nick":"rmaclean"}; rmaclean: “New blog post: How to create an adapter for the TFS Integration Platform - Appendix 2: SimpleDictionary : http://bit.ly/bKRWOU ” 1 hour ago retweet Filter tweets [...]

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Syntax highlight code surrounded by the <pre class="brush: lang">...</pre> tags, where lang is one of the following language brushes: as3, applescript, bash, csharp, coldfusion, cpp, css, delphi, diff, erlang, groovy, jscript, java, javafx, perl, php, plain, powershell, python, ruby, sass, scala, sql, vb, xml.

More information about formatting options