From 67affd584c9b1769aa89b95b6871f5cf81aaba3c Mon Sep 17 00:00:00 2001 From: Scott Xu Date: Tue, 24 Oct 2017 22:24:55 +0800 Subject: [PATCH] Fixed https://github.com/ninject/Ninject/issues/261 --- CHANGELOG.md | 5 +++ .../Integration/CircularDependenciesTests.cs | 36 +++++++++++++++---- src/Ninject/Activation/Context.cs | 5 +-- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bdae3659..73a8a62c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed +- Throw cyclic dependency exception when resolve a named binding with decoration pattern [#261](https://github.com/ninject/Ninject/issues/261) + ## [3.3.3] - 2017-10-22 ### Fixed diff --git a/src/Ninject.Test/Integration/CircularDependenciesTests.cs b/src/Ninject.Test/Integration/CircularDependenciesTests.cs index 9abeb1f3..518d7877 100644 --- a/src/Ninject.Test/Integration/CircularDependenciesTests.cs +++ b/src/Ninject.Test/Integration/CircularDependenciesTests.cs @@ -33,6 +33,9 @@ public WhenDependenciesHaveTwoWayCircularReferenceBetweenConstructors() this.kernel.Bind().To().WhenInjectedInto(); this.kernel.Bind().To(); + + this.kernel.Bind().To().Named("Decorator3"); + this.kernel.Bind().To().Named("Decorator4"); } [Fact] @@ -43,9 +46,15 @@ public void DoesNotThrowExceptionIfHookIsCreated() } [Fact] - public void DoesNotThrowExceptionIfConditionaDoesNotMatch() + public void DoesNotThrowExceptionIfConditionDoesNotMatch() + { + this.kernel.Get((string)null); + } + + [Fact] + public void DoesNotThrowExceptionWithNamedBinding() { - this.kernel.Get(); + this.kernel.Get("Decorator4"); } [Fact] @@ -189,19 +198,27 @@ public WhenDependenciesHaveTwoWayCircularReferenceBetweenConstructorAndProperty( } [Fact] - public void ThrowsActivationExceptionWhenHookIsResolved() + public void DoesNotThrowExceptionWhenGetFoo() { - Assert.Throws(() => this.kernel.Get()); + this.kernel.Get(); } [Fact] - public void DoesNotThrowException() + public void DoesNotThrowExceptionWhenGetBar() { this.kernel.Get(); } [Fact] - public void ScopeIsRespected() + public void ScopeIsRespectedWhenGetFooFirstly() + { + var foo = this.kernel.Get(); + var bar = this.kernel.Get(); + foo.Bar.Should().BeSameAs(bar); + } + + [Fact] + public void ScopeIsRespectedWhenGetBarFirstly() { var bar = this.kernel.Get(); var foo = this.kernel.Get(); @@ -344,4 +361,11 @@ public class Decorator2 : IDecoratorPattern { public Decorator2(IDecoratorPattern decorator) { } } + + public class Decorator3 : IDecoratorPattern { } + + public class Decorator4 : IDecoratorPattern + { + public Decorator4([Named("Decorator3")]IDecoratorPattern decorator) { } + } } \ No newline at end of file diff --git a/src/Ninject/Activation/Context.cs b/src/Ninject/Activation/Context.cs index 959ac66a..75d4d399 100644 --- a/src/Ninject/Activation/Context.cs +++ b/src/Ninject/Activation/Context.cs @@ -148,7 +148,8 @@ public IProvider GetProvider() /// The resolved instance. public object Resolve() { - if (this.IsCyclical(this.Request.ParentContext)) + if (this.Request.ActiveBindings.Contains(this.Binding) && + this.IsCyclical(this.Request.ParentContext)) { throw new ActivationException(ExceptionFormatter.CyclicalDependenciesDetected(this)); } @@ -227,7 +228,7 @@ private bool IsCyclical(IContext targetContext) return false; } - if (targetContext.Request.Service == this.Request.Service && targetContext.Binding.Condition == this.Binding.Condition) + if (targetContext.Request.Service == this.Request.Service) { if ((this.Request.Target is ParameterTarget && targetContext.Request.Target is ParameterTarget) || targetContext.GetScope() != this.GetScope() || this.GetScope() == null) {