Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Flexible Plugin System

, 3 Sep 2008 LGPL3
A generic plugin system used to load and manage plugins
fadd-15373.zip
trunk
dlls
xunit.dll
Examples
Plugins
ExampleApplication.Shared
Properties
ExampleApplication
Properties
ExamplePlugin.Shared
Properties
ExamplePlugin
Properties
Fadd.Globalization.Yaml
fadd.snk
Properties
Tests
fadd
Commands
Net
Tests
fadd.snk
Globalization
Tests
Logging
Plugins
Properties
Tests
Validation
using System;
using System.Collections;
using System.Collections.Generic;

namespace Fadd.Commands
{

    /// <summary>
    /// The purpose of this class is to maintain all delegates for
    /// one of following types: attributes, interfaces and classes.
    /// </summary>
    /// <remarks>
    /// These lists are used to map all commands to their subscribers.
    /// </remarks>
    internal class TypeMapping : IEnumerable<KeyValuePair<Type, List<CommandHandler>>>
    {
        private readonly Dictionary<Type, List<CommandHandler>> _mappings = new Dictionary<Type, List<CommandHandler>>();

        public List<CommandHandler> this[Type key]
        {
            get
            {
                if (_mappings.ContainsKey(key))
                    return _mappings[key];
                else
                    return null;
            }
        }

        /// <summary>
        /// Add a handler
        /// </summary>
        /// <param name="key"></param>
        /// <param name="handler"></param>
        /// <exception cref="ArgumentException">If handler have been added already.</exception>
        public void Add(Type key, CommandHandler handler)
        {
            if (!_mappings.ContainsKey(key))
                _mappings.Add(key, new List<CommandHandler>());

            if (_mappings[key].Contains(handler))
                throw new ArgumentException("Handler have already been added to: " + key.Name);

            _mappings[key].Add(handler);
        }

        public bool Remove(Type key, CommandHandler handler)
        {
            if (_mappings.ContainsKey(key))
            {
                bool res = _mappings[key].Remove(handler);
                if (_mappings[key].Count == 0)
                    _mappings.Remove(key);
                return res;
            }
            else
                return false;
        }

        public bool Contains(Type type)
        {
            return _mappings.ContainsKey(type);
        }

        #region IEnumerable<KeyValuePair<Type,CommandHandler>> Members

        ///<summary>
        ///Returns an enumerator that iterates through the collection.
        ///</summary>
        ///
        ///<returns>
        ///A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        ///</returns>
        ///<filterpriority>1</filterpriority>
        IEnumerator<KeyValuePair<Type, List<CommandHandler>>> IEnumerable<KeyValuePair<Type, List<CommandHandler>>>.
            GetEnumerator()
        {
            return _mappings.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

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

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

jgauffin
Founder Gauffin Interactive AB
Sweden Sweden
Founder of OneTrueError, a .NET service which captures, analyzes and provide possible solutions for exceptions.
 
blog | twitter
Follow on   Twitter   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 3 Sep 2008
Article Copyright 2008 by jgauffin
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid