/src/Nancy.Testing/ConfigurableBootstrapper.cs

https://github.com/factormystic/Nancy · C# · 982 lines · 495 code · 130 blank · 357 comment · 4 complexity · 4d1b608fe3f5099d2d114b36e248e25e MD5 · raw file

  1. namespace Nancy.Testing
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Nancy.Bootstrapper;
  7. using Nancy.Conventions;
  8. using Nancy.ErrorHandling;
  9. using Nancy.ModelBinding;
  10. using Nancy.Routing;
  11. using Nancy.Security;
  12. using Nancy.ViewEngines;
  13. using TinyIoC;
  14. /// <summary>
  15. /// A Nancy boostrapper that can be configured with either Type or Instance overrides for all Nancy types.
  16. /// </summary>
  17. public class ConfigurableBootstrapper : NancyBootstrapperWithRequestContainerBase<TinyIoCContainer>
  18. {
  19. private readonly List<object> registeredTypes;
  20. private readonly List<InstanceRegistration> registeredInstances;
  21. private readonly NancyInternalConfiguration configuration;
  22. private bool disableAutoRegistration;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="ConfigurableBootstrapper"/> class.
  25. /// </summary>
  26. public ConfigurableBootstrapper()
  27. : this(null)
  28. {
  29. }
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="ConfigurableBootstrapper"/> class.
  32. /// </summary>
  33. /// <param name="configuration">The configuration that should be used by the bootstrapper.</param>
  34. public ConfigurableBootstrapper(Action<ConfigurableBoostrapperConfigurator> configuration)
  35. {
  36. this.configuration = NancyInternalConfiguration.Default;
  37. this.registeredTypes = new List<object>();
  38. this.registeredInstances = new List<InstanceRegistration>();
  39. if (configuration != null)
  40. {
  41. var configurator =
  42. new ConfigurableBoostrapperConfigurator(this);
  43. configuration.Invoke(configurator);
  44. }
  45. }
  46. private IEnumerable<ModuleRegistration> GetModuleRegistrations()
  47. {
  48. return this.registeredTypes.Where(x => x.GetType().Equals(typeof(ModuleRegistration))).Cast<ModuleRegistration>();
  49. }
  50. private IEnumerable<TypeRegistration> GetTypeRegistrations()
  51. {
  52. return this.registeredTypes.Where(x => x.GetType().Equals(typeof(TypeRegistration))).Cast<TypeRegistration>();
  53. }
  54. private IEnumerable<CollectionTypeRegistration> GetCollectionTypeRegistrations()
  55. {
  56. return this.registeredTypes.Where(x => x.GetType().Equals(typeof(CollectionTypeRegistration))).Cast<CollectionTypeRegistration>();
  57. }
  58. private IEnumerable<Type> Resolve<T>()
  59. {
  60. var types = this.GetTypeRegistrations()
  61. .Where(x => x.RegistrationType.Equals(typeof(T)))
  62. .Select(x => x.ImplementationType)
  63. .ToList();
  64. return (types.Any()) ? types : null;
  65. }
  66. /// <summary>
  67. /// Nancy internal configuration
  68. /// </summary>
  69. protected override sealed NancyInternalConfiguration InternalConfiguration
  70. {
  71. get { return this.configuration; }
  72. }
  73. /// <summary>
  74. /// Nancy conventions
  75. /// </summary>
  76. protected override NancyConventions Conventions
  77. {
  78. get
  79. {
  80. var conventions = this.registeredInstances
  81. .Where(x => x.RegistrationType.Equals(typeof(NancyConventions)))
  82. .Select(x => x.Implementation)
  83. .Cast<NancyConventions>()
  84. .FirstOrDefault();
  85. return conventions ?? base.Conventions;
  86. }
  87. }
  88. /// <summary>
  89. /// Gets all available module types
  90. /// </summary>
  91. protected override IEnumerable<ModuleRegistration> Modules
  92. {
  93. get
  94. {
  95. var moduleRegistrations =
  96. this.GetModuleRegistrations().ToList();
  97. return (moduleRegistrations.Any()) ? moduleRegistrations : base.Modules;
  98. }
  99. }
  100. /// <summary>
  101. /// Gets the available view engine types
  102. /// </summary>
  103. protected override IEnumerable<Type> ViewEngines
  104. {
  105. get { return this.Resolve<IViewEngine>() ?? base.ViewEngines; }
  106. }
  107. /// <summary>
  108. /// Gets the available custom model binders
  109. /// </summary>
  110. protected override IEnumerable<Type> ModelBinders
  111. {
  112. get { return this.Resolve<IModelBinder>() ?? base.ModelBinders; }
  113. }
  114. /// <summary>
  115. /// Gets the available custom type converters
  116. /// </summary>
  117. protected override IEnumerable<Type> TypeConverters
  118. {
  119. get { return this.Resolve<ITypeConverter>() ?? base.TypeConverters; }
  120. }
  121. /// <summary>
  122. /// Gets the available custom body deserializers
  123. /// </summary>
  124. protected override IEnumerable<Type> BodyDeserializers
  125. {
  126. get { return this.Resolve<IBodyDeserializer>() ?? base.BodyDeserializers; }
  127. }
  128. /// <summary>
  129. /// Gets all startup tasks
  130. /// </summary>
  131. protected override IEnumerable<Type> StartupTasks
  132. {
  133. get { return this.Resolve<IStartup>() ?? base.StartupTasks; }
  134. }
  135. /// <summary>
  136. /// Gets the root path provider
  137. /// </summary>
  138. protected override Type RootPathProvider
  139. {
  140. get
  141. {
  142. var rootPathProvider =
  143. this.Resolve<IRootPathProvider>();
  144. return (rootPathProvider != null) ? rootPathProvider.First() : base.RootPathProvider;
  145. }
  146. }
  147. /// <summary>
  148. /// Configures the container using AutoRegister followed by registration
  149. /// of default INancyModuleCatalog and IRouteResolver.
  150. /// </summary>
  151. /// <param name="container">Container instance</param>
  152. protected override void ConfigureApplicationContainer(TinyIoCContainer container)
  153. {
  154. if (!this.disableAutoRegistration)
  155. {
  156. container.AutoRegister();
  157. this.RegisterBootstrapperTypes(container);
  158. }
  159. }
  160. /// <summary>
  161. /// Creates a per request child/nested container
  162. /// </summary>
  163. /// <returns>Request container instance</returns>
  164. protected override TinyIoCContainer CreateRequestContainer()
  165. {
  166. return this.ApplicationContainer.GetChildContainer();
  167. }
  168. /// <summary>
  169. /// Retrieve all module instances from the container
  170. /// </summary>
  171. /// <param name="container">Container to use</param>
  172. /// <returns>Collection of NancyModule instances</returns>
  173. protected override IEnumerable<NancyModule> GetAllModules(TinyIoCContainer container)
  174. {
  175. return container.ResolveAll<NancyModule>(false);
  176. }
  177. /// <summary>
  178. /// Gets the application level container
  179. /// </summary>
  180. /// <returns>Container instance</returns>
  181. protected override TinyIoCContainer GetApplicationContainer()
  182. {
  183. return new TinyIoCContainer();
  184. }
  185. /// <summary>
  186. /// Resolve INancyEngine
  187. /// </summary>
  188. /// <returns>INancyEngine implementation</returns>
  189. protected override INancyEngine GetEngineInternal()
  190. {
  191. return this.ApplicationContainer.Resolve<INancyEngine>();
  192. }
  193. /// <summary>
  194. /// Retreive a specific module instance from the container by its key
  195. /// </summary>
  196. /// <param name="container">Container to use</param>
  197. /// <param name="moduleKey">Module key of the module</param>
  198. /// <returns>NancyModule instance</returns>
  199. protected override NancyModule GetModuleByKey(TinyIoCContainer container, string moduleKey)
  200. {
  201. return container.Resolve<NancyModule>(moduleKey);
  202. }
  203. /// <summary>
  204. /// Get the moduleKey generator
  205. /// </summary>
  206. /// <returns>IModuleKeyGenerator instance</returns>
  207. protected override IModuleKeyGenerator GetModuleKeyGenerator()
  208. {
  209. return this.ApplicationContainer.Resolve<IModuleKeyGenerator>();
  210. }
  211. /// <summary>
  212. /// Gets all registered startup tasks
  213. /// </summary>
  214. /// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="IStartup"/> instances. </returns>
  215. protected override IEnumerable<IStartup> GetStartupTasks()
  216. {
  217. return this.ApplicationContainer.ResolveAll<IStartup>(false);
  218. }
  219. /// <summary>
  220. /// Register the bootstrapper's implemented types into the container.
  221. /// This is necessary so a user can pass in a populated container but not have
  222. /// to take the responsibility of registering things like INancyModuleCatalog manually.
  223. /// </summary>
  224. /// <param name="applicationContainer">Application container to register into</param>
  225. protected override void RegisterBootstrapperTypes(TinyIoCContainer applicationContainer)
  226. {
  227. var moduleCatalog = this.registeredInstances
  228. .Where(x => x.RegistrationType.Equals(typeof(INancyModuleCatalog)))
  229. .Select(x => x.Implementation)
  230. .Cast<INancyModuleCatalog>()
  231. .FirstOrDefault() ?? this;
  232. applicationContainer.Register<INancyModuleCatalog>(moduleCatalog);
  233. }
  234. /// <summary>
  235. /// Register the default implementations of internally used types into the container as singletons
  236. /// </summary>
  237. /// <param name="container">Container to register into</param>
  238. /// <param name="typeRegistrations">Type registrations to register</param>
  239. protected override void RegisterTypes(TinyIoCContainer container, IEnumerable<TypeRegistration> typeRegistrations)
  240. {
  241. var configuredTypes = this.GetTypeRegistrations().ToList();
  242. typeRegistrations = configuredTypes
  243. .Concat(typeRegistrations.Where(x => !configuredTypes.Any(y => y.RegistrationType.Equals(x.RegistrationType))))
  244. .Where(x => !this.registeredInstances.Any(y => y.RegistrationType.Equals(x.RegistrationType)));
  245. foreach (var typeRegistration in typeRegistrations)
  246. {
  247. container.Register(typeRegistration.RegistrationType, typeRegistration.ImplementationType).AsSingleton();
  248. }
  249. }
  250. /// <summary>
  251. /// Register the various collections into the container as singletons to later be resolved
  252. /// by IEnumerable{Type} constructor dependencies.
  253. /// </summary>
  254. /// <param name="container">Container to register into</param>
  255. /// <param name="collectionTypeRegistrations">Collection type registrations to register</param>
  256. protected override void RegisterCollectionTypes(TinyIoCContainer container, IEnumerable<CollectionTypeRegistration> collectionTypeRegistrations)
  257. {
  258. var configuredCollectionTypes = this.GetCollectionTypeRegistrations().ToList();
  259. collectionTypeRegistrations = configuredCollectionTypes
  260. .Concat(collectionTypeRegistrations.Where(x => !configuredCollectionTypes.Any(y => y.RegistrationType.Equals(x.RegistrationType))));
  261. foreach (var collectionTypeRegistration in collectionTypeRegistrations)
  262. {
  263. container.RegisterMultiple(collectionTypeRegistration.RegistrationType, collectionTypeRegistration.ImplementationTypes);
  264. }
  265. }
  266. /// <summary>
  267. /// Register the given instances into the container
  268. /// </summary>
  269. /// <param name="container">Container to register into</param>
  270. /// <param name="instanceRegistrations">Instance registration types</param>
  271. protected override void RegisterInstances(TinyIoCContainer container, IEnumerable<InstanceRegistration> instanceRegistrations)
  272. {
  273. instanceRegistrations = this.registeredInstances
  274. .Concat(instanceRegistrations.Where(x => !this.registeredInstances.Any(y => y.RegistrationType.Equals(x.RegistrationType))))
  275. .Where(x => !this.GetTypeRegistrations().Any(y => y.RegistrationType.Equals(x.RegistrationType)));
  276. foreach (var instanceRegistration in instanceRegistrations)
  277. {
  278. container.Register(
  279. instanceRegistration.RegistrationType,
  280. instanceRegistration.Implementation);
  281. }
  282. }
  283. /// <summary>
  284. /// Register the given module types into the request container
  285. /// </summary>
  286. /// <param name="container">Container to register into</param>
  287. /// <param name="moduleRegistrationTypes">NancyModule types</param>
  288. protected override void RegisterRequestContainerModules(TinyIoCContainer container, IEnumerable<ModuleRegistration> moduleRegistrationTypes)
  289. {
  290. foreach (var moduleRegistrationType in moduleRegistrationTypes)
  291. {
  292. container.Register(
  293. typeof(NancyModule),
  294. moduleRegistrationType.ModuleType,
  295. moduleRegistrationType.ModuleKey).
  296. AsSingleton();
  297. }
  298. }
  299. /// <summary>
  300. /// Provides an API for configuring a <see cref="ConfigurableBootstrapper"/> instance.
  301. /// </summary>
  302. public class ConfigurableBoostrapperConfigurator
  303. {
  304. private readonly ConfigurableBootstrapper bootstrapper;
  305. /// <summary>
  306. /// Initializes a new instance of the <see cref="ConfigurableBoostrapperConfigurator"/> class.
  307. /// </summary>
  308. /// <param name="bootstrapper">The bootstrapper that should be configured.</param>
  309. public ConfigurableBoostrapperConfigurator(ConfigurableBootstrapper bootstrapper)
  310. {
  311. this.bootstrapper = bootstrapper;
  312. }
  313. public ConfigurableBoostrapperConfigurator Binder(IBinder binder)
  314. {
  315. this.bootstrapper.registeredInstances.Add(
  316. new InstanceRegistration(typeof(IBinder), binder));
  317. return this;
  318. }
  319. /// <summary>
  320. /// Configures the bootstrapper to create an <see cref="IBinder"/> instance of the specified type.
  321. /// </summary>
  322. /// <typeparam name="T">The type of the <see cref="IBinder"/> that the bootstrapper should use.</typeparam>
  323. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  324. public ConfigurableBoostrapperConfigurator Binder<T>() where T : IBinder
  325. {
  326. this.bootstrapper.configuration.Binder = typeof(T);
  327. return this;
  328. }
  329. /// <summary>
  330. /// Configures the bootstrapper to use the provided instance of <see cref="INancyContextFactory"/>.
  331. /// </summary>
  332. /// <param name="contextFactory">The <see cref="INancyContextFactory"/> instance that should be used by the bootstrapper.</param>
  333. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  334. public ConfigurableBoostrapperConfigurator ContextFactory(INancyContextFactory contextFactory)
  335. {
  336. this.bootstrapper.registeredInstances.Add(
  337. new InstanceRegistration(typeof(INancyContextFactory), contextFactory));
  338. return this;
  339. }
  340. /// <summary>
  341. /// Configures the bootstrapper to create an <see cref="INancyContextFactory"/> instance of the specified type.
  342. /// </summary>
  343. /// <typeparam name="T">The type of the <see cref="INancyContextFactory"/> that the bootstrapper should use.</typeparam>
  344. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  345. public ConfigurableBoostrapperConfigurator ContextFactory<T>() where T : INancyContextFactory
  346. {
  347. this.bootstrapper.configuration.ContextFactory = typeof(T);
  348. return this;
  349. }
  350. /// <summary>
  351. /// Configures the bootstrapper to use the provided instance as a dependency.
  352. /// </summary>
  353. /// <param name="instance">The dependency instance that should be used registered with the bootstrapper.</param>
  354. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  355. /// <remarks>This method will register the instance for all the interfaces it implements and the type itself.</remarks>
  356. public ConfigurableBoostrapperConfigurator Dependency(object instance)
  357. {
  358. this.bootstrapper.registeredInstances.Add(new InstanceRegistration(instance.GetType(), instance));
  359. foreach (var interfaceType in instance.GetType().GetInterfaces())
  360. {
  361. this.bootstrapper.registeredInstances.Add(new InstanceRegistration(interfaceType, instance));
  362. }
  363. return this;
  364. }
  365. /// <summary>
  366. /// Configures the bootstrapper to register the specified type as a dependency.
  367. /// </summary>
  368. /// <typeparam name="T">The type of the dependency that should be registered with the bootstrapper.</typeparam>
  369. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  370. public ConfigurableBoostrapperConfigurator Dependency<T>(object instance)
  371. {
  372. this.bootstrapper.registeredInstances.Add(new InstanceRegistration(typeof(T), instance));
  373. return this;
  374. }
  375. /// <summary>
  376. /// Configures the bootstrapper to register the specified instances as a dependencies.
  377. /// </summary>
  378. /// <param name="dependencies">The instances of the dependencies that should be registered with the bootstrapper.</param>
  379. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  380. public ConfigurableBoostrapperConfigurator Dependencies(params object[] dependencies)
  381. {
  382. foreach (var dependency in dependencies)
  383. {
  384. this.Dependency(dependency);
  385. }
  386. return this;
  387. }
  388. /// <summary>
  389. /// Disables the auto registration behavior of the bootstrapper
  390. /// </summary>
  391. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  392. public ConfigurableBoostrapperConfigurator DisableAutoRegistration()
  393. {
  394. this.bootstrapper.disableAutoRegistration = true;
  395. return this;
  396. }
  397. /// <summary>
  398. /// Configures the bootstrapper to use the provided instance of <see cref="IErrorHandler"/>.
  399. /// </summary>
  400. /// <param name="errorHandler">The <see cref="IErrorHandler"/> instance that should be used by the bootstrapper.</param>
  401. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  402. public ConfigurableBoostrapperConfigurator ErrorHandler(IErrorHandler errorHandler)
  403. {
  404. this.bootstrapper.registeredInstances.Add(
  405. new InstanceRegistration(typeof(IErrorHandler), errorHandler));
  406. return this;
  407. }
  408. /// <summary>
  409. /// Configures the bootstrapper to create an <see cref="IErrorHandler"/> instance of the specified type.
  410. /// </summary>
  411. /// <typeparam name="T">The type of the <see cref="IErrorHandler"/> that the bootstrapper should use.</typeparam>
  412. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  413. public ConfigurableBoostrapperConfigurator ErrorHandler<T>() where T : IErrorHandler
  414. {
  415. this.bootstrapper.configuration.ErrorHandler = typeof(T);
  416. return this;
  417. }
  418. /// <summary>
  419. /// Configures the bootstrapper to use the provided instance of <see cref="IFieldNameConverter"/>.
  420. /// </summary>
  421. /// <param name="fieldNameConverter">The <see cref="IFieldNameConverter"/> instance that should be used by the bootstrapper.</param>
  422. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  423. public ConfigurableBoostrapperConfigurator FieldNameConverter(IFieldNameConverter fieldNameConverter)
  424. {
  425. this.bootstrapper.registeredInstances.Add(
  426. new InstanceRegistration(typeof(IFieldNameConverter), fieldNameConverter));
  427. return this;
  428. }
  429. /// <summary>
  430. /// Configures the bootstrapper to create an <see cref="IFieldNameConverter"/> instance of the specified type.
  431. /// </summary>
  432. /// <typeparam name="T">The type of the <see cref="IFieldNameConverter"/> that the bootstrapper should use.</typeparam>
  433. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  434. public ConfigurableBoostrapperConfigurator FieldNameConverter<T>() where T : IFieldNameConverter
  435. {
  436. this.bootstrapper.configuration.FieldNameConverter = typeof(T);
  437. return this;
  438. }
  439. /// <summary>
  440. /// Configures the bootstrapper to use the provided instance of <see cref="IModelBinderLocator"/>.
  441. /// </summary>
  442. /// <param name="modelBinderLocator">The <see cref="IModelBinderLocator"/> instance that should be used by the bootstrapper.</param>
  443. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  444. public ConfigurableBoostrapperConfigurator ModelBinderLocator(IModelBinderLocator modelBinderLocator)
  445. {
  446. this.bootstrapper.registeredInstances.Add(
  447. new InstanceRegistration(typeof(IModelBinderLocator), modelBinderLocator));
  448. return this;
  449. }
  450. /// <summary>
  451. /// Configures the bootstrapper to create an <see cref="IModelBinderLocator"/> instance of the specified type.
  452. /// </summary>
  453. /// <typeparam name="T">The type of the <see cref="IModelBinderLocator"/> that the bootstrapper should use.</typeparam>
  454. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  455. public ConfigurableBoostrapperConfigurator ModelBinderLocator<T>() where T : IModelBinderLocator
  456. {
  457. this.bootstrapper.configuration.ModelBinderLocator = typeof(T);
  458. return this;
  459. }
  460. /// <summary>
  461. /// Configures the bootstrapper to create a <see cref="NancyModule"/> instance of the specified type.
  462. /// </summary>
  463. /// <typeparam name="T">The type of the <see cref="NancyModule"/> that the bootstrapper should use.</typeparam>
  464. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  465. public ConfigurableBoostrapperConfigurator Module<T>() where T : NancyModule
  466. {
  467. return this.Modules(typeof(T));
  468. }
  469. /// <summary>
  470. /// Configures the bootstrapper to create <see cref="NancyModule"/> instances of the specified types.
  471. /// </summary>
  472. /// <param name="modules">The types of the <see cref="NancyModule"/> that the bootstrapper should use.</param>
  473. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  474. public ConfigurableBoostrapperConfigurator Modules(params Type[] modules)
  475. {
  476. var keyGenerator = new DefaultModuleKeyGenerator();
  477. var moduleRegistrations =
  478. from module in modules
  479. select new ModuleRegistration(module, keyGenerator.GetKeyForModuleType(module));
  480. this.bootstrapper.registeredTypes.AddRange(moduleRegistrations);
  481. return this;
  482. }
  483. /// <summary>
  484. /// Configures the bootstrapper to use the provided instance of <see cref="INancyEngine"/>.
  485. /// </summary>
  486. /// <param name="engine">The <see cref="INancyEngine"/> instance that should be used by the bootstrapper.</param>
  487. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  488. public ConfigurableBoostrapperConfigurator NancyEngine(INancyEngine engine)
  489. {
  490. this.bootstrapper.registeredInstances.Add(
  491. new InstanceRegistration(typeof(INancyEngine), engine));
  492. return this;
  493. }
  494. /// <summary>
  495. /// Configures the bootstrapper to create an <see cref="INancyEngine"/> instance of the specified type.
  496. /// </summary>
  497. /// <typeparam name="T">The type of the <see cref="INancyEngine"/> that the bootstrapper should use.</typeparam>
  498. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  499. public ConfigurableBoostrapperConfigurator NancyEngine<T>() where T : INancyEngine
  500. {
  501. this.bootstrapper.configuration.NancyEngine = typeof(T);
  502. return this;
  503. }
  504. /// <summary>
  505. /// Configures the bootstrapper to use the provided instance of <see cref="INancyModuleBuilder"/>.
  506. /// </summary>
  507. /// <param name="nancyModuleBuilder">The <see cref="INancyModuleBuilder"/> instance that should be used by the bootstrapper.</param>
  508. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  509. public ConfigurableBoostrapperConfigurator NancyModuleBuilder(INancyModuleBuilder nancyModuleBuilder)
  510. {
  511. this.bootstrapper.registeredInstances.Add(
  512. new InstanceRegistration(typeof(INancyModuleBuilder), nancyModuleBuilder));
  513. return this;
  514. }
  515. /// <summary>
  516. /// Configures the bootstrapper to create an <see cref="INancyModuleBuilder"/> instance of the specified type.
  517. /// </summary>
  518. /// <typeparam name="T">The type of the <see cref="INancyModuleBuilder"/> that the bootstrapper should use.</typeparam>
  519. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  520. public ConfigurableBoostrapperConfigurator NancyModuleBuilder<T>() where T : INancyModuleBuilder
  521. {
  522. this.bootstrapper.configuration.NancyModuleBuilder = typeof(T);
  523. return this;
  524. }
  525. /// <summary>
  526. /// Configures the bootstrapper to use the provided instance of <see cref="IRenderContextFactory"/>.
  527. /// </summary>
  528. /// <param name="renderContextFactory">The <see cref="IRenderContextFactory"/> instance that should be used by the bootstrapper.</param>
  529. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  530. public ConfigurableBoostrapperConfigurator RenderContextFactory(IRenderContextFactory renderContextFactory)
  531. {
  532. this.bootstrapper.registeredInstances.Add(
  533. new InstanceRegistration(typeof(IRenderContextFactory), renderContextFactory));
  534. return this;
  535. }
  536. /// <summary>
  537. /// Configures the bootstrapper to create an <see cref="IRenderContextFactory"/> instance of the specified type.
  538. /// </summary>
  539. /// <typeparam name="T">The type of the <see cref="IRenderContextFactory"/> that the bootstrapper should use.</typeparam>
  540. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  541. public ConfigurableBoostrapperConfigurator RenderContextFactory<T>() where T : IRenderContextFactory
  542. {
  543. this.bootstrapper.configuration.RenderContextFactory = typeof(T);
  544. return this;
  545. }
  546. /// <summary>
  547. /// Configures the bootstrapper to use the provided instance of <see cref="IResponseFormatter"/>.
  548. /// </summary>
  549. /// <param name="responseFormatter">The <see cref="IResponseFormatter"/> instance that should be used by the bootstrapper.</param>
  550. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  551. public ConfigurableBoostrapperConfigurator ResponseFormatter(IResponseFormatter responseFormatter)
  552. {
  553. this.bootstrapper.registeredInstances.Add(
  554. new InstanceRegistration(typeof(IResponseFormatter), responseFormatter));
  555. return this;
  556. }
  557. /// <summary>
  558. /// Configures the bootstrapper to create an <see cref="IResponseFormatter"/> instance of the specified type.
  559. /// </summary>
  560. /// <typeparam name="T">The type of the <see cref="IResponseFormatter"/> that the bootstrapper should use.</typeparam>
  561. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  562. public ConfigurableBoostrapperConfigurator ResponseFormatter<T>() where T : IResponseFormatter
  563. {
  564. this.bootstrapper.configuration.ResponseFormatter = typeof(T);
  565. return this;
  566. }
  567. /// <summary>
  568. /// Configures the bootstrapper to use the provided instance of <see cref="IRouteCache"/>.
  569. /// </summary>
  570. /// <param name="routeCache">The <see cref="IRouteCache"/> instance that should be used by the bootstrapper.</param>
  571. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  572. public ConfigurableBoostrapperConfigurator RouteCache(IRouteCache routeCache)
  573. {
  574. this.bootstrapper.registeredInstances.Add(
  575. new InstanceRegistration(typeof(IRouteCache), routeCache));
  576. return this;
  577. }
  578. /// <summary>
  579. /// Configures the bootstrapper to create an <see cref="IRouteCache"/> instance of the specified type.
  580. /// </summary>
  581. /// <typeparam name="T">The type of the <see cref="IRouteCache"/> that the bootstrapper should use.</typeparam>
  582. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  583. public ConfigurableBoostrapperConfigurator RouteCache<T>() where T : IRouteCache
  584. {
  585. this.bootstrapper.configuration.RouteCache = typeof(T);
  586. return this;
  587. }
  588. /// <summary>
  589. /// Configures the bootstrapper to use the provided instance of <see cref="IRouteCacheProvider"/>.
  590. /// </summary>
  591. /// <param name="routeCacheProvider">The <see cref="IRouteCacheProvider"/> instance that should be used by the bootstrapper.</param>
  592. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  593. public ConfigurableBoostrapperConfigurator RouteCacheProvider(IRouteCacheProvider routeCacheProvider)
  594. {
  595. this.bootstrapper.registeredInstances.Add(
  596. new InstanceRegistration(typeof(IRouteCacheProvider), routeCacheProvider));
  597. return this;
  598. }
  599. /// <summary>
  600. /// Configures the bootstrapper to create an <see cref="IRouteCacheProvider"/> instance of the specified type.
  601. /// </summary>
  602. /// <typeparam name="T">The type of the <see cref="IRouteCacheProvider"/> that the bootstrapper should use.</typeparam>
  603. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  604. public ConfigurableBoostrapperConfigurator RouteCacheProvider<T>() where T : IRouteCacheProvider
  605. {
  606. this.bootstrapper.configuration.RouteCacheProvider = typeof(T);
  607. return this;
  608. }
  609. /// <summary>
  610. /// Configures the bootstrapper to use the provided instance of <see cref="IRootPathProvider"/>.
  611. /// </summary>
  612. /// <param name="rootPathProvider">The <see cref="IRootPathProvider"/> instance that should be used by the bootstrapper.</param>
  613. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  614. public ConfigurableBoostrapperConfigurator RootPathProvider(IRootPathProvider rootPathProvider)
  615. {
  616. this.bootstrapper.registeredInstances.Add(
  617. new InstanceRegistration(typeof(IRootPathProvider), rootPathProvider));
  618. return this;
  619. }
  620. /// <summary>
  621. /// Configures the bootstrapper to create an <see cref="IRootPathProvider"/> instance of the specified type.
  622. /// </summary>
  623. /// <typeparam name="T">The type of the <see cref="IRootPathProvider"/> that the bootstrapper should use.</typeparam>
  624. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  625. public ConfigurableBoostrapperConfigurator RootPathProvider<T>() where T : IRootPathProvider
  626. {
  627. this.bootstrapper.registeredTypes.Add(
  628. new TypeRegistration(typeof(IRootPathProvider), typeof(T)));
  629. return this;
  630. }
  631. /// <summary>
  632. /// Configures the bootstrapper to use the provided instance of <see cref="IRoutePatternMatcher"/>.
  633. /// </summary>
  634. /// <param name="routePatternMatcher">The <see cref="IRoutePatternMatcher"/> instance that should be used by the bootstrapper.</param>
  635. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  636. public ConfigurableBoostrapperConfigurator RoutePatternMatcher(IRoutePatternMatcher routePatternMatcher)
  637. {
  638. this.bootstrapper.registeredInstances.Add(
  639. new InstanceRegistration(typeof(IRoutePatternMatcher), routePatternMatcher));
  640. return this;
  641. }
  642. /// <summary>
  643. /// Configures the bootstrapper to create an <see cref="IRoutePatternMatcher"/> instance of the specified type.
  644. /// </summary>
  645. /// <typeparam name="T">The type of the <see cref="IRoutePatternMatcher"/> that the bootstrapper should use.</typeparam>
  646. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  647. public ConfigurableBoostrapperConfigurator RoutePatternMatcher<T>() where T : IRoutePatternMatcher
  648. {
  649. this.bootstrapper.configuration.RoutePatternMatcher = typeof(T);
  650. return this;
  651. }
  652. /// <summary>
  653. /// Configures the bootstrapper to use the provided instance of <see cref="IRouteResolver"/>.
  654. /// </summary>
  655. /// <param name="routeResolver">The <see cref="IRouteResolver"/> instance that should be used by the bootstrapper.</param>
  656. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  657. public ConfigurableBoostrapperConfigurator RouteResolver(IRouteResolver routeResolver)
  658. {
  659. this.bootstrapper.registeredInstances.Add(
  660. new InstanceRegistration(typeof(IRouteResolver), routeResolver));
  661. return this;
  662. }
  663. /// <summary>
  664. /// Configures the bootstrapper to create an <see cref="IRouteResolver"/> instance of the specified type.
  665. /// </summary>
  666. /// <typeparam name="T">The type of the <see cref="IRouteResolver"/> that the bootstrapper should use.</typeparam>
  667. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  668. public ConfigurableBoostrapperConfigurator RouteResolver<T>() where T : IRouteResolver
  669. {
  670. this.bootstrapper.configuration.RouteResolver = typeof(T);
  671. return this;
  672. }
  673. /// <summary>
  674. /// Configures the bootstrapper to use the provided instance of <see cref="IViewCache"/>.
  675. /// </summary>
  676. /// <param name="viewCache">The <see cref="IViewCache"/> instance that should be used by the bootstrapper.</param>
  677. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  678. public ConfigurableBoostrapperConfigurator ViewCache(IViewCache viewCache)
  679. {
  680. this.bootstrapper.registeredInstances.Add(
  681. new InstanceRegistration(typeof(IViewCache), viewCache));
  682. return this;
  683. }
  684. /// <summary>
  685. /// Configures the bootstrapper to create an <see cref="IViewCache"/> instance of the specified type.
  686. /// </summary>
  687. /// <typeparam name="T">The type of the <see cref="IViewCache"/> that the bootstrapper should use.</typeparam>
  688. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  689. public ConfigurableBoostrapperConfigurator ViewCache<T>() where T : IViewCache
  690. {
  691. this.bootstrapper.configuration.ViewCache = typeof(T);
  692. return this;
  693. }
  694. /// <summary>
  695. /// Configures the bootstrapper to use the provided instance of <see cref="IViewEngine"/>.
  696. /// </summary>
  697. /// <param name="viewEngine">The <see cref="IViewEngine"/> instance that should be used by the bootstrapper.</param>
  698. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  699. public ConfigurableBoostrapperConfigurator ViewEngine(IViewEngine viewEngine)
  700. {
  701. this.bootstrapper.registeredInstances.Add(
  702. new InstanceRegistration(typeof(IViewEngine), viewEngine));
  703. return this;
  704. }
  705. /// <summary>
  706. /// Configures the bootstrapper to create an <see cref="IViewEngine"/> instance of the specified type.
  707. /// </summary>
  708. /// <typeparam name="T">The type of the <see cref="IViewEngine"/> that the bootstrapper should use.</typeparam>
  709. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  710. public ConfigurableBoostrapperConfigurator ViewEngine<T>() where T : IViewEngine
  711. {
  712. this.bootstrapper.registeredTypes.Add(
  713. new CollectionTypeRegistration(typeof(IViewEngine), new[] { typeof(T) }));
  714. return this;
  715. }
  716. /// <summary>
  717. /// Configures the bootstrapper to use the provided <see cref="IViewEngine"/> types.
  718. /// </summary>
  719. /// <param name="viewEngines">The <see cref="IViewEngine"/> types that should be used by the bootstrapper.</param>
  720. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  721. public ConfigurableBoostrapperConfigurator ViewEngines(params Type[] viewEngines)
  722. {
  723. this.bootstrapper.registeredTypes.Add(
  724. new CollectionTypeRegistration(typeof(IViewEngine), viewEngines));
  725. return this;
  726. }
  727. /// <summary>
  728. /// Configures the bootstrapper to use the provided instance of <see cref="IViewFactory"/>.
  729. /// </summary>
  730. /// <param name="viewFactory">The <see cref="IViewFactory"/> instance that should be used by the bootstrapper.</param>
  731. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  732. public ConfigurableBoostrapperConfigurator ViewFactory(IViewFactory viewFactory)
  733. {
  734. this.bootstrapper.registeredInstances.Add(
  735. new InstanceRegistration(typeof(IViewFactory), viewFactory));
  736. return this;
  737. }
  738. /// <summary>
  739. /// Configures the bootstrapper to create an <see cref="IViewFactory"/> instance of the specified type.
  740. /// </summary>
  741. /// <typeparam name="T">The type of the <see cref="IViewFactory"/> that the bootstrapper should use.</typeparam>
  742. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  743. public ConfigurableBoostrapperConfigurator ViewFactory<T>() where T : IViewFactory
  744. {
  745. this.bootstrapper.configuration.ViewFactory = typeof(T);
  746. return this;
  747. }
  748. /// <summary>
  749. /// Configures the bootstrapper to use the provided instance of <see cref="IViewLocationCache"/>.
  750. /// </summary>
  751. /// <param name="viewLocationCache">The <see cref="IViewLocationCache"/> instance that should be used by the bootstrapper.</param>
  752. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  753. public ConfigurableBoostrapperConfigurator ViewLocationCache(IViewLocationCache viewLocationCache)
  754. {
  755. this.bootstrapper.registeredInstances.Add(
  756. new InstanceRegistration(typeof(IViewLocationCache), viewLocationCache));
  757. return this;
  758. }
  759. /// <summary>
  760. /// Configures the bootstrapper to create an <see cref="IViewLocationCache"/> instance of the specified type.
  761. /// </summary>
  762. /// <typeparam name="T">The type of the <see cref="IViewLocationCache"/> that the bootstrapper should use.</typeparam>
  763. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  764. public ConfigurableBoostrapperConfigurator ViewLocationCache<T>() where T : IViewLocationCache
  765. {
  766. this.bootstrapper.configuration.ViewLocationCache = typeof(T);
  767. return this;
  768. }
  769. /// <summary>
  770. /// Configures the bootstrapper to use the provided instance of <see cref="IViewLocationProvider"/>.
  771. /// </summary>
  772. /// <param name="viewLocationProvider">The <see cref="IViewLocationProvider"/> instance that should be used by the bootstrapper.</param>
  773. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  774. public ConfigurableBoostrapperConfigurator ViewLocationProvider(IViewLocationProvider viewLocationProvider)
  775. {
  776. this.bootstrapper.registeredInstances.Add(
  777. new InstanceRegistration(typeof(IViewLocationProvider), viewLocationProvider));
  778. return this;
  779. }
  780. /// <summary>
  781. /// Configures the bootstrapper to create an <see cref="IViewLocationProvider"/> instance of the specified type.
  782. /// </summary>
  783. /// <typeparam name="T">The type of the <see cref="IViewLocationProvider"/> that the bootstrapper should use.</typeparam>
  784. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  785. public ConfigurableBoostrapperConfigurator ViewLocationProvider<T>() where T : IViewLocationProvider
  786. {
  787. this.bootstrapper.configuration.ViewLocationProvider = typeof(T);
  788. return this;
  789. }
  790. /// <summary>
  791. /// Configures the bootstrapper to use the provided instance of <see cref="IViewLocator"/>.
  792. /// </summary>
  793. /// <param name="viewLocator">The <see cref="IViewLocator"/> instance that should be used by the bootstrapper.</param>
  794. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  795. public ConfigurableBoostrapperConfigurator ViewLocator(IViewLocator viewLocator)
  796. {
  797. this.bootstrapper.registeredInstances.Add(
  798. new InstanceRegistration(typeof(IViewLocator), viewLocator));
  799. return this;
  800. }
  801. /// <summary>
  802. /// Configures the bootstrapper to create an <see cref="IViewLocator"/> instance of the specified type.
  803. /// </summary>
  804. /// <typeparam name="T">The type of the <see cref="IViewLocator"/> that the bootstrapper should use.</typeparam>
  805. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  806. public ConfigurableBoostrapperConfigurator ViewLocator<T>() where T : IViewLocator
  807. {
  808. this.bootstrapper.configuration.ViewLocator = typeof(T);
  809. return this;
  810. }
  811. /// <summary>
  812. /// Configures the bootstrapper to use the provided instance of <see cref="IViewResolver"/>.
  813. /// </summary>
  814. /// <param name="viewResolver">The <see cref="IViewResolver"/> instance that should be used by the bootstrapper.</param>
  815. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  816. public ConfigurableBoostrapperConfigurator ViewResolver(IViewResolver viewResolver)
  817. {
  818. this.bootstrapper.registeredInstances.Add(
  819. new InstanceRegistration(typeof(IViewResolver), viewResolver));
  820. return this;
  821. }
  822. /// <summary>
  823. /// Configures the bootstrapper to create an <see cref="IViewResolver"/> instance of the specified type.
  824. /// </summary>
  825. /// <typeparam name="T">The type of the <see cref="IViewResolver"/> that the bootstrapper should use.</typeparam>
  826. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  827. public ConfigurableBoostrapperConfigurator ViewResolver<T>() where T : IViewResolver
  828. {
  829. this.bootstrapper.configuration.ViewResolver = typeof(T);
  830. return this;
  831. }
  832. /// <summary>
  833. /// Configures the bootstrapper to use the provided instance of <see cref="ICsrfTokenValidator"/>.
  834. /// </summary>
  835. /// <param name="tokenValidator">The <see cref="ICsrfTokenValidator"/> instance that should be used by the bootstrapper.</param>
  836. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  837. public ConfigurableBoostrapperConfigurator CsrfTokenValidator(ICsrfTokenValidator tokenValidator)
  838. {
  839. this.bootstrapper.registeredInstances.Add(
  840. new InstanceRegistration(typeof(ICsrfTokenValidator), tokenValidator));
  841. return this;
  842. }
  843. /// <summary>
  844. /// Configures the bootstrapper to create an <see cref="ICsrfTokenValidator"/> instance of the specified type.
  845. /// </summary>
  846. /// <typeparam name="T">The type of the <see cref="ICsrfTokenValidator"/> that the bootstrapper should use.</typeparam>
  847. /// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
  848. public ConfigurableBoostrapperConfigurator CsrfTokenValidator<T>() where T : ICsrfTokenValidator
  849. {
  850. this.bootstrapper.configuration.CsrfTokenValidator = typeof(T);
  851. return this;
  852. }