PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Ninject.Tests/Integration/CircularDependenciesTests.cs

https://github.com/developingchris/ninject
C# | 168 lines | 141 code | 27 blank | 0 comment | 0 complexity | 6e00dac31bc67c1913fb4862b208b6f7 MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using Ninject.Activation;
  4. using Ninject.Parameters;
  5. using Xunit;
  6. using Xunit.Should;
  7. namespace Ninject.Tests.Integration.CircularDependenciesTests
  8. {
  9. public class CircularDependenciesContext
  10. {
  11. protected readonly StandardKernel kernel;
  12. public CircularDependenciesContext()
  13. {
  14. kernel = new StandardKernel();
  15. }
  16. }
  17. public class WhenDependenciesHaveTwoWayCircularReferenceBetweenConstructors : CircularDependenciesContext
  18. {
  19. public WhenDependenciesHaveTwoWayCircularReferenceBetweenConstructors()
  20. {
  21. kernel.Bind<TwoWayConstructorFoo>().ToSelf().InSingletonScope();
  22. kernel.Bind<TwoWayConstructorBar>().ToSelf().InSingletonScope();
  23. }
  24. [Fact]
  25. public void DoesNotThrowExceptionIfHookIsCreated()
  26. {
  27. var request = new Request(typeof(TwoWayConstructorFoo), null, Enumerable.Empty<IParameter>(), null, false);
  28. Assert.DoesNotThrow(() => kernel.Resolve(request));
  29. }
  30. [Fact]
  31. public void ThrowsActivationExceptionWhenHookIsResolved()
  32. {
  33. Assert.Throws<ActivationException>(() => kernel.Get<TwoWayConstructorFoo>());
  34. }
  35. }
  36. public class WhenDependenciesHaveTwoWayCircularReferenceBetweenProperties : CircularDependenciesContext
  37. {
  38. public WhenDependenciesHaveTwoWayCircularReferenceBetweenProperties()
  39. {
  40. kernel.Bind<TwoWayPropertyFoo>().ToSelf().InSingletonScope();
  41. kernel.Bind<TwoWayPropertyBar>().ToSelf().InSingletonScope();
  42. }
  43. [Fact]
  44. public void DoesNotThrowException()
  45. {
  46. Assert.DoesNotThrow(() => kernel.Get<TwoWayPropertyFoo>());
  47. }
  48. [Fact]
  49. public void ScopeIsRespected()
  50. {
  51. var foo = kernel.Get<TwoWayPropertyFoo>();
  52. var bar = kernel.Get<TwoWayPropertyBar>();
  53. foo.Bar.ShouldBeSameAs(bar);
  54. bar.Foo.ShouldBeSameAs(foo);
  55. }
  56. }
  57. public class WhenDependenciesHaveThreeWayCircularReferenceBetweenConstructors : CircularDependenciesContext
  58. {
  59. public WhenDependenciesHaveThreeWayCircularReferenceBetweenConstructors()
  60. {
  61. kernel.Bind<ThreeWayConstructorFoo>().ToSelf().InSingletonScope();
  62. kernel.Bind<ThreeWayConstructorBar>().ToSelf().InSingletonScope();
  63. kernel.Bind<ThreeWayConstructorBaz>().ToSelf().InSingletonScope();
  64. }
  65. [Fact]
  66. public void DoesNotThrowExceptionIfHookIsCreated()
  67. {
  68. var request = new Request(typeof(ThreeWayConstructorFoo), null, Enumerable.Empty<IParameter>(), null, false);
  69. Assert.DoesNotThrow(() => kernel.Resolve(request));
  70. }
  71. [Fact]
  72. public void ThrowsActivationExceptionWhenHookIsResolved()
  73. {
  74. Assert.Throws<ActivationException>(() => kernel.Get<ThreeWayConstructorFoo>());
  75. }
  76. }
  77. public class WhenDependenciesHaveThreeWayCircularReferenceBetweenProperties : CircularDependenciesContext
  78. {
  79. public WhenDependenciesHaveThreeWayCircularReferenceBetweenProperties()
  80. {
  81. kernel.Bind<ThreeWayPropertyFoo>().ToSelf().InSingletonScope();
  82. kernel.Bind<ThreeWayPropertyBar>().ToSelf().InSingletonScope();
  83. kernel.Bind<ThreeWayPropertyBaz>().ToSelf().InSingletonScope();
  84. }
  85. [Fact]
  86. public void DoesNotThrowException()
  87. {
  88. Assert.DoesNotThrow(() => kernel.Get<ThreeWayPropertyFoo>());
  89. }
  90. [Fact]
  91. public void ScopeIsRespected()
  92. {
  93. var foo = kernel.Get<ThreeWayPropertyFoo>();
  94. var bar = kernel.Get<ThreeWayPropertyBar>();
  95. var baz = kernel.Get<ThreeWayPropertyBaz>();
  96. foo.Bar.ShouldBeSameAs(bar);
  97. bar.Baz.ShouldBeSameAs(baz);
  98. baz.Foo.ShouldBeSameAs(foo);
  99. }
  100. }
  101. public class TwoWayConstructorFoo
  102. {
  103. public TwoWayConstructorFoo(TwoWayConstructorBar bar) { }
  104. }
  105. public class TwoWayConstructorBar
  106. {
  107. public TwoWayConstructorBar(TwoWayConstructorFoo foo) { }
  108. }
  109. public class TwoWayPropertyFoo
  110. {
  111. [Inject] public TwoWayPropertyBar Bar { get; set; }
  112. }
  113. public class TwoWayPropertyBar
  114. {
  115. [Inject] public TwoWayPropertyFoo Foo { get; set; }
  116. }
  117. public class ThreeWayConstructorFoo
  118. {
  119. public ThreeWayConstructorFoo(ThreeWayConstructorBar bar) { }
  120. }
  121. public class ThreeWayConstructorBar
  122. {
  123. public ThreeWayConstructorBar(ThreeWayConstructorBaz baz) { }
  124. }
  125. public class ThreeWayConstructorBaz
  126. {
  127. public ThreeWayConstructorBaz(TwoWayConstructorFoo foo) { }
  128. }
  129. public class ThreeWayPropertyFoo
  130. {
  131. [Inject] public ThreeWayPropertyBar Bar { get; set; }
  132. }
  133. public class ThreeWayPropertyBar
  134. {
  135. [Inject] public ThreeWayPropertyBaz Baz { get; set; }
  136. }
  137. public class ThreeWayPropertyBaz
  138. {
  139. [Inject] public ThreeWayPropertyFoo Foo { get; set; }
  140. }
  141. }