/// <summary>
        /// Builds an instance of PackageGraph using provided information.
        /// </summary>
        /// <returns><see cref="PackageGraph"/>.</returns>
        public PackageGraph Build()
        {
            var nodes = new List <PackageGraphNode>();

            foreach (var package in _packages)
            {
                var packageId = package.Key;

                var packageNode = nodes.FirstOrDefault(x => x.Package.IsSamePackage(packageId));
                if (packageNode == null)
                {
                    packageNode = new PackageGraphNode(packageId);
                    nodes.Add(packageNode);
                }

                foreach (var dependencyId in package.Value)
                {
                    var depPackageNode = nodes.FirstOrDefault(x => x.Package.IsSamePackage(dependencyId));
                    if (depPackageNode == null)
                    {
                        depPackageNode = new PackageGraphNode(dependencyId);
                        nodes.Add(depPackageNode);
                    }

                    packageNode.AddDependency(depPackageNode);
                }
            }

            return(new PackageGraph(nodes.Where(x => x.Dependents.Count == 0).ToList(), nodes.Count));
        }
 /// <summary>
 /// Adds a dependency for the node.
 /// </summary>
 /// <param name="dependency">New dependency.</param>
 public void AddDependency(PackageGraphNode dependency)
 {
     _dependencies.Add(dependency);
     dependency._dependents.Add(this);
 }