PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/src/LinFu.IoC/Configuration/RecursiveDependencyException.cs

http://github.com/philiplaureano/LinFu
C# | 56 lines | 33 code | 7 blank | 16 comment | 4 complexity | 6fa2af1a912b3fceca6ebe1992726ccc MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using LinFu.IoC.Interfaces;
 5
 6namespace LinFu.IoC.Configuration
 7{
 8    /// <summary>
 9    ///     The exception thrown when a recursive dependency is detected
10    ///     inside a <see cref="IServiceContainer" /> instance.
11    /// </summary>
12    [Serializable]
13    public class RecursiveDependencyException : Exception
14    {
15        private readonly LinkedList<Type> _typeChain;
16
17        /// <summary>
18        ///     Initializes the <see cref="RecursiveDependencyException" />
19        ///     class with the <paramref name="typeChain">chain</paramref>
20        ///     of depedencies that caused the exception.
21        /// </summary>
22        /// <param name="typeChain">The sequence of types that caused the dependency exception.</param>
23        public RecursiveDependencyException(LinkedList<Type> typeChain)
24        {
25            _typeChain = typeChain;
26        }
27
28        /// <summary>
29        ///     Gets the value indicating the chain of types that caused the exception.
30        /// </summary>
31        public LinkedList<Type> TypeChain => new LinkedList<Type>(_typeChain);
32
33        /// <summary>
34        ///     Gets the value indicating the error message from the <see cref="RecursiveDependencyException" />.
35        /// </summary>
36        public override string Message
37        {
38            get
39            {
40                var messageFormat = "Recursive Dependency Detected: {0}";
41                var builder = new StringBuilder();
42
43                var currentNode = _typeChain.First;
44                while (currentNode != null)
45                {
46                    builder.AppendFormat("{0}", currentNode.Value.AssemblyQualifiedName);
47
48                    if (currentNode.Next != null) builder.Append("--->");
49                    currentNode = currentNode.Next;
50                }
51
52                return string.Format(messageFormat, builder);
53            }
54        }
55    }
56}