示例#1
0
        /// <summary>
        ///     Loads all modules that are discoverable by provided <paramref name="moduleDiscovery"/>,
        ///     and are not filtered out by provided <see cref="IModuleFilter"/>.
        /// </summary>
        /// <remarks>
        ///     This method will try to load all modules that match requirements regardless to whether
        ///     other modules succeed to load or fail to.
        /// </remarks>
        /// <param name="moduleDiscovery">Source of all modules that can and should be loaded.</param>
        /// <exception cref="ArgumentNullException">When <paramref name="moduleDiscovery"/> is <c>null</c></exception>
        /// <exception cref="NomadCouldNotLoadModuleException">
        ///     If any of the modules fail to load for any reason, exception will be raised,
        /// and some information about the failure will be provided.
        /// </exception>
        public void LoadModules(IModuleDiscovery moduleDiscovery)
        {
            if (moduleDiscovery == null)
            {
                throw new ArgumentNullException("moduleDiscovery");
            }

            // pass to filtering
            var allModules      = moduleDiscovery.GetModules();
            var filteredModules = allModules.Where(module => _moduleFilter.Matches(module));
            var modulesToSort   = filteredModules.Union(_moduleLoader.GetLoadedModules());
            // perform fail safe dependency checking
            IEnumerable <ModuleInfo> dependencyCheckedModules;

            try
            {
                dependencyCheckedModules = _dependencyChecker.SortModules(modulesToSort);
            }
            catch (ArgumentException e)
            {
                throw new NomadCouldNotLoadModuleException("Dependency resolving failed due to arguments.", e, e.ParamName);
            }
            catch (Exception e)
            {
                throw new NomadCouldNotLoadModuleException(
                          "Unknown error during dependency resolving", e);
            }

            // perform fail safe loading
            try
            {
                //skip items which are loaded
                var modulesToLoad = dependencyCheckedModules.Where(x => filteredModules.Contains(x));
                foreach (var moduleInfo in modulesToLoad)
                {
                    LoadSingleModule(moduleInfo);
                }
            }
            catch (Exception e)
            {
                throw new NomadCouldNotLoadModuleException("Loading one of modules failed", e);
            }
        }