PageRenderTime 65ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FubuMVC.StructureMap.Testing/Compliance/ObjectDef_Compliance.cs

https://github.com/wbinford/fubumvc
C# | 323 lines | 244 code | 75 blank | 4 comment | 0 complexity | a3a733c0cafbd762a39ecd05a30d53d4 MD5 | raw file
  1. using System.Collections.Generic;
  2. using FubuMVC.Core.Registration.ObjectGraph;
  3. using NUnit.Framework;
  4. using FubuTestingSupport;
  5. using System.Linq;
  6. namespace FubuMVC.StructureMap.Testing.Compliance
  7. {
  8. [TestFixture]
  9. public class ObjectDef_Compliance
  10. {
  11. [Test]
  12. public void simple_ObjectDef_by_type()
  13. {
  14. var container = ContainerFacilitySource.New(x => {
  15. x.Register(typeof(IService), ObjectDef.ForType<SimpleService>());
  16. });
  17. container.Get<IService>().ShouldBeOfType<SimpleService>();
  18. }
  19. [Test]
  20. public void simple_ObjectDef_by_value()
  21. {
  22. var service = new SimpleService();
  23. var container = ContainerFacilitySource.New(x => {
  24. x.Register(typeof (IService), ObjectDef.ForValue(service));
  25. });
  26. container.Get<IService>().ShouldBeTheSameAs(service);
  27. }
  28. [Test]
  29. public void auto_wiring_applies_when_the_dependency_is_not_set_explicitly()
  30. {
  31. var container = ContainerFacilitySource.New(x =>
  32. {
  33. x.Register(typeof(IService), ObjectDef.ForType<SimpleService>());
  34. });
  35. container.Get<GuyWithService>().Service.ShouldBeOfType<SimpleService>();
  36. }
  37. [Test]
  38. public void auto_wiring_applies_even_when_another_dependency_is_set_explicitly() {
  39. var container = ContainerFacilitySource.New(x => {
  40. x.Register(typeof(IService), ObjectDef.ForType<SimpleService>());
  41. x.Register(typeof(IThing), ObjectDef.ForType<ThingOne>());
  42. var def = ObjectDef.ForType<GuyWithServiceAndThing>();
  43. def.DependencyByType<IThing>(ObjectDef.ForType<ThingTwo>());
  44. var highLevelDef = ObjectDef.ForType<HighLevelObject>();
  45. highLevelDef.DependencyByType<GuyWithServiceAndThing>(def);
  46. x.Register(typeof(IHighLevelObject), highLevelDef);
  47. });
  48. var guyWithServiceAndThing = container.Get<GuyWithServiceAndThing>();
  49. guyWithServiceAndThing.Service.ShouldBeOfType<SimpleService>(); // auto-wired
  50. guyWithServiceAndThing.Thing.ShouldBeOfType<ThingOne>(); // auto-wired, even though explicitly set to ThingTwo for HighLevelObject
  51. }
  52. [Test]
  53. public void ObjectDef_with_one_explicit_dependency_defined_by_type()
  54. {
  55. var container = ContainerFacilitySource.New(x =>
  56. {
  57. x.Register(typeof(IService), ObjectDef.ForType<SimpleService>());
  58. var objectDef = ObjectDef.ForType<GuyWithService>();
  59. objectDef.DependencyByType<IService>(ObjectDef.ForType<DifferentService>());
  60. x.Register(typeof(GuyWithService), objectDef);
  61. });
  62. // The default IService is SimpleService, but the default ObjectDef (first one) explicitly
  63. // set up its IService dependency to be a "DifferentService"
  64. container.Get<GuyWithService>().Service.ShouldBeOfType<DifferentService>();
  65. }
  66. [Test]
  67. public void ObjectDef_with_one_explicit_dependency_defined_by_value()
  68. {
  69. var container = ContainerFacilitySource.New(x =>
  70. {
  71. x.Register(typeof(IService), ObjectDef.ForType<SimpleService>());
  72. var objectDef = ObjectDef.ForType<GuyWithService>();
  73. objectDef.DependencyByType<IService>(ObjectDef.ForValue(new DifferentService()));
  74. x.Register(typeof(GuyWithService), objectDef);
  75. });
  76. // The default IService is SimpleService, but the default ObjectDef (first one) explicitly
  77. // set up its IService dependency to be a "DifferentService"
  78. container.Get<GuyWithService>().Service.ShouldBeOfType<DifferentService>();
  79. }
  80. [Test]
  81. public void ObjectDef_with_one_explicit_and_one_implicit_dependency()
  82. {
  83. var container = ContainerFacilitySource.New(x => {
  84. x.Register(typeof(IService), ObjectDef.ForType<SimpleService>());
  85. x.Register(typeof(IThing), ObjectDef.ForType<ThingOne>());
  86. var def = ObjectDef.ForType<GuyWithServiceAndThing>();
  87. def.DependencyByType<IThing>(ObjectDef.ForType<ThingTwo>());
  88. x.Register(typeof(GuyWithServiceAndThing), def);
  89. });
  90. var guyWithServiceAndThing = container.Get<GuyWithServiceAndThing>();
  91. guyWithServiceAndThing.Service.ShouldBeOfType<SimpleService>(); // auto-wired
  92. guyWithServiceAndThing.Thing.ShouldBeOfType<ThingTwo>(); // explicitly set to be ThingTwo, even though auto-wiring would have put ThingOne here
  93. }
  94. [Test]
  95. public void three_deep_explicitly_configured_dep_tree()
  96. {
  97. var container = ContainerFacilitySource.New(x => {
  98. x.Register(typeof(IService), ObjectDef.ForType<SimpleService>());
  99. x.Register(typeof(IThing), ObjectDef.ForType<ThingOne>());
  100. var def = ObjectDef.ForType<GuyWithServiceAndThing>();
  101. def.DependencyByType<IThing>(ObjectDef.ForType<ThingTwo>());
  102. var highLevelDef = ObjectDef.ForType<HighLevelObject>();
  103. highLevelDef.DependencyByType<GuyWithServiceAndThing>(def);
  104. x.Register(typeof(IHighLevelObject), highLevelDef);
  105. });
  106. var guyWithServiceAndThing = container.Get<IHighLevelObject>().Guy;
  107. guyWithServiceAndThing.Service.ShouldBeOfType<SimpleService>(); // auto-wired
  108. guyWithServiceAndThing.Thing.ShouldBeOfType<ThingTwo>(); // explicitly set to be ThingTwo, even though auto-wiring would have put ThingOne here
  109. }
  110. [Test]
  111. public void can_get_all_registered_implementations_of_a_service()
  112. {
  113. var container = ContainerFacilitySource.New(x =>
  114. {
  115. x.Register(typeof(IService), ObjectDef.ForType<SimpleService>());
  116. x.Register(typeof(IService), ObjectDef.ForType<DifferentService>());
  117. x.Register(typeof(IService), ObjectDef.ForType<ExceptionCaseService>());
  118. });
  119. container.GetAll<IService>()
  120. .Select(x => x.GetType())
  121. .ShouldHaveTheSameElementsAs(typeof(SimpleService), typeof(DifferentService), typeof(ExceptionCaseService));
  122. }
  123. [Test]
  124. public void implicitly_auto_wires_all_implementations_of_a_service_if_not_explicitly_overridden()
  125. {
  126. var container = ContainerFacilitySource.New(x => {
  127. x.Register(typeof(IService), ObjectDef.ForType<SimpleService>());
  128. x.Register(typeof(IService), ObjectDef.ForType<DifferentService>());
  129. x.Register(typeof(IService), ObjectDef.ForType<ExceptionCaseService>());
  130. });
  131. container.Get<ThingThatUsesLotsOfServices>()
  132. .Services.Select(x => x.GetType())
  133. .ShouldHaveTheSameElementsAs(typeof(SimpleService), typeof(DifferentService), typeof(ExceptionCaseService));
  134. }
  135. [Test]
  136. public void implicitly_auto_wires_all_implementations_of_a_service_if_not_explicitly_overridden_and_maintains_the_order_of_registration()
  137. {
  138. var container = ContainerFacilitySource.New(x =>
  139. {
  140. x.Register(typeof(IService), ObjectDef.ForType<ExceptionCaseService>());
  141. x.Register(typeof(IService), ObjectDef.ForType<SimpleService>());
  142. x.Register(typeof(IService), ObjectDef.ForType<DifferentService>());
  143. });
  144. container.Get<ThingThatUsesLotsOfServices>()
  145. .Services.Select(x => x.GetType())
  146. .ShouldHaveTheSameElementsAs(typeof(ExceptionCaseService), typeof(SimpleService), typeof(DifferentService));
  147. }
  148. [Test]
  149. public void explicit_registration_of_an_ienumerable_argument()
  150. {
  151. var container = ContainerFacilitySource.New(x =>
  152. {
  153. x.Register(typeof(IService), ObjectDef.ForType<ExceptionCaseService>());
  154. x.Register(typeof(IService), ObjectDef.ForType<SimpleService>());
  155. x.Register(typeof(IService), ObjectDef.ForType<DifferentService>());
  156. var def = ObjectDef.ForType<ThingThatUsesLotsOfServices>();
  157. def.EnumerableDependenciesOf<IService>().Add(ObjectDef.ForType<OddballService>());
  158. def.EnumerableDependenciesOf<IService>().Add(ObjectDef.ForType<DifferentService>());
  159. x.Register(typeof(ThingThatUsesLotsOfServices), def);
  160. });
  161. container.Get<ThingThatUsesLotsOfServices>()
  162. .Services.Select(x => x.GetType())
  163. .ShouldHaveTheSameElementsAs(typeof(OddballService), typeof(DifferentService));
  164. }
  165. }
  166. public class ThingThatUsesLotsOfServices
  167. {
  168. private readonly IEnumerable<IService> _services;
  169. public ThingThatUsesLotsOfServices(IEnumerable<IService> services)
  170. {
  171. _services = services;
  172. }
  173. public IEnumerable<IService> Services
  174. {
  175. get { return _services; }
  176. }
  177. }
  178. public interface IHighLevelObject
  179. {
  180. GuyWithServiceAndThing Guy { get; }
  181. }
  182. public class HighLevelObject : IHighLevelObject
  183. {
  184. private readonly GuyWithServiceAndThing _guy;
  185. public HighLevelObject(GuyWithServiceAndThing guy)
  186. {
  187. _guy = guy;
  188. }
  189. public GuyWithServiceAndThing Guy
  190. {
  191. get { return _guy; }
  192. }
  193. }
  194. public class GuyWithServiceAndThing
  195. {
  196. private readonly IService _service;
  197. private readonly IThing _thing;
  198. public GuyWithServiceAndThing(IService service, IThing thing)
  199. {
  200. _service = service;
  201. _thing = thing;
  202. }
  203. public IService Service
  204. {
  205. get { return _service; }
  206. }
  207. public IThing Thing
  208. {
  209. get { return _thing; }
  210. }
  211. }
  212. public class GuyWithService
  213. {
  214. private readonly IService _service;
  215. public GuyWithService(IService service)
  216. {
  217. _service = service;
  218. }
  219. public IService Service
  220. {
  221. get { return _service; }
  222. }
  223. }
  224. public interface IService
  225. {
  226. }
  227. public class DifferentService : IService
  228. {
  229. }
  230. public class SimpleService : IService
  231. {
  232. }
  233. public class OddballService : IService
  234. {
  235. }
  236. public class ExceptionCaseService : IService
  237. {
  238. }
  239. public interface IThing
  240. {
  241. }
  242. public class ThingOne : IThing
  243. {
  244. }
  245. public class ThingTwo : IThing
  246. {
  247. }
  248. }