示例#1
0
        /// <summary>
        /// Create service dependency graph from the specified array of service definitions.
        /// </summary>
        /// <param name="services">Array of services.</param>
        /// <returns>
        /// A <see cref="ServiceDependencyGraph"/> instance.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// If <paramref name="services"/> is <c>null</c>.
        /// </exception>
        public static ServiceDependencyGraph Create(IEnumerable <IServiceInstaller> services)
        {
            if (services == null)
            {
                throw new ArgumentNullException("services");
            }

            // Generate nodes along with mapping between definition and nodes.
            var map = services.ToDictionary(x => x.TargetService, x => new Node(x));

            // Form edges between nodes.
            foreach (var node in map.Values)
            {
                foreach (var dependencyDescriptor in node.Installer.GetDependencies())
                {
                    Node dependencyNode;
                    if (map.TryGetValue(dependencyDescriptor, out dependencyNode))
                    {
                        node.Targets.Add(dependencyNode);
                        dependencyNode.Inputs.Add(node);
                    }
                }
            }

            var graph = new ServiceDependencyGraph();

            graph.Nodes = new List <Node>(map.Values);
            return(graph);
        }
        /// <summary>
        /// Sort the specified services by their dependencies so that root-most services
        /// occur first in resulting array.
        /// </summary>
        /// <param name="installers">Collection of service installers.</param>
        /// <returns>
        /// Results of sort; check the value of <see cref="Results.HasCircularDependency"/>
        /// to determine whether sort was successful or unsuccessful.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// If <paramref name="installers"/> is <c>null</c>.
        /// </exception>
        public Results SortDependencies(IEnumerable <IServiceInstaller> installers)
        {
            if (installers == null)
            {
                throw new ArgumentNullException("installers");
            }

            var graph      = ServiceDependencyGraph.Create(installers);
            var graphNodes = new HashSet <ServiceDependencyGraph.Node>(graph.Nodes);

            return(SortDependencies(graphNodes));
        }