Module enumerator for loading modules statically and dynamically

Oct 24, 2008 at 10:03 PM
I needed to load modules statically and dynamically for an application. I implemented this aggregate enumerator which can handle any loader that implements the IModuleEnumerator interface. It also provides a generic method to retrieve modules for the specified type of enumerator. One limitation with this class currently is it does nothing to manage dependencies between loader types. I hope someone finds this useful.

    public class AggregateModuleEnumerator : IModuleEnumerator
    {
        #region Fields

        List<IModuleEnumerator> enumerators;

        #endregion

        #region Constructors

        public AggregateModuleEnumerator(IModuleEnumerator[] enumerators)
        {
            this.enumerators = new List<IModuleEnumerator>(enumerators);
        }
       
        public AggregateModuleEnumerator()
        {
            this.enumerators = new List<IModuleEnumerator>();
        }

        #endregion

        #region IModuleEnumerator Members

        /// <summary>
        /// Gets the metadata information of a module by its name in the specified path.
        /// </summary>
        /// <param name="moduleName">The module's name.</param>
        /// <returns>A <see cref="ModuleInfo"/> associated with the <paramref name="moduleName"/> parameter.</returns>
        public ModuleInfo GetModule(string moduleName)
        {
            return ( from e in enumerators from m in e.GetModules() where m.ModuleName == moduleName select m).FirstOrDefault();
        }

        /// <summary>
        /// Gets a list of metadata information of the modules in the specified path.
        /// </summary>
        /// <returns>An array of <see cref="ModuleInfo"/>.</returns>
        public ModuleInfo[] GetModules()
        {
            return ( from e in enumerators from m in e.GetModules() select m).ToArray();
        }

        /// <summary>
        /// Gets a list of metadata information of the modules that should be loaded at startup.
        /// </summary>
        /// <returns>An array of <see cref="ModuleInfo"/>.</returns>
        public ModuleInfo[] GetStartupLoadedModules()
        {
            return ( from e in enumerators from m in e.GetModules() where m.StartupLoaded == true select m).ToArray();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Adds the module enumerator.
        /// </summary>
        /// <param name="enumerator">The enumerator.</param>
        public void AddModuleEnumerator(IModuleEnumerator enumerator)
        {
            enumerators.Add(enumerator);
        }

        /// <summary>
        /// Gets the modules for the specified enumerator type.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public ModuleInfo[] GetModules<T>() where T : class
        {
            return (from e in enumerators where e is T from m in e.GetModules() select m).ToArray();
        }

        #endregion
    }