Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-xu committed Oct 24, 2017
1 parent e4f428d commit c37c1fe
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 30 additions & 6 deletions src/Ninject.Test/Integration/CircularDependenciesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public WhenDependenciesHaveTwoWayCircularReferenceBetweenConstructors()

this.kernel.Bind<IDecoratorPattern>().To<Decorator1>().WhenInjectedInto<Decorator2>();
this.kernel.Bind<IDecoratorPattern>().To<Decorator2>();

this.kernel.Bind<IDecoratorPattern>().To<Decorator3>().Named("Decorator3");
this.kernel.Bind<IDecoratorPattern>().To<Decorator4>().Named("Decorator4");
}

[Fact]
Expand All @@ -43,9 +46,15 @@ public void DoesNotThrowExceptionIfHookIsCreated()
}

[Fact]
public void DoesNotThrowExceptionIfConditionaDoesNotMatch()
public void DoesNotThrowExceptionIfConditionDoesNotMatch()
{
this.kernel.Get<IDecoratorPattern>((string)null);
}

[Fact]
public void DoesNotThrowExceptionWithNamedBinding()
{
this.kernel.Get<IDecoratorPattern>();
this.kernel.Get<IDecoratorPattern>("Decorator4");
}

[Fact]
Expand Down Expand Up @@ -189,19 +198,27 @@ public WhenDependenciesHaveTwoWayCircularReferenceBetweenConstructorAndProperty(
}

[Fact]
public void ThrowsActivationExceptionWhenHookIsResolved()
public void DoesNotThrowExceptionWhenGetFoo()
{
Assert.Throws<ActivationException>(() => this.kernel.Get<TwoWayConstructorPropertyFoo>());
this.kernel.Get<TwoWayConstructorPropertyFoo>();
}

[Fact]
public void DoesNotThrowException()
public void DoesNotThrowExceptionWhenGetBar()
{
this.kernel.Get<TwoWayConstructorPropertyBar>();
}

[Fact]
public void ScopeIsRespected()
public void ScopeIsRespectedWhenGetFooFirstly()
{
var foo = this.kernel.Get<TwoWayConstructorPropertyFoo>();
var bar = this.kernel.Get<TwoWayConstructorPropertyBar>();
foo.Bar.Should().BeSameAs(bar);
}

[Fact]
public void ScopeIsRespectedWhenGetBarFirstly()
{
var bar = this.kernel.Get<TwoWayConstructorPropertyBar>();
var foo = this.kernel.Get<TwoWayConstructorPropertyFoo>();
Expand Down Expand Up @@ -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) { }
}
}
5 changes: 3 additions & 2 deletions src/Ninject/Activation/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ public IProvider GetProvider()
/// <returns>The resolved instance.</returns>
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));
}
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit c37c1fe

Please sign in to comment.