PageRenderTime 34ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Nancy.Testing/ConfigurableBootstrapper.cs

http://github.com/NancyFx/Nancy
C# | 2119 lines | 1065 code | 270 blank | 784 comment | 24 complexity | 597c068d7a6a4b8714f76067aae0a459 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-3.0, Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.0, BSD-3-Clause
  1. namespace Nancy.Testing
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using Nancy.Bootstrapper;
  9. using Nancy.Configuration;
  10. using Nancy.Conventions;
  11. using Nancy.Culture;
  12. using Nancy.Diagnostics;
  13. using Nancy.ErrorHandling;
  14. using Nancy.Localization;
  15. using Nancy.ModelBinding;
  16. using Nancy.Responses.Negotiation;
  17. using Nancy.Routing;
  18. using Nancy.Routing.Constraints;
  19. using Nancy.Routing.Trie;
  20. using Nancy.Security;
  21. using Nancy.TinyIoc;
  22. using Nancy.Validation;
  23. using Nancy.ViewEngines;
  24. /// <summary>
  25. /// A Nancy bootstrapper that can be configured with either Type or Instance overrides for all Nancy types.
  26. /// </summary>
  27. public class ConfigurableBootstrapper : NancyBootstrapperWithRequestContainerBase<TinyIoCContainer>, IPipelines, INancyModuleCatalog
  28. {
  29. private readonly List<object> registeredTypes;
  30. private readonly List<InstanceRegistration> registeredInstances;
  31. private readonly NancyInternalConfiguration configuration;
  32. private readonly ConfigurableModuleCatalog catalog;
  33. private bool enableAutoRegistration;
  34. private readonly List<Action<TinyIoCContainer, IPipelines>> applicationStartupActions;
  35. private readonly List<Action<TinyIoCContainer, IPipelines, NancyContext>> requestStartupActions;
  36. private readonly Assembly nancyAssembly = typeof(NancyEngine).GetTypeInfo().Assembly;
  37. private Action<INancyEnvironment> configure;
  38. private readonly IList<Action<NancyInternalConfiguration>> configurationOverrides;
  39. /// <summary>
  40. /// Test project name suffixes that will be stripped from the test name project
  41. /// in order to try and resolve the name of the assembly that is under test so
  42. /// that all of its references can be loaded into the application domain.
  43. /// </summary>
  44. public static IList<string> TestAssemblySuffixes = new[] { "test", "tests", "unittests", "specs", "specifications" };
  45. private bool allDiscoveredModules;
  46. private bool autoRegistrations = true;
  47. private bool disableAutoApplicationStartupRegistration;
  48. private bool disableAutoRequestStartupRegistration;
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="ConfigurableBootstrapper"/> class.
  51. /// </summary>
  52. public ConfigurableBootstrapper()
  53. : this(null)
  54. {
  55. }
  56. /// <summary>
  57. /// Initializes a new instance of the <see cref="ConfigurableBootstrapper"/> class.
  58. /// </summary>
  59. /// <param name="configuration">The configuration that should be used by the bootstrapper.</param>
  60. public ConfigurableBootstrapper(Action<ConfigurableBootstrapperConfigurator> configuration)
  61. {
  62. this.catalog = new ConfigurableModuleCatalog();
  63. this.configuration = NancyInternalConfiguration.Default.Invoke(this.TypeCatalog);
  64. this.registeredTypes = new List<object>();
  65. this.registeredInstances = new List<InstanceRegistration>();
  66. this.applicationStartupActions = new List<Action<TinyIoCContainer, IPipelines>>();
  67. this.requestStartupActions = new List<Action<TinyIoCContainer, IPipelines, NancyContext>>();
  68. this.configurationOverrides = new List<Action<NancyInternalConfiguration>>();
  69. if (configuration != null)
  70. {
  71. var configurator =
  72. new ConfigurableBootstrapperConfigurator(this);
  73. configurator.StatusCodeHandler<PassThroughStatusCodeHandler>();
  74. configuration.Invoke(configurator);
  75. foreach (var configurationOverride in this.configurationOverrides)
  76. {
  77. configurationOverride.Invoke(this.configuration);
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// Configures the Nancy environment
  83. /// </summary>
  84. /// <param name="environment">The <see cref="INancyEnvironment"/> instance to configure</param>
  85. public override void Configure(INancyEnvironment environment)
  86. {
  87. if (this.configure != null)
  88. {
  89. this.configure.Invoke(environment);
  90. }
  91. }
  92. /// <summary>
  93. /// Initialise the bootstrapper - can be used for adding pre/post hooks and
  94. /// any other initialisation tasks that aren't specifically container setup
  95. /// related
  96. /// </summary>
  97. /// <param name="container">Container instance for resolving types if required.</param>
  98. protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
  99. {
  100. base.ApplicationStartup(container, pipelines);
  101. foreach (var action in this.applicationStartupActions)
  102. {
  103. action.Invoke(container, pipelines);
  104. }
  105. }
  106. /// <summary>
  107. /// Initialise the request - can be used for adding pre/post hooks and
  108. /// any other per-request initialisation tasks that aren't specifically container setup
  109. /// related
  110. /// </summary>
  111. /// <param name="container">Container</param>
  112. /// <param name="pipelines">Current pipelines</param>
  113. /// <param name="context">Current context</param>
  114. protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
  115. {
  116. base.RequestStartup(container, pipelines, context);
  117. foreach (var action in this.requestStartupActions)
  118. {
  119. action.Invoke(container, pipelines, context);
  120. }
  121. }
  122. /// <summary>
  123. /// Get all NancyModule implementation instances
  124. /// </summary>
  125. /// <param name="context">The current context</param>
  126. /// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="INancyModule"/> instances.</returns>
  127. public new IEnumerable<INancyModule> GetAllModules(NancyContext context)
  128. {
  129. return base.GetAllModules(context).Union(this.catalog.GetAllModules(context));
  130. }
  131. /// <summary>
  132. /// Get the <see cref="INancyEnvironment"/> instance.
  133. /// </summary>
  134. /// <returns>An configured <see cref="INancyEnvironment"/> instance.</returns>
  135. /// <remarks>The boostrapper must be initialised (<see cref="INancyBootstrapper.Initialise"/>) prior to calling this.</remarks>
  136. public override INancyEnvironment GetEnvironment()
  137. {
  138. return base.ApplicationContainer.Resolve<INancyEnvironment>();
  139. }
  140. /// <summary>
  141. /// Retrieve a specific module instance from the container
  142. /// </summary>
  143. /// <param name="container">Container to use</param>
  144. /// <param name="moduleType">Type of the module</param>
  145. /// <returns>INancyModule instance</returns>
  146. protected override INancyModule GetModule(TinyIoCContainer container, Type moduleType)
  147. {
  148. var module =
  149. this.catalog.GetModule(moduleType, null);
  150. if (module != null)
  151. {
  152. return module;
  153. }
  154. container.Register(typeof(INancyModule), moduleType);
  155. return container.Resolve<INancyModule>();
  156. }
  157. private IEnumerable<ModuleRegistration> GetModuleRegistrations()
  158. {
  159. return this.registeredTypes.Where(x => x is ModuleRegistration).Cast<ModuleRegistration>();
  160. }
  161. private IEnumerable<TypeRegistration> GetTypeRegistrations()
  162. {
  163. return this.registeredTypes.Where(x => x is TypeRegistration).Cast<TypeRegistration>();
  164. }
  165. private IEnumerable<CollectionTypeRegistration> GetCollectionTypeRegistrations()
  166. {
  167. return this.registeredTypes.Where(x => x.GetType() == typeof(CollectionTypeRegistration)).Cast<CollectionTypeRegistration>();
  168. }
  169. private static string GetSafePathExtension(string name)
  170. {
  171. return Path.GetExtension(name) ?? string.Empty;
  172. }
  173. private IEnumerable<Type> Resolve<T>()
  174. {
  175. var types = this.GetTypeRegistrations()
  176. .Where(x => x.RegistrationType == typeof(T))
  177. .Select(x => x.ImplementationType)
  178. .ToList();
  179. return (types.Any()) ? types : null;
  180. }
  181. /// <summary>
  182. /// Nancy internal configuration
  183. /// </summary>
  184. protected override sealed Func<ITypeCatalog, NancyInternalConfiguration> InternalConfiguration
  185. {
  186. get { return x => this.configuration; }
  187. }
  188. /// <summary>
  189. /// Nancy conventions
  190. /// </summary>
  191. protected override NancyConventions Conventions
  192. {
  193. get
  194. {
  195. var conventions = this.registeredInstances
  196. .Where(x => x.RegistrationType == typeof(NancyConventions))
  197. .Select(x => x.Implementation)
  198. .Cast<NancyConventions>()
  199. .FirstOrDefault();
  200. return conventions ?? base.Conventions;
  201. }
  202. }
  203. /// <summary>
  204. /// Gets all available module types
  205. /// </summary>
  206. protected override IEnumerable<ModuleRegistration> Modules
  207. {
  208. get
  209. {
  210. var moduleRegistrations =
  211. this.GetModuleRegistrations().ToList();
  212. if (moduleRegistrations.Any())
  213. {
  214. return moduleRegistrations;
  215. }
  216. return this.allDiscoveredModules ? base.Modules : ArrayCache.Empty<ModuleRegistration>();
  217. }
  218. }
  219. /// <summary>
  220. /// Gets the available view engine types
  221. /// </summary>
  222. protected override IEnumerable<Type> ViewEngines
  223. {
  224. get { return this.Resolve<IViewEngine>() ?? base.ViewEngines; }
  225. }
  226. /// <summary>
  227. /// Gets the available custom model binders
  228. /// </summary>
  229. protected override IEnumerable<Type> ModelBinders
  230. {
  231. get { return this.Resolve<IModelBinder>() ?? base.ModelBinders; }
  232. }
  233. /// <summary>
  234. /// Gets the available custom type converters
  235. /// </summary>
  236. protected override IEnumerable<Type> TypeConverters
  237. {
  238. get { return this.Resolve<ITypeConverter>() ?? base.TypeConverters; }
  239. }
  240. /// <summary>
  241. /// Gets the available custom body deserializers
  242. /// </summary>
  243. protected override IEnumerable<Type> BodyDeserializers
  244. {
  245. get { return this.Resolve<IBodyDeserializer>() ?? base.BodyDeserializers; }
  246. }
  247. /// <summary>
  248. /// Gets all startup tasks
  249. /// </summary>
  250. protected override IEnumerable<Type> ApplicationStartupTasks
  251. {
  252. get
  253. {
  254. var tasks = base.ApplicationStartupTasks;
  255. var user = (this.Resolve<IApplicationStartup>() ?? Enumerable.Empty<Type>()).ToArray();
  256. if (this.disableAutoApplicationStartupRegistration || user.Any())
  257. {
  258. tasks = tasks.Where(x => x.GetTypeInfo().Assembly.GetName().Name.StartsWith("Nancy", StringComparison.OrdinalIgnoreCase));
  259. }
  260. return tasks.Union(user);
  261. }
  262. }
  263. /// <summary>
  264. /// Gets all request startup tasks
  265. /// </summary>
  266. protected override IEnumerable<Type> RequestStartupTasks
  267. {
  268. get
  269. {
  270. var tasks = base.RequestStartupTasks;
  271. var user = (this.Resolve<IRequestStartup>() ?? Enumerable.Empty<Type>()).ToArray();
  272. if (this.disableAutoRequestStartupRegistration || user.Any())
  273. {
  274. tasks = tasks.Where(x => x.GetTypeInfo().Assembly.GetName().Name.StartsWith("Nancy", StringComparison.OrdinalIgnoreCase));
  275. }
  276. return tasks.Union(user);
  277. }
  278. }
  279. /// <summary>
  280. /// Gets the root path provider
  281. /// </summary>
  282. protected override IRootPathProvider RootPathProvider
  283. {
  284. get { return new DefaultRootPathProvider(); }
  285. }
  286. /// <summary>
  287. /// Configures the container using AutoRegister followed by registration
  288. /// of default INancyModuleCatalog and IRouteResolver.
  289. /// </summary>
  290. /// <param name="container">Container instance</param>
  291. protected override void ConfigureApplicationContainer(TinyIoCContainer container)
  292. {
  293. if (this.enableAutoRegistration)
  294. {
  295. container.AutoRegister();
  296. this.RegisterBootstrapperTypes(container);
  297. }
  298. RegisterTypesInternal(this.ApplicationContainer, this.GetTypeRegistrations());
  299. RegisterCollectionTypesInternal(this.ApplicationContainer, this.GetCollectionTypeRegistrations());
  300. RegisterInstancesInternal(this.ApplicationContainer, this.registeredInstances);
  301. }
  302. /// <summary>
  303. /// Creates a per request child/nested container
  304. /// </summary>
  305. /// <param name="context">Current context</param>
  306. /// <returns>Request container instance</returns>
  307. protected override TinyIoCContainer CreateRequestContainer(NancyContext context)
  308. {
  309. return this.ApplicationContainer.GetChildContainer();
  310. }
  311. /// <summary>
  312. /// Retrieve all module instances from the container
  313. /// </summary>
  314. /// <param name="container">Container to use</param>
  315. /// <returns>Collection of INancyModule instances</returns>
  316. protected override IEnumerable<INancyModule> GetAllModules(TinyIoCContainer container)
  317. {
  318. return container.ResolveAll<INancyModule>(false);
  319. }
  320. /// <summary>
  321. /// Gets the application level container
  322. /// </summary>
  323. /// <returns>Container instance</returns>
  324. protected override TinyIoCContainer GetApplicationContainer()
  325. {
  326. return new TinyIoCContainer();
  327. }
  328. /// <summary>
  329. /// Resolve INancyEngine
  330. /// </summary>
  331. /// <returns>INancyEngine implementation</returns>
  332. protected override INancyEngine GetEngineInternal()
  333. {
  334. try
  335. {
  336. return this.ApplicationContainer.Resolve<INancyEngine>();
  337. }
  338. catch (InvalidOperationException ex)
  339. {
  340. throw new InvalidOperationException(
  341. "Something went wrong when trying to satisfy one of the dependencies during composition, make sure that you've registered all new dependencies in the container and specified either a module to test, or set AllDiscoveredModules in the ConfigurableBootstrapper. Inspect the innerexception for more details.",
  342. ex.InnerException);
  343. }
  344. }
  345. /// <summary>
  346. /// Gets the diagnostics for initialization
  347. /// </summary>
  348. /// <returns>IDiagnostics implementation</returns>
  349. protected override IDiagnostics GetDiagnostics()
  350. {
  351. return this.ApplicationContainer.Resolve<IDiagnostics>();
  352. }
  353. /// <summary>
  354. /// Gets all registered startup tasks
  355. /// </summary>
  356. /// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="IApplicationStartup"/> instances. </returns>
  357. protected override IEnumerable<IApplicationStartup> GetApplicationStartupTasks()
  358. {
  359. return this.ApplicationContainer.ResolveAll<IApplicationStartup>(false);
  360. }
  361. /// <summary>
  362. /// Gets all registered request startup tasks
  363. /// </summary>
  364. /// <returns>An <see cref="System.Collections.Generic.IEnumerable{T}"/> instance containing <see cref="IRequestStartup"/> instances.</returns>
  365. protected override IEnumerable<IRequestStartup> RegisterAndGetRequestStartupTasks(TinyIoCContainer container, Type[] requestStartupTypes)
  366. {
  367. container.RegisterMultiple(typeof(IRequestStartup), requestStartupTypes);
  368. return container.ResolveAll<IRequestStartup>(false);
  369. }
  370. /// <summary>
  371. /// Gets all registered application registration tasks
  372. /// </summary>
  373. /// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="IRegistrations"/> instances.</returns>
  374. protected override IEnumerable<IRegistrations> GetRegistrationTasks()
  375. {
  376. if (this.autoRegistrations)
  377. {
  378. return this.ApplicationContainer.ResolveAll<IRegistrations>(false);
  379. }
  380. return this.ApplicationContainer.ResolveAll<IRegistrations>(false)
  381. .Where(x => x.GetType().GetTypeInfo().Assembly == nancyAssembly);
  382. }
  383. /// <summary>
  384. /// Register the bootstrapper's implemented types into the container.
  385. /// This is necessary so a user can pass in a populated container but not have
  386. /// to take the responsibility of registering things like INancyModuleCatalog manually.
  387. /// </summary>
  388. /// <param name="applicationContainer">Application container to register into</param>
  389. protected override void RegisterBootstrapperTypes(TinyIoCContainer applicationContainer)
  390. {
  391. var moduleCatalog = this.registeredInstances
  392. .Where(x => x.RegistrationType == typeof(INancyModuleCatalog))
  393. .Select(x => x.Implementation)
  394. .Cast<INancyModuleCatalog>()
  395. .FirstOrDefault() ?? this;
  396. applicationContainer.Register<INancyModuleCatalog>(moduleCatalog);
  397. }
  398. /// <summary>
  399. /// Register the default implementations of internally used types into the container as singletons
  400. /// </summary>
  401. /// <param name="container">Container to register into</param>
  402. /// <param name="typeRegistrations">Type registrations to register</param>
  403. protected override void RegisterTypes(TinyIoCContainer container, IEnumerable<TypeRegistration> typeRegistrations)
  404. {
  405. var configuredTypes =
  406. this.GetTypeRegistrations().ToList();
  407. var filtered = typeRegistrations
  408. .Where(x => !configuredTypes.Any(y => y.RegistrationType == x.RegistrationType))
  409. .Where(x => !this.registeredInstances.Any(y => y.RegistrationType == x.RegistrationType));
  410. RegisterTypesInternal(container, filtered);
  411. }
  412. private static void RegisterTypesInternal(TinyIoCContainer container, IEnumerable<TypeRegistration> filtered)
  413. {
  414. foreach (var typeRegistration in filtered)
  415. {
  416. container.Register(typeRegistration.RegistrationType, typeRegistration.ImplementationType).AsSingleton();
  417. }
  418. }
  419. /// <summary>
  420. /// Register the various collections into the container as singletons to later be resolved
  421. /// by IEnumerable{Type} constructor dependencies.
  422. /// </summary>
  423. /// <param name="container">Container to register into</param>
  424. /// <param name="collectionTypeRegistrations">Collection type registrations to register</param>
  425. protected override void RegisterCollectionTypes(TinyIoCContainer container, IEnumerable<CollectionTypeRegistration> collectionTypeRegistrations)
  426. {
  427. var configuredCollectionTypes =
  428. this.GetCollectionTypeRegistrations().ToList();
  429. var filtered = collectionTypeRegistrations
  430. .Where(x => !configuredCollectionTypes.Any(y => y.RegistrationType == x.RegistrationType));
  431. RegisterCollectionTypesInternal(container, filtered);
  432. }
  433. private static void RegisterCollectionTypesInternal(TinyIoCContainer container, IEnumerable<CollectionTypeRegistration> filtered)
  434. {
  435. foreach (var collectionTypeRegistration in filtered)
  436. {
  437. container.RegisterMultiple(collectionTypeRegistration.RegistrationType,
  438. collectionTypeRegistration.ImplementationTypes);
  439. }
  440. }
  441. /// <summary>
  442. /// Register the given instances into the container
  443. /// </summary>
  444. /// <param name="container">Container to register into</param>
  445. /// <param name="instanceRegistrations">Instance registration types</param>
  446. protected override void RegisterInstances(TinyIoCContainer container, IEnumerable<InstanceRegistration> instanceRegistrations)
  447. {
  448. var configuredInstanceRegistrations = this.GetTypeRegistrations();
  449. var fileteredInstanceRegistrations = instanceRegistrations
  450. .Where(x => !this.registeredInstances.Any(y => y.RegistrationType == x.RegistrationType))
  451. .Where(x => !configuredInstanceRegistrations.Any(y => y.RegistrationType == x.RegistrationType))
  452. .ToList();
  453. RegisterInstancesInternal(container, fileteredInstanceRegistrations);
  454. }
  455. private static void RegisterInstancesInternal(TinyIoCContainer container, IEnumerable<InstanceRegistration> fileteredInstanceRegistrations)
  456. {
  457. foreach (var instanceRegistration in fileteredInstanceRegistrations)
  458. {
  459. container.Register(
  460. instanceRegistration.RegistrationType,
  461. instanceRegistration.Implementation);
  462. }
  463. }
  464. /// <summary>
  465. /// Register the given module types into the request container
  466. /// </summary>
  467. /// <param name="container">Container to register into</param>
  468. /// <param name="moduleRegistrationTypes">NancyModule types</param>
  469. protected override void RegisterRequestContainerModules(TinyIoCContainer container, IEnumerable<ModuleRegistration> moduleRegistrationTypes)
  470. {
  471. foreach (var moduleRegistrationType in moduleRegistrationTypes)
  472. {
  473. container.Register(
  474. typeof(INancyModule),
  475. moduleRegistrationType.ModuleType,
  476. moduleRegistrationType.ModuleType.FullName).
  477. AsSingleton();
  478. }
  479. }
  480. /// <summary>
  481. /// Gets the <see cref="INancyEnvironmentConfigurator"/> used by th.
  482. /// </summary>
  483. /// <returns>An <see cref="INancyEnvironmentConfigurator"/> instance.</returns>
  484. protected override INancyEnvironmentConfigurator GetEnvironmentConfigurator()
  485. {
  486. return this.ApplicationContainer.Resolve<INancyEnvironmentConfigurator>();
  487. }
  488. /// <summary>
  489. /// Registers an <see cref="INancyEnvironment"/> instance in the container.
  490. /// </summary>
  491. /// <param name="container">The container to register into.</param>
  492. /// <param name="environment">The <see cref="INancyEnvironment"/> instance to register.</param>
  493. protected override void RegisterNancyEnvironment(TinyIoCContainer container, INancyEnvironment environment)
  494. {
  495. container.Register(environment);
  496. }
  497. /// <summary>
  498. /// <para>
  499. /// The pre-request hook
  500. /// </para>
  501. /// <para>
  502. /// The PreRequest hook is called prior to processing a request. If a hook returns
  503. /// a non-null response then processing is aborted and the response provided is
  504. /// returned.
  505. /// </para>
  506. /// </summary>
  507. public BeforePipeline BeforeRequest
  508. {
  509. get { return this.ApplicationPipelines.BeforeRequest; }
  510. set { this.ApplicationPipelines.BeforeRequest = value; }
  511. }
  512. /// <summary>
  513. /// <para>
  514. /// The post-request hook
  515. /// </para>
  516. /// <para>
  517. /// The post-request hook is called after the response is created. It can be used
  518. /// to rewrite the response or add/remove items from the context.
  519. /// </para>
  520. /// </summary>
  521. public AfterPipeline AfterRequest
  522. {
  523. get { return this.ApplicationPipelines.AfterRequest; }
  524. set { this.ApplicationPipelines.AfterRequest = value; }
  525. }
  526. /// <summary>
  527. /// <para>
  528. /// The error hook
  529. /// </para>
  530. /// <para>
  531. /// The error hook is called if an exception is thrown at any time during the pipeline.
  532. /// If no error hook exists a standard InternalServerError response is returned
  533. /// </para>
  534. /// </summary>
  535. public ErrorPipeline OnError
  536. {
  537. get { return this.ApplicationPipelines.OnError; }
  538. set { this.ApplicationPipelines.OnError = value; }
  539. }
  540. /// <summary>
  541. /// Provides an API for configuring a <see cref="ConfigurableBootstrapper"/> instance.
  542. /// </summary>
  543. public class ConfigurableBootstrapperConfigurator
  544. {
  545. private readonly ConfigurableBootstrapper bootstrapper;
  546. /// <summary>
  547. /// Initializes a new instance of the <see cref="ConfigurableBootstrapperConfigurator"/> class.
  548. /// </summary>
  549. /// <param name="bootstrapper">The bootstrapper that should be configured.</param>
  550. public ConfigurableBootstrapperConfigurator(ConfigurableBootstrapper bootstrapper)
  551. {
  552. this.bootstrapper = bootstrapper;
  553. this.Diagnostics<DisabledDiagnostics>();
  554. }
  555. public ConfigurableBootstrapperConfigurator AllDiscoveredModules()
  556. {
  557. this.bootstrapper.allDiscoveredModules = true;
  558. return this;
  559. }
  560. public ConfigurableBootstrapperConfigurator Binder(IBinder binder)
  561. {
  562. this.bootstrapper.registeredInstances.Add(
  563. new InstanceRegistration(typeof(IBinder), binder));
  564. return this;
  565. }
  566. /// <summary>
  567. /// Configures the bootstrapper to create an <see cref="IBinder"/> instance of the specified type.
  568. /// </summary>
  569. /// <typeparam name="T">The type of the <see cref="IBinder"/> that the bootstrapper should use.</typeparam>
  570. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  571. public ConfigurableBootstrapperConfigurator Binder<T>() where T : IBinder
  572. {
  573. this.bootstrapper.configurationOverrides.Add(x => x.Binder = typeof(T));
  574. return this;
  575. }
  576. /// <summary>
  577. /// Configures the <see cref="INancyEnvironment"/>.
  578. /// </summary>
  579. /// <param name="configuration">The configuration to apply to the environment.</param>
  580. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  581. public ConfigurableBootstrapperConfigurator Configure(Action<INancyEnvironment> configuration)
  582. {
  583. this.bootstrapper.configure = configuration;
  584. return this;
  585. }
  586. /// <summary>
  587. /// Configures the bootstrapper to use the provided instance of <see cref="INancyContextFactory"/>.
  588. /// </summary>
  589. /// <param name="contextFactory">The <see cref="INancyContextFactory"/> instance that should be used by the bootstrapper.</param>
  590. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  591. public ConfigurableBootstrapperConfigurator ContextFactory(INancyContextFactory contextFactory)
  592. {
  593. this.bootstrapper.registeredInstances.Add(
  594. new InstanceRegistration(typeof(INancyContextFactory), contextFactory));
  595. return this;
  596. }
  597. /// <summary>
  598. /// Configures the bootstrapper to create an <see cref="INancyContextFactory"/> instance of the specified type.
  599. /// </summary>
  600. /// <typeparam name="T">The type of the <see cref="INancyContextFactory"/> that the bootstrapper should use.</typeparam>
  601. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  602. public ConfigurableBootstrapperConfigurator ContextFactory<T>() where T : INancyContextFactory
  603. {
  604. this.bootstrapper.configurationOverrides.Add(x => x.ContextFactory = typeof(T));
  605. return this;
  606. }
  607. /// <summary>
  608. /// Configures the bootstrapper to create an <see cref="INancyDefaultConfigurationProvider"/> instance of the specified type.
  609. /// </summary>
  610. /// <typeparam name="T">The type of the <see cref="IRouteMetadataProvider"/> that the bootstrapper should use.</typeparam>
  611. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  612. public ConfigurableBootstrapperConfigurator DefaultConfigurationProvider<T>() where T : INancyDefaultConfigurationProvider
  613. {
  614. this.bootstrapper.configurationOverrides.Add(x => x.DefaultConfigurationProviders = new List<Type>(new[] { typeof(T) }));
  615. return this;
  616. }
  617. /// <summary>
  618. /// Configures the bootstrapper to use the provided <see cref="INancyDefaultConfigurationProvider"/> types.
  619. /// </summary>
  620. /// <param name="defaultConfigurationProviders">The <see cref="INancyDefaultConfigurationProvider"/> types that should be used by the bootstrapper.</param>
  621. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  622. public ConfigurableBootstrapperConfigurator DefaultConfigurationProviders(params Type[] defaultConfigurationProviders)
  623. {
  624. this.bootstrapper.configurationOverrides.Add(x => x.DefaultConfigurationProviders = defaultConfigurationProviders);
  625. return this;
  626. }
  627. /// <summary>
  628. /// Configures the bootstrapper to use the provided instances of <see cref="INancyDefaultConfigurationProvider"/>.
  629. /// </summary>
  630. /// <param name="defaultConfigurationProvider">The <see cref="INancyDefaultConfigurationProvider"/> types that should be used by the bootstrapper.</param>
  631. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  632. public ConfigurableBootstrapperConfigurator DefaultConfigurationProviders(INancyDefaultConfigurationProvider defaultConfigurationProvider)
  633. {
  634. this.bootstrapper.registeredInstances.Add(
  635. new InstanceRegistration(typeof(INancyDefaultConfigurationProvider), defaultConfigurationProvider));
  636. return this;
  637. }
  638. /// <summary>
  639. /// Configures the bootstrapper to use the provided instances of <see cref="INancyDefaultConfigurationProvider"/>.
  640. /// </summary>
  641. /// <param name="defaultConfigurationProviders">The <see cref="INancyDefaultConfigurationProvider"/> types that should be used by the bootstrapper.</param>
  642. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  643. public ConfigurableBootstrapperConfigurator DefaultConfigurationProviders(params INancyDefaultConfigurationProvider[] defaultConfigurationProviders)
  644. {
  645. foreach (var defaultConfigurationProvider in defaultConfigurationProviders)
  646. {
  647. this.bootstrapper.registeredInstances.Add(
  648. new InstanceRegistration(typeof(INancyDefaultConfigurationProvider), defaultConfigurationProvider));
  649. }
  650. return this;
  651. }
  652. /// <summary>
  653. /// Configures the bootstrapper to use the provided type as a dependency.
  654. /// </summary>
  655. /// <param name="type">The type of the dependency that should be used registered with the bootstrapper.</param>
  656. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  657. public ConfigurableBootstrapperConfigurator Dependency<T>(Type type)
  658. {
  659. this.bootstrapper.registeredTypes.Add(new TypeRegistration(typeof(T), type));
  660. return this;
  661. }
  662. /// <summary>
  663. /// Configures the bootstrapper to register the specified type as a dependency.
  664. /// </summary>
  665. /// <typeparam name="T">The type of the dependency that should be registered with the bootstrapper.</typeparam>
  666. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  667. /// <remarks>This method will register the type for all the interfaces it implements and the type itself.</remarks>
  668. public ConfigurableBootstrapperConfigurator Dependency<T>()
  669. {
  670. this.bootstrapper.registeredTypes.Add(new TypeRegistration(typeof(T), typeof(T)));
  671. foreach (var interfaceType in GetSafeInterfaces(typeof(T)))
  672. {
  673. this.bootstrapper.registeredTypes.Add(new TypeRegistration(interfaceType, typeof(T)));
  674. }
  675. return this;
  676. }
  677. /// <summary>
  678. /// Configures the bootstrapper to use the provided instance as a dependency.
  679. /// </summary>
  680. /// <param name="instance">The dependency instance that should be used registered with the bootstrapper.</param>
  681. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  682. /// <remarks>This method will register the instance for all the interfaces it implements and the type itself.</remarks>
  683. public ConfigurableBootstrapperConfigurator Dependency<T>(T instance)
  684. {
  685. this.bootstrapper.registeredInstances.Add(new InstanceRegistration(typeof(T), instance));
  686. var interfacesToRegisterBy = GetSafeInterfaces(instance.GetType()).Where(i => !i.Equals(typeof(T)));
  687. foreach (var interfaceType in interfacesToRegisterBy)
  688. {
  689. this.bootstrapper.registeredInstances.Add(new InstanceRegistration(interfaceType, instance));
  690. }
  691. return this;
  692. }
  693. private static IEnumerable<Type> GetSafeInterfaces(Type type)
  694. {
  695. return type.GetInterfaces().Where(x => x != typeof(IDisposable));
  696. }
  697. /// <summary>
  698. /// Configures the bootstrapper to register the specified types and instances as a dependencies.
  699. /// </summary>
  700. /// <param name="dependencies">An array of maps between the interfaces and instances that should be registered with the bootstrapper.</param>
  701. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  702. public ConfigurableBootstrapperConfigurator MappedDependencies<T, K>(IEnumerable<Tuple<T, K>> dependencies)
  703. where T : Type
  704. where K : class
  705. {
  706. foreach (var dependency in dependencies)
  707. {
  708. this.bootstrapper.registeredInstances.Add(
  709. new InstanceRegistration(dependency.Item1, dependency.Item2));
  710. }
  711. return this;
  712. }
  713. /// <summary>
  714. /// Configures the bootstrapper to register the specified instances as a dependencies.
  715. /// </summary>
  716. /// <param name="dependencies">The instances of the dependencies that should be registered with the bootstrapper.</param>
  717. /// <typeparam name="T">The type that the dependencies should be registered as.</typeparam>
  718. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  719. public ConfigurableBootstrapperConfigurator Dependencies<T>(params T[] dependencies)
  720. {
  721. foreach (var dependency in dependencies)
  722. {
  723. this.Dependency(dependency);
  724. }
  725. return this;
  726. }
  727. /// <summary>
  728. /// Configures the bootstrapper to use the provided types as a dependency.
  729. /// </summary>
  730. /// <param name="dependencies">The types that should be used registered as dependencies with the bootstrapper.</param>
  731. /// <typeparam name="T">The type that the dependencies should be registered as.</typeparam>
  732. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  733. public ConfigurableBootstrapperConfigurator Dependencies<T>(params Type[] dependencies)
  734. {
  735. foreach (var dependency in dependencies)
  736. {
  737. this.Dependency<T>(dependency);
  738. }
  739. return this;
  740. }
  741. /// <summary>
  742. /// Enables the auto registration behavior of the bootstrapper
  743. /// </summary>
  744. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  745. public ConfigurableBootstrapperConfigurator EnableAutoRegistration()
  746. {
  747. this.bootstrapper.enableAutoRegistration = true;
  748. return this;
  749. }
  750. /// <summary>
  751. /// Configures the bootstrapper to use the provided instance of <see cref="IStatusCodeHandler"/>.
  752. /// </summary>
  753. /// <param name="statusCodeHandlers">The <see cref="IStatusCodeHandler"/> types that should be used by the bootstrapper.</param>
  754. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  755. public ConfigurableBootstrapperConfigurator StatusCodeHandlers(params Type[] statusCodeHandlers)
  756. {
  757. this.bootstrapper.configurationOverrides.Add(x => x.StatusCodeHandlers = new List<Type>(statusCodeHandlers));
  758. return this;
  759. }
  760. /// <summary>
  761. /// Configures the bootstrapper to create an <see cref="IStatusCodeHandler"/> instance of the specified type.
  762. /// </summary>
  763. /// <typeparam name="T">The type of the <see cref="IStatusCodeHandler"/> that the bootstrapper should use.</typeparam>
  764. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  765. public ConfigurableBootstrapperConfigurator StatusCodeHandler<T>() where T : IStatusCodeHandler
  766. {
  767. this.bootstrapper.configurationOverrides.Add(x => x.StatusCodeHandlers = new List<Type>(new[] { typeof(T) }));
  768. return this;
  769. }
  770. /// <summary>
  771. /// Configures the bootstrapper to create an <see cref="INancyEnvironmentConfigurator"/> instance of the specified type.
  772. /// </summary>
  773. /// <typeparam name="T">The type of the <see cref="INancyEnvironmentConfigurator"/> that the bootstrapper should use.</typeparam>
  774. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  775. public ConfigurableBootstrapperConfigurator EnvironmentConfigurator<T>() where T : INancyEnvironmentConfigurator
  776. {
  777. this.bootstrapper.configurationOverrides.Add(x => x.EnvironmentConfigurator = typeof(T));
  778. return this;
  779. }
  780. /// <summary>
  781. /// Configures the bootstrapper to use the provided instance of <see cref="INancyEnvironmentConfigurator"/>.
  782. /// </summary>
  783. /// <param name="environmentConfigurator">The <see cref="INancyEnvironmentConfigurator"/> instance that should be used by the bootstrapper.</param>
  784. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  785. public ConfigurableBootstrapperConfigurator EnvironmentConfigurator(INancyEnvironmentConfigurator environmentConfigurator)
  786. {
  787. this.bootstrapper.registeredInstances.Add(
  788. new InstanceRegistration(typeof(INancyEnvironmentConfigurator), environmentConfigurator));
  789. return this;
  790. }
  791. /// <summary>
  792. /// Configures the bootstrapper to use the provided instance of <see cref="INancyEnvironmentFactory"/>.
  793. /// </summary>
  794. /// <param name="environmentFactory">The <see cref="INancyEnvironmentFactory"/> instance that should be used by the bootstrapper.</param>
  795. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  796. public ConfigurableBootstrapperConfigurator EnvironmentFactory(INancyEnvironmentFactory environmentFactory)
  797. {
  798. this.bootstrapper.registeredInstances.Add(
  799. new InstanceRegistration(typeof(INancyEnvironmentConfigurator), environmentFactory));
  800. return this;
  801. }
  802. /// <summary>
  803. /// Configures the bootstrapper to create an <see cref="INancyEnvironmentFactory"/> instance of the specified type.
  804. /// </summary>
  805. /// <typeparam name="T">The type of the <see cref="INancyEnvironmentFactory"/> that the bootstrapper should use.</typeparam>
  806. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  807. public ConfigurableBootstrapperConfigurator EnvironmentFactory<T>() where T : INancyEnvironmentFactory
  808. {
  809. this.bootstrapper.configurationOverrides.Add(x => x.EnvironmentFactory = typeof(T));
  810. return this;
  811. }
  812. /// <summary>
  813. /// Configures the bootstrapper to use the provided instance of <see cref="IFieldNameConverter"/>.
  814. /// </summary>
  815. /// <param name="fieldNameConverter">The <see cref="IFieldNameConverter"/> instance that should be used by the bootstrapper.</param>
  816. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  817. public ConfigurableBootstrapperConfigurator FieldNameConverter(IFieldNameConverter fieldNameConverter)
  818. {
  819. this.bootstrapper.registeredInstances.Add(
  820. new InstanceRegistration(typeof(IFieldNameConverter), fieldNameConverter));
  821. return this;
  822. }
  823. /// <summary>
  824. /// Configures the bootstrapper to create an <see cref="IFieldNameConverter"/> instance of the specified type.
  825. /// </summary>
  826. /// <typeparam name="T">The type of the <see cref="IFieldNameConverter"/> that the bootstrapper should use.</typeparam>
  827. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  828. public ConfigurableBootstrapperConfigurator FieldNameConverter<T>() where T : IFieldNameConverter
  829. {
  830. this.bootstrapper.configurationOverrides.Add(x => x.FieldNameConverter = typeof(T));
  831. return this;
  832. }
  833. /// <summary>
  834. /// Configures the bootstrapper to use the provided instance of <see cref="IModelBinderLocator"/>.
  835. /// </summary>
  836. /// <param name="modelBinderLocator">The <see cref="IModelBinderLocator"/> instance that should be used by the bootstrapper.</param>
  837. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  838. public ConfigurableBootstrapperConfigurator ModelBinderLocator(IModelBinderLocator modelBinderLocator)
  839. {
  840. this.bootstrapper.registeredInstances.Add(
  841. new InstanceRegistration(typeof(IModelBinderLocator), modelBinderLocator));
  842. return this;
  843. }
  844. /// <summary>
  845. /// Configures the bootstrapper to create an <see cref="IModelBinderLocator"/> instance of the specified type.
  846. /// </summary>
  847. /// <typeparam name="T">The type of the <see cref="IModelBinderLocator"/> that the bootstrapper should use.</typeparam>
  848. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  849. public ConfigurableBootstrapperConfigurator ModelBinderLocator<T>() where T : IModelBinderLocator
  850. {
  851. this.bootstrapper.configurationOverrides.Add(x => x.ModelBinderLocator = typeof(T));
  852. return this;
  853. }
  854. /// <summary>
  855. /// Configures the bootstrapper to create a <see cref="INancyModule"/> instance of the specified type.
  856. /// </summary>
  857. /// <typeparam name="T">The type of the <see cref="INancyModule"/> that the bootstrapper should use.</typeparam>
  858. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  859. public ConfigurableBootstrapperConfigurator Module<T>() where T : INancyModule
  860. {
  861. return this.Modules(typeof(T));
  862. }
  863. /// <summary>
  864. /// Configures the bootstrapper to register the provided <see cref="INancyModule"/> instance.
  865. /// </summary>
  866. /// <param name="module">The <see cref="INancyModule"/> instance to register.</param>
  867. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  868. public ConfigurableBootstrapperConfigurator Module(INancyModule module)
  869. {
  870. this.bootstrapper.catalog.RegisterModuleInstance(module);
  871. return this;
  872. }
  873. /// <summary>
  874. /// Configures the bootstrapper to create <see cref="INancyModule"/> instances of the specified types.
  875. /// </summary>
  876. /// <param name="modules">The types of the <see cref="INancyModule"/> that the bootstrapper should use.</param>
  877. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  878. public ConfigurableBootstrapperConfigurator Modules(params Type[] modules)
  879. {
  880. var moduleRegistrations =
  881. from module in modules
  882. select new ModuleRegistration(module);
  883. this.bootstrapper.registeredTypes.AddRange(moduleRegistrations);
  884. return this;
  885. }
  886. /// <summary>
  887. /// Configures the bootstrapper to use the provided instance of <see cref="INancyEngine"/>.
  888. /// </summary>
  889. /// <param name="engine">The <see cref="INancyEngine"/> instance that should be used by the bootstrapper.</param>
  890. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  891. public ConfigurableBootstrapperConfigurator NancyEngine(INancyEngine engine)
  892. {
  893. this.bootstrapper.registeredInstances.Add(
  894. new InstanceRegistration(typeof(INancyEngine), engine));
  895. return this;
  896. }
  897. /// <summary>
  898. /// Configures the bootstrapper to create an <see cref="INancyEngine"/> instance of the specified type.
  899. /// </summary>
  900. /// <typeparam name="T">The type of the <see cref="INancyEngine"/> that the bootstrapper should use.</typeparam>
  901. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  902. public ConfigurableBootstrapperConfigurator NancyEngine<T>() where T : INancyEngine
  903. {
  904. this.bootstrapper.configurationOverrides.Add(x => x.NancyEngine = typeof(T));
  905. return this;
  906. }
  907. /// <summary>
  908. /// Configures the bootstrapper to use the provided instance of <see cref="INancyModuleBuilder"/>.
  909. /// </summary>
  910. /// <param name="nancyModuleBuilder">The <see cref="INancyModuleBuilder"/> instance that should be used by the bootstrapper.</param>
  911. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  912. public ConfigurableBootstrapperConfigurator NancyModuleBuilder(INancyModuleBuilder nancyModuleBuilder)
  913. {
  914. this.bootstrapper.registeredInstances.Add(
  915. new InstanceRegistration(typeof(INancyModuleBuilder), nancyModuleBuilder));
  916. return this;
  917. }
  918. /// <summary>
  919. /// Configures the bootstrapper to create an <see cref="INancyModuleBuilder"/> instance of the specified type.
  920. /// </summary>
  921. /// <typeparam name="T">The type of the <see cref="INancyModuleBuilder"/> that the bootstrapper should use.</typeparam>
  922. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  923. public ConfigurableBootstrapperConfigurator NancyModuleBuilder<T>() where T : INancyModuleBuilder
  924. {
  925. this.bootstrapper.configurationOverrides.Add(x => x.NancyModuleBuilder = typeof(T));
  926. return this;
  927. }
  928. /// <summary>
  929. /// Configures the bootstrapper to use the provided instance of <see cref="IRenderContextFactory"/>.
  930. /// </summary>
  931. /// <param name="renderContextFactory">The <see cref="IRenderContextFactory"/> instance that should be used by the bootstrapper.</param>
  932. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  933. public ConfigurableBootstrapperConfigurator RenderContextFactory(IRenderContextFactory renderContextFactory)
  934. {
  935. this.bootstrapper.registeredInstances.Add(
  936. new InstanceRegistration(typeof(IRenderContextFactory), renderContextFactory));
  937. return this;
  938. }
  939. /// <summary>
  940. /// Configures the bootstrapper to create an <see cref="IRenderContextFactory"/> instance of the specified type.
  941. /// </summary>
  942. /// <typeparam name="T">The type of the <see cref="IRenderContextFactory"/> that the bootstrapper should use.</typeparam>
  943. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  944. public ConfigurableBootstrapperConfigurator RenderContextFactory<T>() where T : IRenderContextFactory
  945. {
  946. this.bootstrapper.configurationOverrides.Add(x => x.RenderContextFactory = typeof(T));
  947. return this;
  948. }
  949. /// <summary>
  950. /// Configures the bootstrapper to use the provided instance of <see cref="IRequestTraceFactory"/>.
  951. /// </summary>
  952. /// <param name="requestTraceFactory">The <see cref="IRequestTraceFactory"/> instance that should be used by the bootstrapper.</param>
  953. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  954. public ConfigurableBootstrapperConfigurator RequestTraceFactory(IRequestTraceFactory requestTraceFactory)
  955. {
  956. this.bootstrapper.registeredInstances.Add(
  957. new InstanceRegistration(typeof(IRequestTraceFactory), requestTraceFactory));
  958. return this;
  959. }
  960. /// <summary>
  961. /// Configures the bootstrapper to create an <see cref="IRequestTraceFactory"/> instance of the specified type.
  962. /// </summary>
  963. /// <typeparam name="T">The type of the <see cref="IRequestTraceFactory"/> that the bootstrapper should use.</typeparam>
  964. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  965. public ConfigurableBootstrapperConfigurator RequestTraceFactory<T>() where T : IRequestTraceFactory
  966. {
  967. this.bootstrapper.configurationOverrides.Add(x => x.RequestTraceFactory = typeof(T));
  968. return this;
  969. }
  970. /// <summary>
  971. /// Configures the bootstrapper to use the provided instance of <see cref="IResponseFormatterFactory"/>.
  972. /// </summary>
  973. /// <param name="responseFormatterFactory">The <see cref="IResponseFormatterFactory"/> instance that should be used by the bootstrapper.</param>
  974. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  975. public ConfigurableBootstrapperConfigurator ResponseFormatterFactory(IResponseFormatterFactory responseFormatterFactory)
  976. {
  977. this.bootstrapper.registeredInstances.Add(
  978. new InstanceRegistration(typeof(IResponseFormatterFactory), responseFormatterFactory));
  979. return this;
  980. }
  981. /// <summary>
  982. /// Configures the bootstrapper to create an <see cref="IResponseFormatterFactory"/> instance of the specified type.
  983. /// </summary>
  984. /// <typeparam name="T">The type of the <see cref="IResponseFormatterFactory"/> that the bootstrapper should use.</typeparam>
  985. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  986. public ConfigurableBootstrapperConfigurator ResponseFormatterFactory<T>() where T : IResponseFormatterFactory
  987. {
  988. this.bootstrapper.configurationOverrides.Add(x => x.ResponseFormatterFactory = typeof(T));
  989. return this;
  990. }
  991. /// <summary>
  992. /// Configures the bootstrapper to use the provided instance of <see cref="IRouteCache"/>.
  993. /// </summary>
  994. /// <param name="routeCache">The <see cref="IRouteCache"/> instance that should be used by the bootstrapper.</param>
  995. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  996. public ConfigurableBootstrapperConfigurator RouteCache(IRouteCache routeCache)
  997. {
  998. this.bootstrapper.registeredInstances.Add(
  999. new InstanceRegistration(typeof(IRouteCache), routeCache));
  1000. return this;
  1001. }
  1002. /// <summary>
  1003. /// Configures the bootstrapper to create an <see cref="IRouteCache"/> instance of the specified type.
  1004. /// </summary>
  1005. /// <typeparam name="T">The type of the <see cref="IRouteCache"/> that the bootstrapper should use.</typeparam>
  1006. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1007. public ConfigurableBootstrapperConfigurator RouteCache<T>() where T : IRouteCache
  1008. {
  1009. this.bootstrapper.configurationOverrides.Add(x => x.RouteCache = typeof(T));
  1010. return this;
  1011. }
  1012. /// <summary>
  1013. /// Configures the bootstrapper to use the provided instance of <see cref="IRouteCacheProvider"/>.
  1014. /// </summary>
  1015. /// <param name="routeCacheProvider">The <see cref="IRouteCacheProvider"/> instance that should be used by the bootstrapper.</param>
  1016. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1017. public ConfigurableBootstrapperConfigurator RouteCacheProvider(IRouteCacheProvider routeCacheProvider)
  1018. {
  1019. this.bootstrapper.registeredInstances.Add(
  1020. new InstanceRegistration(typeof(IRouteCacheProvider), routeCacheProvider));
  1021. return this;
  1022. }
  1023. /// <summary>
  1024. /// Configures the bootstrapper to create an <see cref="IRouteCacheProvider"/> instance of the specified type.
  1025. /// </summary>
  1026. /// <typeparam name="T">The type of the <see cref="IRouteCacheProvider"/> that the bootstrapper should use.</typeparam>
  1027. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1028. public ConfigurableBootstrapperConfigurator RouteCacheProvider<T>() where T : IRouteCacheProvider
  1029. {
  1030. this.bootstrapper.configurationOverrides.Add(x => x.RouteCacheProvider = typeof(T));
  1031. return this;
  1032. }
  1033. /// <summary>
  1034. /// Configures the bootstrapper to use the provided instance of <see cref="IRootPathProvider"/>.
  1035. /// </summary>
  1036. /// <param name="rootPathProvider">The <see cref="IRootPathProvider"/> instance that should be used by the bootstrapper.</param>
  1037. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1038. public ConfigurableBootstrapperConfigurator RootPathProvider(IRootPathProvider rootPathProvider)
  1039. {
  1040. this.bootstrapper.registeredInstances.Add(
  1041. new InstanceRegistration(typeof(IRootPathProvider), rootPathProvider));
  1042. return this;
  1043. }
  1044. /// <summary>
  1045. /// Configures the bootstrapper to create an <see cref="IRootPathProvider"/> instance of the specified type.
  1046. /// </summary>
  1047. /// <typeparam name="T">The type of the <see cref="IRootPathProvider"/> that the bootstrapper should use.</typeparam>
  1048. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1049. public ConfigurableBootstrapperConfigurator RootPathProvider<T>() where T : IRootPathProvider
  1050. {
  1051. this.bootstrapper.registeredTypes.Add(
  1052. new TypeRegistration(typeof(IRootPathProvider), typeof(T)));
  1053. return this;
  1054. }
  1055. /// <summary>
  1056. /// Configures the bootstrapper to create an <see cref="IRouteInvoker"/> instance of the specified type.
  1057. /// </summary>
  1058. /// <typeparam name="T">The type of the <see cref="IRouteInvoker"/> that the bootstrapper should use.</typeparam>
  1059. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1060. public ConfigurableBootstrapperConfigurator RouteInvoker<T>() where T : IRouteInvoker
  1061. {
  1062. this.bootstrapper.configurationOverrides.Add(x => x.RouteInvoker = typeof(T));
  1063. return this;
  1064. }
  1065. /// <summary>
  1066. /// Configures the bootstrapper to use the provided instance of <see cref="IRouteInvoker"/>.
  1067. /// </summary>
  1068. /// <param name="routeInvoker">The <see cref="IRouteInvoker"/> instance that should be used by the bootstrapper.</param>
  1069. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1070. public ConfigurableBootstrapperConfigurator RouteInvoker(IRouteInvoker routeInvoker)
  1071. {
  1072. this.bootstrapper.registeredInstances.Add(
  1073. new InstanceRegistration(typeof(IRouteInvoker), routeInvoker));
  1074. return this;
  1075. }
  1076. /// <summary>
  1077. /// Configures the bootstrapper to use the provided instance of <see cref="IRouteResolver"/>.
  1078. /// </summary>
  1079. /// <param name="routeResolver">The <see cref="IRouteResolver"/> instance that should be used by the bootstrapper.</param>
  1080. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1081. public ConfigurableBootstrapperConfigurator RouteResolver(IRouteResolver routeResolver)
  1082. {
  1083. this.bootstrapper.registeredInstances.Add(
  1084. new InstanceRegistration(typeof(IRouteResolver), routeResolver));
  1085. return this;
  1086. }
  1087. /// <summary>
  1088. /// Configures the bootstrapper to create an <see cref="IRouteResolver"/> instance of the specified type.
  1089. /// </summary>
  1090. /// <typeparam name="T">The type of the <see cref="IRouteResolver"/> that the bootstrapper should use.</typeparam>
  1091. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1092. public ConfigurableBootstrapperConfigurator RouteResolver<T>() where T : IRouteResolver
  1093. {
  1094. this.bootstrapper.configurationOverrides.Add(x => x.RouteResolver = typeof(T));
  1095. return this;
  1096. }
  1097. /// <summary>
  1098. /// Configures the bootstrapper to use the provided instance of <see cref="IModelValidatorLocator"/>.
  1099. /// </summary>
  1100. /// <param name="modelValidatorLocator">The <see cref="IModelValidatorLocator"/> instance that should be used by the bootstrapper.</param>
  1101. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1102. public ConfigurableBootstrapperConfigurator ModelValidatorLocator(IModelValidatorLocator modelValidatorLocator)
  1103. {
  1104. this.bootstrapper.registeredInstances.Add(
  1105. new InstanceRegistration(typeof(IModelValidatorLocator), modelValidatorLocator));
  1106. return this;
  1107. }
  1108. /// <summary>
  1109. /// Configures the bootstrapper to create an <see cref="IModelValidatorLocator"/> instance of the specified type.
  1110. /// </summary>
  1111. /// <typeparam name="T">The type of the <see cref="IModelValidatorLocator"/> that the bootstrapper should use.</typeparam>
  1112. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1113. public ConfigurableBootstrapperConfigurator ModelValidatorLocator<T>() where T : IModelValidatorLocator
  1114. {
  1115. this.bootstrapper.configurationOverrides.Add(x => x.ModelValidatorLocator = typeof(T));
  1116. return this;
  1117. }
  1118. /// <summary>
  1119. /// Configures the bootstrapper to use the provided instance of <see cref="IRequestDispatcher"/>.
  1120. /// </summary>
  1121. /// <param name="requestDispatcher">The <see cref="IRequestDispatcher"/> instance that should be used by the bootstrapper.</param>
  1122. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1123. public ConfigurableBootstrapperConfigurator RequestDispatcher(IRequestDispatcher requestDispatcher)
  1124. {
  1125. this.bootstrapper.registeredInstances.Add(
  1126. new InstanceRegistration(typeof(IRequestDispatcher), requestDispatcher));
  1127. return this;
  1128. }
  1129. /// <summary>
  1130. /// Configures the bootstrapper to create an <see cref="IRequestDispatcher"/> instance of the specified type.
  1131. /// </summary>
  1132. /// <typeparam name="T">The type of the <see cref="IRequestDispatcher"/> that the bootstrapper should use.</typeparam>
  1133. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1134. public ConfigurableBootstrapperConfigurator RequestDispatcher<T>() where T : IRequestDispatcher
  1135. {
  1136. this.bootstrapper.registeredTypes.Add(
  1137. new TypeRegistration(typeof(IRequestDispatcher), typeof(T)));
  1138. return this;
  1139. }
  1140. /// <summary>
  1141. /// Configures the bootstrapper to use the provided instance of <see cref="IResourceAssemblyProvider"/>.
  1142. /// </summary>
  1143. /// <param name="resourceAssemblyProvider">The <see cref="IResourceAssemblyProvider"/> instance that should be used by the bootstrapper.</param>
  1144. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1145. public ConfigurableBootstrapperConfigurator ResourceAssemblyProvider(IResourceAssemblyProvider resourceAssemblyProvider)
  1146. {
  1147. this.bootstrapper.registeredInstances.Add(
  1148. new InstanceRegistration(typeof(IResourceAssemblyProvider), resourceAssemblyProvider));
  1149. return this;
  1150. }
  1151. /// <summary>
  1152. /// Configures the bootstrapper to create an <see cref="IResourceAssemblyProvider"/> instance of the specified type.
  1153. /// </summary>
  1154. /// <typeparam name="T">The type of the <see cref="IResourceAssemblyProvider"/> that the bootstrapper should use.</typeparam>
  1155. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1156. public ConfigurableBootstrapperConfigurator ResourceAssemblyProvider<T>() where T : IResourceAssemblyProvider
  1157. {
  1158. this.bootstrapper.configurationOverrides.Add(x => x.ResourceAssemblyProvider = typeof(T));
  1159. return this;
  1160. }
  1161. /// <summary>
  1162. /// Configures the bootstrapper to use the provided instance of <see cref="IResourceReader"/>.
  1163. /// </summary>
  1164. /// <param name="resourceReader">The <see cref="IResourceReader"/> instance that should be used by the bootstrapper.</param>
  1165. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1166. public ConfigurableBootstrapperConfigurator ResourceReader(IResourceReader resourceReader)
  1167. {
  1168. this.bootstrapper.registeredInstances.Add(
  1169. new InstanceRegistration(typeof(IResourceReader), resourceReader));
  1170. return this;
  1171. }
  1172. /// <summary>
  1173. /// Configures the bootstrapper to create an <see cref="IResourceReader"/> instance of the specified type.
  1174. /// </summary>
  1175. /// <typeparam name="T">The type of the <see cref="IResourceReader"/> that the bootstrapper should use.</typeparam>
  1176. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1177. public ConfigurableBootstrapperConfigurator ResourceReader<T>() where T : IResourceReader
  1178. {
  1179. this.bootstrapper.configurationOverrides.Add(x => x.ResourceReader = typeof(T));
  1180. return this;
  1181. }
  1182. /// <summary>
  1183. /// Configures the bootstrapper to create an <see cref="IRouteDescriptionProvider"/> instance of the specified type.
  1184. /// </summary>
  1185. /// <typeparam name="T">The type of the <see cref="IRouteDescriptionProvider"/> that the bootstrapper should use.</typeparam>
  1186. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1187. public ConfigurableBootstrapperConfigurator RouteDescriptionProvider<T>() where T : IRouteDescriptionProvider
  1188. {
  1189. this.bootstrapper.registeredTypes.Add(
  1190. new TypeRegistration(typeof(IRouteDescriptionProvider), typeof(T)));
  1191. return this;
  1192. }
  1193. /// <summary>
  1194. /// Configures the bootstrapper to use the provided instance of <see cref="IRouteDescriptionProvider"/>.
  1195. /// </summary>
  1196. /// <param name="routeDescriptionProvider">The <see cref="IRouteDescriptionProvider"/> instance that should be used by the bootstrapper.</param>
  1197. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1198. public ConfigurableBootstrapperConfigurator RouteDescriptionProvider(IRouteDescriptionProvider routeDescriptionProvider)
  1199. {
  1200. this.bootstrapper.registeredInstances.Add(
  1201. new InstanceRegistration(typeof(IRouteDescriptionProvider), routeDescriptionProvider));
  1202. return this;
  1203. }
  1204. /// <summary>
  1205. /// Configures the bootstrapper to use the provided instance of <see cref="IRouteMetadataProvider"/>.
  1206. /// </summary>
  1207. /// <param name="routeMetadataProviders">The <see cref="IRouteMetadataProvider"/> instance that should be used by the bootstrapper.</param>
  1208. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1209. public ConfigurableBootstrapperConfigurator RouteMetadataProvider(IRouteMetadataProvider routeMetadataProviders)
  1210. {
  1211. this.bootstrapper.registeredInstances.Add(
  1212. new InstanceRegistration(typeof(IRouteMetadataProvider), routeMetadataProviders));
  1213. return this;
  1214. }
  1215. /// <summary>
  1216. /// Configures the bootstrapper to create an <see cref="IRouteMetadataProvider"/> instance of the specified type.
  1217. /// </summary>
  1218. /// <typeparam name="T">The type of the <see cref="IRouteMetadataProvider"/> that the bootstrapper should use.</typeparam>
  1219. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1220. public ConfigurableBootstrapperConfigurator RouteMetadataProvider<T>() where T : IRouteMetadataProvider
  1221. {
  1222. this.bootstrapper.registeredTypes.Add(
  1223. new CollectionTypeRegistration(typeof(IRouteMetadataProvider), new[] { typeof(T) }));
  1224. return this;
  1225. }
  1226. /// <summary>
  1227. /// Configures the bootstrapper to use the provided <see cref="IRouteMetadataProvider"/> types.
  1228. /// </summary>
  1229. /// <param name="routeMetadataProviders">The <see cref="IRouteMetadataProvider"/> types that should be used by the bootstrapper.</param>
  1230. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1231. public ConfigurableBootstrapperConfigurator RouteMetadataProviders(params Type[] routeMetadataProviders)
  1232. {
  1233. this.bootstrapper.registeredTypes.Add(
  1234. new CollectionTypeRegistration(typeof(IRouteMetadataProvider), routeMetadataProviders));
  1235. return this;
  1236. }
  1237. /// <summary>
  1238. /// Configures the bootstrapper to use the provided instances of <see cref="IRouteMetadataProvider"/>.
  1239. /// </summary>
  1240. /// <param name="routeMetadataProviders">The <see cref="IRouteMetadataProvider"/> types that should be used by the bootstrapper.</param>
  1241. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1242. public ConfigurableBootstrapperConfigurator RouteMetadataProviders(params IRouteMetadataProvider[] routeMetadataProviders)
  1243. {
  1244. foreach (var routeMetadataProvider in routeMetadataProviders)
  1245. {
  1246. this.bootstrapper.registeredTypes.Add(
  1247. new InstanceRegistration(typeof(IRouteMetadataProvider), routeMetadataProvider));
  1248. }
  1249. return this;
  1250. }
  1251. /// <summary>
  1252. /// Configures the bootstrapper to create an <see cref="IRouteSegmentExtractor"/> instance of the specified type.
  1253. /// </summary>
  1254. /// <typeparam name="T">The type of the <see cref="IRouteSegmentExtractor"/> that the bootstrapper should use.</typeparam>
  1255. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1256. public ConfigurableBootstrapperConfigurator RouteSegmentExtractor<T>() where T : IRouteSegmentExtractor
  1257. {
  1258. this.bootstrapper.registeredTypes.Add(
  1259. new TypeRegistration(typeof(IRouteSegmentExtractor), typeof(T)));
  1260. return this;
  1261. }
  1262. /// <summary>
  1263. /// Configures the bootstrapper to use the provided instance of <see cref="IRouteSegmentExtractor"/>.
  1264. /// </summary>
  1265. /// <param name="routeSegmentExtractor">The <see cref="IRouteSegmentExtractor"/> instance that should be used by the bootstrapper.</param>
  1266. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1267. public ConfigurableBootstrapperConfigurator RouteSegmentExtractor(IRouteSegmentExtractor routeSegmentExtractor)
  1268. {
  1269. this.bootstrapper.registeredInstances.Add(
  1270. new InstanceRegistration(typeof(IRouteSegmentExtractor), routeSegmentExtractor));
  1271. return this;
  1272. }
  1273. /// <summary>
  1274. /// Configures the bootstrapper to create an <see cref="IResponseProcessor"/> instance of the specified type.
  1275. /// </summary>
  1276. /// <typeparam name="T">The type of the <see cref="IResponseProcessor"/> that the bootstrapper should use.</typeparam>
  1277. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1278. public ConfigurableBootstrapperConfigurator ResponseProcessor<T>() where T : IResponseProcessor
  1279. {
  1280. this.bootstrapper.registeredTypes.Add(
  1281. new CollectionTypeRegistration(typeof(IResponseProcessor), new[] { typeof(T) }));
  1282. return this;
  1283. }
  1284. /// <summary>
  1285. /// Configures the bootstrapper to use the provided <see cref="IResponseProcessor"/> types.
  1286. /// </summary>
  1287. /// <param name="responseProcessors">The <see cref="IResponseProcessor"/> types that should be used by the bootstrapper.</param>
  1288. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1289. public ConfigurableBootstrapperConfigurator ResponseProcessors(params Type[] responseProcessors)
  1290. {
  1291. this.bootstrapper.registeredTypes.Add(
  1292. new CollectionTypeRegistration(typeof(IResponseProcessor), responseProcessors));
  1293. return this;
  1294. }
  1295. /// <summary>
  1296. /// Configures the bootstrapper to use the provided instance of <see cref="IRuntimeEnvironmentInformation"/>.
  1297. /// </summary>
  1298. /// <param name="runtimeEnvironmentInformation">The <see cref="IRuntimeEnvironmentInformation"/> instance that should be used by the bootstrapper.</param>
  1299. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1300. public ConfigurableBootstrapperConfigurator RuntimeEnvironmentInformation(IRuntimeEnvironmentInformation runtimeEnvironmentInformation)
  1301. {
  1302. this.bootstrapper.registeredInstances.Add(
  1303. new InstanceRegistration(typeof(IRuntimeEnvironmentInformation), runtimeEnvironmentInformation));
  1304. return this;
  1305. }
  1306. /// <summary>
  1307. /// Configures the bootstrapper to create an <see cref="IRuntimeEnvironmentInformation"/> instance of the specified type.
  1308. /// </summary>
  1309. /// <typeparam name="T">The type of the <see cref="IRuntimeEnvironmentInformation"/> that the bootstrapper should use.</typeparam>
  1310. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1311. public ConfigurableBootstrapperConfigurator RuntimeEnvironmentInformation<T>() where T : IRuntimeEnvironmentInformation
  1312. {
  1313. this.bootstrapper.configurationOverrides.Add(x => x.RuntimeEnvironmentInformation = typeof(T));
  1314. return this;
  1315. }
  1316. /// <summary>
  1317. /// Configures the bootstrapper to use the provided instance of <see cref="ITextResource"/>.
  1318. /// </summary>
  1319. /// <param name="textResource">The <see cref="ITextResource"/> instance that should be used by the bootstrapper.</param>
  1320. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1321. public ConfigurableBootstrapperConfigurator TextResource(ITextResource textResource)
  1322. {
  1323. this.bootstrapper.registeredInstances.Add(
  1324. new InstanceRegistration(typeof(ITextResource), textResource));
  1325. return this;
  1326. }
  1327. /// <summary>
  1328. /// Configures the bootstrapper to create an <see cref="ITextResource"/> instance of the specified type.
  1329. /// </summary>
  1330. /// <typeparam name="T">The type of the <see cref="ITextResource"/> that the bootstrapper should use.</typeparam>
  1331. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1332. public ConfigurableBootstrapperConfigurator TextResource<T>() where T : ITextResource
  1333. {
  1334. this.bootstrapper.configurationOverrides.Add(x => x.TextResource = typeof(T));
  1335. return this;
  1336. }
  1337. /// <summary>
  1338. /// Configures the bootstrapper to use the provided instance of <see cref="IViewCache"/>.
  1339. /// </summary>
  1340. /// <param name="viewCache">The <see cref="IViewCache"/> instance that should be used by the bootstrapper.</param>
  1341. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1342. public ConfigurableBootstrapperConfigurator ViewCache(IViewCache viewCache)
  1343. {
  1344. this.bootstrapper.registeredInstances.Add(
  1345. new InstanceRegistration(typeof(IViewCache), viewCache));
  1346. return this;
  1347. }
  1348. /// <summary>
  1349. /// Configures the bootstrapper to create an <see cref="IViewCache"/> instance of the specified type.
  1350. /// </summary>
  1351. /// <typeparam name="T">The type of the <see cref="IViewCache"/> that the bootstrapper should use.</typeparam>
  1352. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1353. public ConfigurableBootstrapperConfigurator ViewCache<T>() where T : IViewCache
  1354. {
  1355. this.bootstrapper.configurationOverrides.Add(x => x.ViewCache = typeof(T));
  1356. return this;
  1357. }
  1358. /// <summary>
  1359. /// Configures the bootstrapper to use the provided instance of <see cref="IViewEngine"/>.
  1360. /// </summary>
  1361. /// <param name="viewEngine">The <see cref="IViewEngine"/> instance that should be used by the bootstrapper.</param>
  1362. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1363. public ConfigurableBootstrapperConfigurator ViewEngine(IViewEngine viewEngine)
  1364. {
  1365. this.bootstrapper.registeredInstances.Add(
  1366. new InstanceRegistration(typeof(IViewEngine), viewEngine));
  1367. return this;
  1368. }
  1369. /// <summary>
  1370. /// Configures the bootstrapper to create an <see cref="IViewEngine"/> instance of the specified type.
  1371. /// </summary>
  1372. /// <typeparam name="T">The type of the <see cref="IViewEngine"/> that the bootstrapper should use.</typeparam>
  1373. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1374. public ConfigurableBootstrapperConfigurator ViewEngine<T>() where T : IViewEngine
  1375. {
  1376. this.bootstrapper.registeredTypes.Add(
  1377. new CollectionTypeRegistration(typeof(IViewEngine), new[] { typeof(T) }));
  1378. return this;
  1379. }
  1380. /// <summary>
  1381. /// Configures the bootstrapper to use the provided <see cref="IViewEngine"/> types.
  1382. /// </summary>
  1383. /// <param name="viewEngines">The <see cref="IViewEngine"/> types that should be used by the bootstrapper.</param>
  1384. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1385. public ConfigurableBootstrapperConfigurator ViewEngines(params Type[] viewEngines)
  1386. {
  1387. this.bootstrapper.registeredTypes.Add(
  1388. new CollectionTypeRegistration(typeof(IViewEngine), viewEngines));
  1389. return this;
  1390. }
  1391. /// <summary>
  1392. /// Configures the bootstrapper to use the provided instance of <see cref="IViewFactory"/>.
  1393. /// </summary>
  1394. /// <param name="viewFactory">The <see cref="IViewFactory"/> instance that should be used by the bootstrapper.</param>
  1395. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1396. public ConfigurableBootstrapperConfigurator ViewFactory(IViewFactory viewFactory)
  1397. {
  1398. this.bootstrapper.registeredInstances.Add(
  1399. new InstanceRegistration(typeof(IViewFactory), viewFactory));
  1400. return this;
  1401. }
  1402. /// <summary>
  1403. /// Configures the bootstrapper to create an <see cref="IViewFactory"/> instance of the specified type.
  1404. /// </summary>
  1405. /// <typeparam name="T">The type of the <see cref="IViewFactory"/> that the bootstrapper should use.</typeparam>
  1406. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1407. public ConfigurableBootstrapperConfigurator ViewFactory<T>() where T : IViewFactory
  1408. {
  1409. this.bootstrapper.configurationOverrides.Add(x => x.ViewFactory = typeof(T));
  1410. return this;
  1411. }
  1412. /// <summary>
  1413. /// Configures the bootstrapper to use the provided instance of <see cref="IViewLocationProvider"/>.
  1414. /// </summary>
  1415. /// <param name="viewLocationProvider">The <see cref="IViewLocationProvider"/> instance that should be used by the bootstrapper.</param>
  1416. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1417. public ConfigurableBootstrapperConfigurator ViewLocationProvider(IViewLocationProvider viewLocationProvider)
  1418. {
  1419. this.bootstrapper.registeredInstances.Add(
  1420. new InstanceRegistration(typeof(IViewLocationProvider), viewLocationProvider));
  1421. return this;
  1422. }
  1423. /// <summary>
  1424. /// Configures the bootstrapper to create an <see cref="IViewLocationProvider"/> instance of the specified type.
  1425. /// </summary>
  1426. /// <typeparam name="T">The type of the <see cref="IViewLocationProvider"/> that the bootstrapper should use.</typeparam>
  1427. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1428. public ConfigurableBootstrapperConfigurator ViewLocationProvider<T>() where T : IViewLocationProvider
  1429. {
  1430. this.bootstrapper.configurationOverrides.Add(x => x.ViewLocationProvider = typeof(T));
  1431. return this;
  1432. }
  1433. /// <summary>
  1434. /// Configures the bootstrapper to use the provided instance of <see cref="IViewLocator"/>.
  1435. /// </summary>
  1436. /// <param name="viewLocator">The <see cref="IViewLocator"/> instance that should be used by the bootstrapper.</param>
  1437. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1438. public ConfigurableBootstrapperConfigurator ViewLocator(IViewLocator viewLocator)
  1439. {
  1440. this.bootstrapper.registeredInstances.Add(
  1441. new InstanceRegistration(typeof(IViewLocator), viewLocator));
  1442. return this;
  1443. }
  1444. /// <summary>
  1445. /// Configures the bootstrapper to create an <see cref="IViewLocator"/> instance of the specified type.
  1446. /// </summary>
  1447. /// <typeparam name="T">The type of the <see cref="IViewLocator"/> that the bootstrapper should use.</typeparam>
  1448. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1449. public ConfigurableBootstrapperConfigurator ViewLocator<T>() where T : IViewLocator
  1450. {
  1451. this.bootstrapper.configurationOverrides.Add(x => x.ViewLocator = typeof(T));
  1452. return this;
  1453. }
  1454. /// <summary>
  1455. /// Configures the bootstrapper to use the provided instance of <see cref="IViewResolver"/>.
  1456. /// </summary>
  1457. /// <param name="viewResolver">The <see cref="IViewResolver"/> instance that should be used by the bootstrapper.</param>
  1458. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1459. public ConfigurableBootstrapperConfigurator ViewResolver(IViewResolver viewResolver)
  1460. {
  1461. this.bootstrapper.registeredInstances.Add(
  1462. new InstanceRegistration(typeof(IViewResolver), viewResolver));
  1463. return this;
  1464. }
  1465. /// <summary>
  1466. /// Configures the bootstrapper to create an <see cref="IViewResolver"/> instance of the specified type.
  1467. /// </summary>
  1468. /// <typeparam name="T">The type of the <see cref="IViewResolver"/> that the bootstrapper should use.</typeparam>
  1469. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1470. public ConfigurableBootstrapperConfigurator ViewResolver<T>() where T : IViewResolver
  1471. {
  1472. this.bootstrapper.configurationOverrides.Add(x => x.ViewResolver = typeof(T));
  1473. return this;
  1474. }
  1475. /// <summary>
  1476. /// Configures the bootstrapper to use the provided instance of <see cref="ICsrfTokenValidator"/>.
  1477. /// </summary>
  1478. /// <param name="tokenValidator">The <see cref="ICsrfTokenValidator"/> instance that should be used by the bootstrapper.</param>
  1479. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1480. public ConfigurableBootstrapperConfigurator CsrfTokenValidator(ICsrfTokenValidator tokenValidator)
  1481. {
  1482. this.bootstrapper.registeredInstances.Add(
  1483. new InstanceRegistration(typeof(ICsrfTokenValidator), tokenValidator));
  1484. return this;
  1485. }
  1486. /// <summary>
  1487. /// Configures the bootstrapper to create an <see cref="ICsrfTokenValidator"/> instance of the specified type.
  1488. /// </summary>
  1489. /// <typeparam name="T">The type of the <see cref="ICsrfTokenValidator"/> that the bootstrapper should use.</typeparam>
  1490. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1491. public ConfigurableBootstrapperConfigurator CsrfTokenValidator<T>() where T : ICsrfTokenValidator
  1492. {
  1493. this.bootstrapper.configurationOverrides.Add(x => x.CsrfTokenValidator = typeof(T));
  1494. return this;
  1495. }
  1496. /// <summary>
  1497. /// Configures the bootstrapper to use the provided instance of <see cref="IObjectSerializer"/>.
  1498. /// </summary>
  1499. /// <param name="objectSerializer">The <see cref="IObjectSerializer"/> instance that should be used by the bootstrapper.</param>
  1500. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1501. public ConfigurableBootstrapperConfigurator ObjectSerializer(IObjectSerializer objectSerializer)
  1502. {
  1503. this.bootstrapper.registeredInstances.Add(
  1504. new InstanceRegistration(typeof(IObjectSerializer), objectSerializer));
  1505. return this;
  1506. }
  1507. /// <summary>
  1508. /// Configures the bootstrapper to create an <see cref="IObjectSerializer"/> instance of the specified type.
  1509. /// </summary>
  1510. /// <typeparam name="T">The type of the <see cref="IObjectSerializer"/> that the bootstrapper should use.</typeparam>
  1511. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1512. public ConfigurableBootstrapperConfigurator ObjectSerializer<T>() where T : IObjectSerializer
  1513. {
  1514. this.bootstrapper.configurationOverrides.Add(x => x.ObjectSerializer = typeof(T));
  1515. return this;
  1516. }
  1517. /// <summary>
  1518. /// Configures the bootstrapper to use a specific serializer
  1519. /// </summary>
  1520. /// <typeparam name="T">Serializer type</typeparam>
  1521. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1522. public ConfigurableBootstrapperConfigurator Serializer<T>() where T : ISerializer
  1523. {
  1524. this.bootstrapper.configurationOverrides.Add(x => x.Serializers = new List<Type> { typeof(T) });
  1525. return this;
  1526. }
  1527. /// <summary>
  1528. /// Configures the bootstrapper to use specific serializers
  1529. /// </summary>
  1530. /// <param name="serializers">Collection of serializer types</param>
  1531. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1532. public ConfigurableBootstrapperConfigurator Serializers(params Type[] serializers)
  1533. {
  1534. this.bootstrapper.configurationOverrides.Add(x => x.Serializers = new List<Type>(serializers));
  1535. return this;
  1536. }
  1537. /// <summary>
  1538. /// Configures the bootstrapper to use the provided instance of <see cref="IDiagnostics"/>.
  1539. /// </summary>
  1540. /// <param name="diagnostics">The <see cref="IDiagnostics"/> instance that should be used by the bootstrapper.</param>
  1541. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1542. public ConfigurableBootstrapperConfigurator Diagnostics(IDiagnostics diagnostics)
  1543. {
  1544. this.bootstrapper.registeredInstances.Add(
  1545. new InstanceRegistration(typeof(IDiagnostics), diagnostics));
  1546. return this;
  1547. }
  1548. /// <summary>
  1549. /// Configures the bootstrapper to create an <see cref="IDiagnostics"/> instance of the specified type.
  1550. /// </summary>
  1551. /// <typeparam name="T">The type of the <see cref="IDiagnostics"/> that the bootstrapper should use.</typeparam>
  1552. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1553. public ConfigurableBootstrapperConfigurator Diagnostics<T>() where T : IDiagnostics
  1554. {
  1555. this.bootstrapper.configurationOverrides.Add(x => x.Diagnostics = typeof(T));
  1556. return this;
  1557. }
  1558. /// <summary>
  1559. /// Configures the bootstrapper to use the provided instance of <see cref="ICultureService "/>.
  1560. /// </summary>
  1561. /// <param name="cultureService">The <see cref="ICultureService "/> instance that should be used by the bootstrapper.</param>
  1562. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1563. public ConfigurableBootstrapperConfigurator CultureService(ICultureService cultureService)
  1564. {
  1565. this.bootstrapper.registeredInstances.Add(
  1566. new InstanceRegistration(typeof(ICultureService), cultureService));
  1567. return this;
  1568. }
  1569. /// <summary>
  1570. /// Configures the bootstrapper to create an <see cref="ICultureService"/> instance of the specified type.
  1571. /// </summary>
  1572. /// <typeparam name="T">The type of the <see cref="ICultureService"/> that the bootstrapper should use.</typeparam>
  1573. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1574. public ConfigurableBootstrapperConfigurator CultureService<T>() where T : ICultureService
  1575. {
  1576. this.bootstrapper.configurationOverrides.Add(x => x.CultureService = typeof(T));
  1577. return this;
  1578. }
  1579. /// <summary>
  1580. /// Configures the bootstrapper to use the provided instance of <see cref="ICultureService "/>.
  1581. /// </summary>
  1582. /// <param name="staticContentProvider">The <see cref="IStaticContentProvider "/> instance that should be used by the bootstrapper.</param>
  1583. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1584. public ConfigurableBootstrapperConfigurator StaticContentProvider(IStaticContentProvider staticContentProvider)
  1585. {
  1586. this.bootstrapper.registeredInstances.Add(
  1587. new InstanceRegistration(typeof(IStaticContentProvider), staticContentProvider));
  1588. return this;
  1589. }
  1590. /// <summary>
  1591. /// Configures the bootstrapper to create an <see cref="IStaticContentProvider"/> instance of the specified type.
  1592. /// </summary>
  1593. /// <typeparam name="T">The type of the <see cref="IStaticContentProvider"/> that the bootstrapper should use.</typeparam>
  1594. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1595. public ConfigurableBootstrapperConfigurator StaticContentProvider<T>() where T : IStaticContentProvider
  1596. {
  1597. this.bootstrapper.configurationOverrides.Add(x => x.StaticContentProvider = typeof(T));
  1598. return this;
  1599. }
  1600. /// <summary>
  1601. /// Configures the bootstrapper to use the provided instance of <see cref="IRouteResolverTrie "/>.
  1602. /// </summary>
  1603. /// <param name="routeResolverTrie">The <see cref="IStaticContentProvider "/> instance that should be used by the bootstrapper.</param>
  1604. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1605. public ConfigurableBootstrapperConfigurator RouteResolverTrie(IRouteResolverTrie routeResolverTrie)
  1606. {
  1607. this.bootstrapper.registeredInstances.Add(
  1608. new InstanceRegistration(typeof(IRouteResolverTrie), routeResolverTrie));
  1609. return this;
  1610. }
  1611. /// <summary>
  1612. /// Configures the bootstrapper to create an <see cref="IRouteResolverTrie"/> instance of the specified type.
  1613. /// </summary>
  1614. /// <typeparam name="T">The type of the <see cref="IRouteResolverTrie"/> that the bootstrapper should use.</typeparam>
  1615. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1616. public ConfigurableBootstrapperConfigurator RouteResolverTrie<T>() where T : IRouteResolverTrie
  1617. {
  1618. this.bootstrapper.configurationOverrides.Add(x => x.RouteResolverTrie = typeof(T));
  1619. return this;
  1620. }
  1621. /// <summary>
  1622. /// Configures the bootstrapper to use the provided instance of <see cref="ITrieNodeFactory "/>.
  1623. /// </summary>
  1624. /// <param name="nodeFactory">The <see cref="ITrieNodeFactory "/> instance that should be used by the bootstrapper.</param>
  1625. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1626. public ConfigurableBootstrapperConfigurator TrieNodeFactory(ITrieNodeFactory nodeFactory)
  1627. {
  1628. this.bootstrapper.registeredInstances.Add(
  1629. new InstanceRegistration(typeof(ITrieNodeFactory), nodeFactory));
  1630. return this;
  1631. }
  1632. /// <summary>
  1633. /// Configures the bootstrapper to create an <see cref="ITrieNodeFactory"/> instance of the specified type.
  1634. /// </summary>
  1635. /// <typeparam name="T">The type of the <see cref="ITrieNodeFactory"/> that the bootstrapper should use.</typeparam>
  1636. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1637. public ConfigurableBootstrapperConfigurator TrieNodeFactory<T>() where T : ITrieNodeFactory
  1638. {
  1639. this.bootstrapper.configurationOverrides.Add(x => x.TrieNodeFactory = typeof(T));
  1640. return this;
  1641. }
  1642. /// <summary>
  1643. /// Configures the bootstrapper to create an <see cref="IRouteSegmentConstraint"/> instance of the specified type.
  1644. /// </summary>
  1645. /// <typeparam name="T">The type of the <see cref="IRouteSegmentConstraint"/> that the bootstrapper should use.</typeparam>
  1646. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1647. public ConfigurableBootstrapperConfigurator RouteSegmentConstraint<T>() where T : IRouteSegmentConstraint
  1648. {
  1649. this.bootstrapper.configurationOverrides.Add(x => x.RouteSegmentConstraints = new List<Type> { typeof(T) });
  1650. return this;
  1651. }
  1652. /// <summary>
  1653. /// Configures the bootstrapper to use specific route segment constraints.
  1654. /// </summary>
  1655. /// <param name="types">Collection of route segment constraint types.</param>
  1656. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1657. public ConfigurableBootstrapperConfigurator RouteSegmentConstraints(params Type[] types)
  1658. {
  1659. this.bootstrapper.configurationOverrides.Add(x => x.RouteSegmentConstraints = new List<Type>(types));
  1660. return this;
  1661. }
  1662. /// <summary>
  1663. /// Configures the bootstrapper to use the provided instance of <see cref="IResponseNegotiator"/>.
  1664. /// </summary>
  1665. /// <param name="negotiator">The <see cref="IResponseNegotiator"/> instance that should be used by the bootstrapper.</param>
  1666. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1667. public ConfigurableBootstrapperConfigurator ResponseNegotiator(IResponseNegotiator negotiator)
  1668. {
  1669. this.bootstrapper.registeredInstances.Add(
  1670. new InstanceRegistration(typeof(IResponseNegotiator), negotiator));
  1671. return this;
  1672. }
  1673. /// <summary>
  1674. /// Configures the bootstrapper to create an <see cref="IResponseNegotiator"/> instance of the specified type.
  1675. /// </summary>
  1676. /// <typeparam name="T">The type of the <see cref="IResponseNegotiator"/> that the bootstrapper should use.</typeparam>
  1677. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1678. public ConfigurableBootstrapperConfigurator ResponseNegotiator<T>() where T : IResponseNegotiator
  1679. {
  1680. this.bootstrapper.configurationOverrides.Add(x => x.ResponseNegotiator = typeof(T));
  1681. return this;
  1682. }
  1683. /// <summary>
  1684. /// Configures the bootstrapper to create an <see cref="ISerializerFactory"/> instance of the specified type.
  1685. /// </summary>
  1686. /// <typeparam name="T">The type of the <see cref="ISerializerFactory"/> that the bootstrapper should use.</typeparam>
  1687. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1688. public ConfigurableBootstrapperConfigurator SerializerFactory<T>() where T : ISerializerFactory
  1689. {
  1690. this.bootstrapper.configurationOverrides.Add(x => x.SerializerFactory = typeof(T));
  1691. return this;
  1692. }
  1693. /// <summary>
  1694. /// Configures the bootstrapper to use the provided instance of <see cref="IResponseNegotiator"/>.
  1695. /// </summary>
  1696. /// <param name="serializer">The <see cref="IResponseNegotiator"/> instance that should be used by the bootstrapper.</param>
  1697. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1698. public ConfigurableBootstrapperConfigurator SerializerFactory(ISerializerFactory serializer)
  1699. {
  1700. this.bootstrapper.registeredInstances.Add(
  1701. new InstanceRegistration(typeof(ISerializerFactory), serializer));
  1702. return this;
  1703. }
  1704. /// <summary>
  1705. /// Configures the bootstrapper to use the provided instance of <see cref="IApplicationStartup"/>.
  1706. /// </summary>
  1707. /// <typeparam name="T">The type of the <see cref="IApplicationStartup"/> that the bootstrapper should use.</typeparam>
  1708. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1709. public ConfigurableBootstrapperConfigurator ApplicationStartupTask<T>() where T : IApplicationStartup
  1710. {
  1711. this.bootstrapper.registeredTypes.Add(
  1712. new TypeRegistration(typeof(IApplicationStartup), typeof(T)));
  1713. return this;
  1714. }
  1715. /// <summary>
  1716. /// Configures the bootstrapper to use the provided <see cref="IApplicationStartup"/> types.
  1717. /// </summary>
  1718. /// <param name="applicationStartupTypes">The <see cref="IApplicationStartup"/> types that should be used by the bootstrapper.</param>
  1719. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1720. public ConfigurableBootstrapperConfigurator ApplicationStartupTasks(params Type[] applicationStartupTypes)
  1721. {
  1722. foreach (var type in applicationStartupTypes)
  1723. {
  1724. this.bootstrapper.registeredTypes.Add(
  1725. new TypeRegistration(typeof(IApplicationStartup), type));
  1726. }
  1727. return this;
  1728. }
  1729. /// <summary>
  1730. /// Configures the bootstrapper to use the provided instance of <see cref="IRequestStartup"/>.
  1731. /// </summary>
  1732. /// <typeparam name="T">The type of the <see cref="IApplicationStartup"/> that the bootstrapper should use.</typeparam>
  1733. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1734. public ConfigurableBootstrapperConfigurator RequestStartupTask<T>() where T : IRequestStartup
  1735. {
  1736. this.bootstrapper.registeredTypes.Add(
  1737. new TypeRegistration(typeof(IRequestStartup), typeof(T)));
  1738. return this;
  1739. }
  1740. /// <summary>
  1741. /// Configures the bootstrapper to use the provided <see cref="IRequestStartup"/> types.
  1742. /// </summary>
  1743. /// <param name="requestStartupTypes">The <see cref="IRequestStartup"/> types that should be used by the bootstrapper.</param>
  1744. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1745. public ConfigurableBootstrapperConfigurator RequestStartupTasks(params Type[] requestStartupTypes)
  1746. {
  1747. foreach (var type in requestStartupTypes)
  1748. {
  1749. this.bootstrapper.registeredTypes.Add(
  1750. new TypeRegistration(typeof(IRequestStartup), type));
  1751. }
  1752. return this;
  1753. }
  1754. /// <summary>
  1755. /// Disables automatic registration of user-defined <see cref="IApplicationStartup"/> instances. It
  1756. /// will not prevent auto-registration of implementations bundled with Nancy.
  1757. /// </summary>
  1758. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1759. public ConfigurableBootstrapperConfigurator DisableAutoApplicationStartupRegistration()
  1760. {
  1761. this.bootstrapper.disableAutoApplicationStartupRegistration = true;
  1762. return this;
  1763. }
  1764. /// <summary>
  1765. /// Disables automatic registration of user-defined <see cref="IRequestStartup"/> instances. It
  1766. /// will not prevent auto-registration of implementations bundled with Nancy.
  1767. /// </summary>
  1768. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1769. public ConfigurableBootstrapperConfigurator DisableAutoRequestStartupRegistration()
  1770. {
  1771. this.bootstrapper.disableAutoRequestStartupRegistration = true;
  1772. return this;
  1773. }
  1774. /// <summary>
  1775. /// Adds a hook to the application startup pipeline. This can be called multiple times to add
  1776. /// more hooks.
  1777. /// </summary>
  1778. /// <param name="action">The pipeline hook.</param>
  1779. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1780. public ConfigurableBootstrapperConfigurator ApplicationStartup(Action<TinyIoCContainer, IPipelines> action)
  1781. {
  1782. this.bootstrapper.applicationStartupActions.Add(action);
  1783. return this;
  1784. }
  1785. /// <summary>
  1786. /// Adds a hook to the request startup pipeline. This can be called multiple times to add
  1787. /// more hooks.
  1788. /// </summary>
  1789. /// <param name="action">The pipeline hook.</param>
  1790. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1791. public ConfigurableBootstrapperConfigurator RequestStartup(Action<TinyIoCContainer, IPipelines, NancyContext> action)
  1792. {
  1793. this.bootstrapper.requestStartupActions.Add(action);
  1794. return this;
  1795. }
  1796. /// <summary>
  1797. /// Disables registrations performed by <see cref="IRegistrations"/> instances.
  1798. /// </summary>
  1799. /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
  1800. public ConfigurableBootstrapperConfigurator DisableAutoRegistrations()
  1801. {
  1802. this.bootstrapper.autoRegistrations = false;
  1803. return this;
  1804. }
  1805. }
  1806. /// <summary>
  1807. /// Provides the functionality to register <see cref="INancyModule"/> instances in a <see cref="INancyModuleCatalog"/>.
  1808. /// </summary>
  1809. public class ConfigurableModuleCatalog : INancyModuleCatalog
  1810. {
  1811. private readonly IDictionary<string, INancyModule> moduleInstances;
  1812. /// <summary>
  1813. /// Initializes a new instance of the <see cref="ConfigurableModuleCatalog"/> class.
  1814. /// </summary>
  1815. public ConfigurableModuleCatalog()
  1816. {
  1817. this.moduleInstances = new Dictionary<string, INancyModule>();
  1818. }
  1819. /// <summary>
  1820. /// Get all NancyModule implementation instances - should be per-request lifetime
  1821. /// </summary>
  1822. /// <param name="context">The current context</param>
  1823. /// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="INancyModule"/> instances.</returns>
  1824. public IEnumerable<INancyModule> GetAllModules(NancyContext context)
  1825. {
  1826. return this.moduleInstances.Values;
  1827. }
  1828. /// <summary>
  1829. /// Retrieves a specific <see cref="INancyModule"/> implementation - should be per-request lifetime
  1830. /// </summary>
  1831. /// <param name="moduleType">Module type</param>
  1832. /// <param name="context">The current context</param>
  1833. /// <returns>The <see cref="INancyModule"/> instance</returns>
  1834. public INancyModule GetModule(Type moduleType, NancyContext context)
  1835. {
  1836. INancyModule module;
  1837. return this.moduleInstances.TryGetValue(moduleType.FullName, out module) ? module : null;
  1838. }
  1839. /// <summary>
  1840. /// Registers a <see cref="INancyModule"/> instance.
  1841. /// </summary>
  1842. /// <param name="module">The <see cref="INancyModule"/> instance to register.</param>
  1843. public void RegisterModuleInstance(INancyModule module)
  1844. {
  1845. this.moduleInstances.Add(module.GetType().FullName, module);
  1846. }
  1847. }
  1848. }
  1849. }