Пример #1
0
        /// <summary>
        /// Adds an <see cref="object"/> and its dependencies to the <see cref="CurrentGraph"/>.
        /// </summary>
        /// <param name="value">The <see cref="object"/> to add.</param>
        /// <returns>The created <see cref="SerializeNode"/> representing <paramref name="value"/>.</returns>
        private SerializeNode GetNode(object value)
        {
            if (value != null && !TypeConfiguration.MatchAny().Match(value?.GetType()))
            {
                throw new TypeMatchException(TypeConfiguration.MatchAny(), value?.GetType());
            }

            var existing = CurrentGraph.Nodes.FirstOrDefault(n => object.ReferenceEquals(n.Value, value));

            if (existing != null)
            {
                //// Existing nodes should be reused.
                return(existing);
            }
            else
            {
                var node = new SerializeNode(currentId.ToString(), value);
                currentId++;

                //// Adds the newly-created node to the graph.
                CurrentGraph.AddNode(node);

                //// Check if dependencies need to be resolved or if the value is native/null.
                if (value != null && !TypeConfiguration.MatchNative().Match(value.GetType()))
                {
                    //// Get all of the dependencies as connections (unique for each individual node, so can construct them here).
                    var dependencies = Providers.GetDependencies(value)
                                       .Select(p => new SerializeDependency(p.Key, node, GetNode(p.Value)));

                    foreach (var dep in dependencies)
                    {
                        //// Add new dependencies to the graph.
                        CurrentGraph.AddConnection(dep);
                    }
                }
                else
                {
                    node.IsNative = true;
                }

                return(node);
            }
        }