Skip to content

Commit 67affd5

Browse files
committed
Fixed #261
1 parent c2ba5c2 commit 67affd5

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
9+
### Fixed
10+
- Throw cyclic dependency exception when resolve a named binding with decoration pattern [#261](https://github.com/ninject/Ninject/issues/261)
11+
712
## [3.3.3] - 2017-10-22
813

914
### Fixed

src/Ninject.Test/Integration/CircularDependenciesTests.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public WhenDependenciesHaveTwoWayCircularReferenceBetweenConstructors()
3333

3434
this.kernel.Bind<IDecoratorPattern>().To<Decorator1>().WhenInjectedInto<Decorator2>();
3535
this.kernel.Bind<IDecoratorPattern>().To<Decorator2>();
36+
37+
this.kernel.Bind<IDecoratorPattern>().To<Decorator3>().Named("Decorator3");
38+
this.kernel.Bind<IDecoratorPattern>().To<Decorator4>().Named("Decorator4");
3639
}
3740

3841
[Fact]
@@ -43,9 +46,15 @@ public void DoesNotThrowExceptionIfHookIsCreated()
4346
}
4447

4548
[Fact]
46-
public void DoesNotThrowExceptionIfConditionaDoesNotMatch()
49+
public void DoesNotThrowExceptionIfConditionDoesNotMatch()
50+
{
51+
this.kernel.Get<IDecoratorPattern>((string)null);
52+
}
53+
54+
[Fact]
55+
public void DoesNotThrowExceptionWithNamedBinding()
4756
{
48-
this.kernel.Get<IDecoratorPattern>();
57+
this.kernel.Get<IDecoratorPattern>("Decorator4");
4958
}
5059

5160
[Fact]
@@ -189,19 +198,27 @@ public WhenDependenciesHaveTwoWayCircularReferenceBetweenConstructorAndProperty(
189198
}
190199

191200
[Fact]
192-
public void ThrowsActivationExceptionWhenHookIsResolved()
201+
public void DoesNotThrowExceptionWhenGetFoo()
193202
{
194-
Assert.Throws<ActivationException>(() => this.kernel.Get<TwoWayConstructorPropertyFoo>());
203+
this.kernel.Get<TwoWayConstructorPropertyFoo>();
195204
}
196205

197206
[Fact]
198-
public void DoesNotThrowException()
207+
public void DoesNotThrowExceptionWhenGetBar()
199208
{
200209
this.kernel.Get<TwoWayConstructorPropertyBar>();
201210
}
202211

203212
[Fact]
204-
public void ScopeIsRespected()
213+
public void ScopeIsRespectedWhenGetFooFirstly()
214+
{
215+
var foo = this.kernel.Get<TwoWayConstructorPropertyFoo>();
216+
var bar = this.kernel.Get<TwoWayConstructorPropertyBar>();
217+
foo.Bar.Should().BeSameAs(bar);
218+
}
219+
220+
[Fact]
221+
public void ScopeIsRespectedWhenGetBarFirstly()
205222
{
206223
var bar = this.kernel.Get<TwoWayConstructorPropertyBar>();
207224
var foo = this.kernel.Get<TwoWayConstructorPropertyFoo>();
@@ -344,4 +361,11 @@ public class Decorator2 : IDecoratorPattern
344361
{
345362
public Decorator2(IDecoratorPattern decorator) { }
346363
}
364+
365+
public class Decorator3 : IDecoratorPattern { }
366+
367+
public class Decorator4 : IDecoratorPattern
368+
{
369+
public Decorator4([Named("Decorator3")]IDecoratorPattern decorator) { }
370+
}
347371
}

src/Ninject/Activation/Context.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ public IProvider GetProvider()
148148
/// <returns>The resolved instance.</returns>
149149
public object Resolve()
150150
{
151-
if (this.IsCyclical(this.Request.ParentContext))
151+
if (this.Request.ActiveBindings.Contains(this.Binding) &&
152+
this.IsCyclical(this.Request.ParentContext))
152153
{
153154
throw new ActivationException(ExceptionFormatter.CyclicalDependenciesDetected(this));
154155
}
@@ -227,7 +228,7 @@ private bool IsCyclical(IContext targetContext)
227228
return false;
228229
}
229230

230-
if (targetContext.Request.Service == this.Request.Service && targetContext.Binding.Condition == this.Binding.Condition)
231+
if (targetContext.Request.Service == this.Request.Service)
231232
{
232233
if ((this.Request.Target is ParameterTarget && targetContext.Request.Target is ParameterTarget) || targetContext.GetScope() != this.GetScope() || this.GetScope() == null)
233234
{

0 commit comments

Comments
 (0)