Skip to content

Commit

Permalink
Fixed #244
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-xu committed Sep 15, 2017
1 parent d91afc6 commit 8de2b02
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/Ninject.Test/Integration/CircularDependenciesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,34 @@ public void DetectsCyclicDependenciesForGenericServiceRegisteredViaOpenGenericTy

}

public class WhenDependenciesHaveTwoWayCircularReferenceBetweenConstructorAndProperty : CircularDependenciesContext
{
public WhenDependenciesHaveTwoWayCircularReferenceBetweenConstructorAndProperty()
{
kernel.Bind<TwoWayConstructorPropertyFoo>().ToSelf().InSingletonScope();
kernel.Bind<TwoWayConstructorPropertyBar>().ToSelf().InSingletonScope();
}

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

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

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

public class TwoWayConstructorFoo
{
Expand All @@ -195,6 +222,22 @@ public class TwoWayPropertyBar
public TwoWayPropertyFoo Foo { get; set; }
}

public class TwoWayConstructorPropertyFoo
{
public TwoWayConstructorPropertyFoo(TwoWayConstructorPropertyBar bar)
{
this.Bar = bar;
}

public TwoWayConstructorPropertyBar Bar { get; private set; }
}

public class TwoWayConstructorPropertyBar
{
[Inject]
public TwoWayConstructorPropertyFoo Foo { get; set; }
}

public class ThreeWayConstructorFoo
{
public ThreeWayConstructorFoo(ThreeWayConstructorBar bar) { }
Expand Down
2 changes: 2 additions & 0 deletions src/Ninject.Test/Unit/CachePruningTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ public void BuildPlan(Type type)

public IPlan Plan { get; set; }

public ICache Cache { get; private set; }

public ICollection<IParameter> Parameters { get; set; }

public Type[] GenericArguments
Expand Down
2 changes: 1 addition & 1 deletion src/Ninject/Activation/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private bool IsCyclical(IContext targetContext)

if (targetContext.Request.Service == this.Request.Service)
{
if (!(this.Request.Target is PropertyTarget) || targetContext.GetScope() != this.GetScope() || this.GetScope() == null)
if ((this.Request.Target is ParameterTarget && targetContext.Request.Target is ParameterTarget) || targetContext.GetScope() != this.GetScope() || this.GetScope() == null)
{
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Ninject/Activation/IContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Ninject.Activation
{
using System;
using System.Collections.Generic;
using Ninject.Activation.Caching;
using Ninject.Parameters;
using Ninject.Planning;
using Ninject.Planning.Bindings;
Expand Down Expand Up @@ -54,6 +55,11 @@ public interface IContext
/// </summary>
IPlan Plan { get; set; }

/// <summary>
/// Gets the cache component.
/// </summary>
ICache Cache { get; }

/// <summary>
/// Gets the parameters that were passed to manipulate the activation process.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Ninject/Activation/Providers/StandardProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ public virtual object Create(IContext context)

var arguments = directive.Targets.Select(target => this.GetValue(context, target)).ToArray();

var cachedInstance = context.Cache.TryGet(context);

if (cachedInstance != null)
{
return cachedInstance;
}

return directive.Injector(arguments);
}

Expand Down

0 comments on commit 8de2b02

Please sign in to comment.