Exemple #1
0
        public void TestCircularDependencyExceptionNoStack()
        {
            CircularDependencyException circularDependencyException = new CircularDependencyException(typeof(IFoo), null);
            string message = $"Circular dependency has been detected when trying to resolve `{typeof(IFoo)}`.\n";

            Assert.AreEqual(message, circularDependencyException.Message);
        }
Exemple #2
0
        public void CircularDependency_ThrowsCircularDependencyException_Test()
        {
            var a = new Node <object>("a");
            var b = new Node <object>("b");
            var c = new Node <object>("c");
            var d = new Node <object>("d");
            var e = new Node <object>("e");

            a.Edges.Add(b); // a depends on b
            a.Edges.Add(d); // a depends on d
            b.Edges.Add(c); // b depends on c
            b.Edges.Add(e); // b depends on e
            c.Edges.Add(d); // c depends on d
            c.Edges.Add(e); // c depends on e
            d.Edges.Add(b); // d depends on b

            var resolved = new List <Node <object> >();

            CircularDependencyException <object> exception = null;

            try
            {
                a.ResolveDependencies();
            }
            catch (CircularDependencyException <object> ex)
            {
                exception = ex;
            }

            Assert.NotNull(exception);
            Assert.Equal(d, exception.A);
            Assert.Equal(b, exception.B);
        }
 public void Enter(Type type)
 {
     if (_visitedDependencies.Contains(type))
     {
         var error = new CircularDependencyException(_visitedDependencies.Concat(new[] { type }).ToArray());
         throw error;
     }
     _visitedDependencies.Add(type);
 }
        public void Normal()
        {
            var dependencies = new[]
            {
                new object[] { 1, 2, 3 },
                new object[] { 2, 3, 4 }
            };

            var exception = new CircularDependencyException(dependencies);

            var expected = "Circular dependencies detected:\r\n" +
                           "1 -> 2 -> 3\r\n" +
                           "2 -> 3 -> 4";
            var actual = exception.Message;

            Assert.That(actual, Is.EqualTo(expected));
        }
Exemple #5
0
        public void TestCircularDependencies()
        {
            _iocContainer.Register <IFoo, Foo>();
            _iocContainer.Register <IBar, Bar>();

            CircularDependencyException exception = Assert.Throws <CircularDependencyException>(() => _iocContainer.Resolve <IFoo>());

            Assert.AreEqual(typeof(IFoo), exception.ResolvingType);
            Assert.AreEqual(2, exception.ResolveStack.Count);

            string message = $"Circular dependency has been detected when trying to resolve `{typeof(IFoo)}`.\n" +
                             "Resolve stack that resulted in the circular dependency:\n" +
                             $"\t`{typeof(IFoo)}` resolved as dependency of\n" +
                             $"\t`{typeof(IBar)}` resolved as dependency of\n" +
                             $"\t`{typeof(IFoo)}` which is the root type being resolved.";

            Assert.AreEqual(message, exception.Message);
        }
Exemple #6
0
        /// <summary>
        /// RegistrationType => container.RegisterType<RegistrationType, type>
        /// Type => used by container to build up the object. It searches the [Dependency] in Type and fill them up in the object
        /// </summary>
        public override void PreBuildUp(ref BuilderContext context)
        {
            var stackExist = _stackFactory.Exist();
            Stack <ResolveStackEntry> stack = stackExist ? _stackFactory.Get() : _stackFactory.Set();
            var name = context.Name;
            var type = context.Type;

            // check cirular dep
            stack.Push(new ResolveStackEntry(type, name, !stackExist));

            if (stack.Skip(1).Any(ent => ent.ResolveType == type && ent.ResolveName == name))
            {
                var ex = new CircularDependencyException(string.Join("\r\n->", stack.Reverse().Select(t => $"type {t.ResolveType} (name: {t.ResolveName})").ToArray()));

                _stackFactory.Delete();

                throw ex;
            }

            base.PreBuildUp(ref context);
        }