/src/Nancy.Testing/ConfigurableBootstrapper.cs
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
Large files files are truncated, but you can click here to view the full file
- namespace Nancy.Testing
- {
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using Nancy.Bootstrapper;
- using Nancy.Configuration;
- using Nancy.Conventions;
- using Nancy.Culture;
- using Nancy.Diagnostics;
- using Nancy.ErrorHandling;
- using Nancy.Localization;
- using Nancy.ModelBinding;
- using Nancy.Responses.Negotiation;
- using Nancy.Routing;
- using Nancy.Routing.Constraints;
- using Nancy.Routing.Trie;
- using Nancy.Security;
- using Nancy.TinyIoc;
- using Nancy.Validation;
- using Nancy.ViewEngines;
-
- /// <summary>
- /// A Nancy bootstrapper that can be configured with either Type or Instance overrides for all Nancy types.
- /// </summary>
- public class ConfigurableBootstrapper : NancyBootstrapperWithRequestContainerBase<TinyIoCContainer>, IPipelines, INancyModuleCatalog
- {
- private readonly List<object> registeredTypes;
- private readonly List<InstanceRegistration> registeredInstances;
- private readonly NancyInternalConfiguration configuration;
- private readonly ConfigurableModuleCatalog catalog;
- private bool enableAutoRegistration;
- private readonly List<Action<TinyIoCContainer, IPipelines>> applicationStartupActions;
- private readonly List<Action<TinyIoCContainer, IPipelines, NancyContext>> requestStartupActions;
- private readonly Assembly nancyAssembly = typeof(NancyEngine).GetTypeInfo().Assembly;
- private Action<INancyEnvironment> configure;
- private readonly IList<Action<NancyInternalConfiguration>> configurationOverrides;
-
- /// <summary>
- /// Test project name suffixes that will be stripped from the test name project
- /// in order to try and resolve the name of the assembly that is under test so
- /// that all of its references can be loaded into the application domain.
- /// </summary>
- public static IList<string> TestAssemblySuffixes = new[] { "test", "tests", "unittests", "specs", "specifications" };
-
- private bool allDiscoveredModules;
- private bool autoRegistrations = true;
- private bool disableAutoApplicationStartupRegistration;
- private bool disableAutoRequestStartupRegistration;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="ConfigurableBootstrapper"/> class.
- /// </summary>
- public ConfigurableBootstrapper()
- : this(null)
- {
- }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="ConfigurableBootstrapper"/> class.
- /// </summary>
- /// <param name="configuration">The configuration that should be used by the bootstrapper.</param>
- public ConfigurableBootstrapper(Action<ConfigurableBootstrapperConfigurator> configuration)
- {
- this.catalog = new ConfigurableModuleCatalog();
- this.configuration = NancyInternalConfiguration.Default.Invoke(this.TypeCatalog);
- this.registeredTypes = new List<object>();
- this.registeredInstances = new List<InstanceRegistration>();
- this.applicationStartupActions = new List<Action<TinyIoCContainer, IPipelines>>();
- this.requestStartupActions = new List<Action<TinyIoCContainer, IPipelines, NancyContext>>();
- this.configurationOverrides = new List<Action<NancyInternalConfiguration>>();
-
- if (configuration != null)
- {
- var configurator =
- new ConfigurableBootstrapperConfigurator(this);
-
- configurator.StatusCodeHandler<PassThroughStatusCodeHandler>();
- configuration.Invoke(configurator);
-
- foreach (var configurationOverride in this.configurationOverrides)
- {
- configurationOverride.Invoke(this.configuration);
- }
- }
- }
-
- /// <summary>
- /// Configures the Nancy environment
- /// </summary>
- /// <param name="environment">The <see cref="INancyEnvironment"/> instance to configure</param>
- public override void Configure(INancyEnvironment environment)
- {
- if (this.configure != null)
- {
- this.configure.Invoke(environment);
- }
- }
-
- /// <summary>
- /// Initialise the bootstrapper - can be used for adding pre/post hooks and
- /// any other initialisation tasks that aren't specifically container setup
- /// related
- /// </summary>
- /// <param name="container">Container instance for resolving types if required.</param>
- protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
- {
- base.ApplicationStartup(container, pipelines);
- foreach (var action in this.applicationStartupActions)
- {
- action.Invoke(container, pipelines);
- }
- }
-
- /// <summary>
- /// Initialise the request - can be used for adding pre/post hooks and
- /// any other per-request initialisation tasks that aren't specifically container setup
- /// related
- /// </summary>
- /// <param name="container">Container</param>
- /// <param name="pipelines">Current pipelines</param>
- /// <param name="context">Current context</param>
- protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
- {
- base.RequestStartup(container, pipelines, context);
- foreach (var action in this.requestStartupActions)
- {
- action.Invoke(container, pipelines, context);
- }
- }
-
- /// <summary>
- /// Get all NancyModule implementation instances
- /// </summary>
- /// <param name="context">The current context</param>
- /// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="INancyModule"/> instances.</returns>
- public new IEnumerable<INancyModule> GetAllModules(NancyContext context)
- {
- return base.GetAllModules(context).Union(this.catalog.GetAllModules(context));
- }
-
- /// <summary>
- /// Get the <see cref="INancyEnvironment"/> instance.
- /// </summary>
- /// <returns>An configured <see cref="INancyEnvironment"/> instance.</returns>
- /// <remarks>The boostrapper must be initialised (<see cref="INancyBootstrapper.Initialise"/>) prior to calling this.</remarks>
- public override INancyEnvironment GetEnvironment()
- {
- return base.ApplicationContainer.Resolve<INancyEnvironment>();
- }
-
- /// <summary>
- /// Retrieve a specific module instance from the container
- /// </summary>
- /// <param name="container">Container to use</param>
- /// <param name="moduleType">Type of the module</param>
- /// <returns>INancyModule instance</returns>
- protected override INancyModule GetModule(TinyIoCContainer container, Type moduleType)
- {
- var module =
- this.catalog.GetModule(moduleType, null);
-
- if (module != null)
- {
- return module;
- }
-
- container.Register(typeof(INancyModule), moduleType);
- return container.Resolve<INancyModule>();
- }
-
- private IEnumerable<ModuleRegistration> GetModuleRegistrations()
- {
- return this.registeredTypes.Where(x => x is ModuleRegistration).Cast<ModuleRegistration>();
- }
-
- private IEnumerable<TypeRegistration> GetTypeRegistrations()
- {
- return this.registeredTypes.Where(x => x is TypeRegistration).Cast<TypeRegistration>();
- }
-
- private IEnumerable<CollectionTypeRegistration> GetCollectionTypeRegistrations()
- {
- return this.registeredTypes.Where(x => x.GetType() == typeof(CollectionTypeRegistration)).Cast<CollectionTypeRegistration>();
- }
-
- private static string GetSafePathExtension(string name)
- {
- return Path.GetExtension(name) ?? string.Empty;
- }
-
- private IEnumerable<Type> Resolve<T>()
- {
- var types = this.GetTypeRegistrations()
- .Where(x => x.RegistrationType == typeof(T))
- .Select(x => x.ImplementationType)
- .ToList();
-
- return (types.Any()) ? types : null;
- }
-
- /// <summary>
- /// Nancy internal configuration
- /// </summary>
- protected override sealed Func<ITypeCatalog, NancyInternalConfiguration> InternalConfiguration
- {
- get { return x => this.configuration; }
- }
-
- /// <summary>
- /// Nancy conventions
- /// </summary>
- protected override NancyConventions Conventions
- {
- get
- {
- var conventions = this.registeredInstances
- .Where(x => x.RegistrationType == typeof(NancyConventions))
- .Select(x => x.Implementation)
- .Cast<NancyConventions>()
- .FirstOrDefault();
-
- return conventions ?? base.Conventions;
- }
- }
-
- /// <summary>
- /// Gets all available module types
- /// </summary>
- protected override IEnumerable<ModuleRegistration> Modules
- {
- get
- {
- var moduleRegistrations =
- this.GetModuleRegistrations().ToList();
-
- if (moduleRegistrations.Any())
- {
- return moduleRegistrations;
- }
-
- return this.allDiscoveredModules ? base.Modules : ArrayCache.Empty<ModuleRegistration>();
- }
- }
-
- /// <summary>
- /// Gets the available view engine types
- /// </summary>
- protected override IEnumerable<Type> ViewEngines
- {
- get { return this.Resolve<IViewEngine>() ?? base.ViewEngines; }
- }
-
- /// <summary>
- /// Gets the available custom model binders
- /// </summary>
- protected override IEnumerable<Type> ModelBinders
- {
- get { return this.Resolve<IModelBinder>() ?? base.ModelBinders; }
- }
-
- /// <summary>
- /// Gets the available custom type converters
- /// </summary>
- protected override IEnumerable<Type> TypeConverters
- {
- get { return this.Resolve<ITypeConverter>() ?? base.TypeConverters; }
- }
-
- /// <summary>
- /// Gets the available custom body deserializers
- /// </summary>
- protected override IEnumerable<Type> BodyDeserializers
- {
- get { return this.Resolve<IBodyDeserializer>() ?? base.BodyDeserializers; }
- }
-
- /// <summary>
- /// Gets all startup tasks
- /// </summary>
- protected override IEnumerable<Type> ApplicationStartupTasks
- {
- get
- {
- var tasks = base.ApplicationStartupTasks;
-
- var user = (this.Resolve<IApplicationStartup>() ?? Enumerable.Empty<Type>()).ToArray();
-
- if (this.disableAutoApplicationStartupRegistration || user.Any())
- {
- tasks = tasks.Where(x => x.GetTypeInfo().Assembly.GetName().Name.StartsWith("Nancy", StringComparison.OrdinalIgnoreCase));
- }
-
- return tasks.Union(user);
- }
- }
-
- /// <summary>
- /// Gets all request startup tasks
- /// </summary>
- protected override IEnumerable<Type> RequestStartupTasks
- {
- get
- {
- var tasks = base.RequestStartupTasks;
-
- var user = (this.Resolve<IRequestStartup>() ?? Enumerable.Empty<Type>()).ToArray();
-
- if (this.disableAutoRequestStartupRegistration || user.Any())
- {
- tasks = tasks.Where(x => x.GetTypeInfo().Assembly.GetName().Name.StartsWith("Nancy", StringComparison.OrdinalIgnoreCase));
- }
-
- return tasks.Union(user);
- }
- }
-
- /// <summary>
- /// Gets the root path provider
- /// </summary>
- protected override IRootPathProvider RootPathProvider
- {
- get { return new DefaultRootPathProvider(); }
- }
-
- /// <summary>
- /// Configures the container using AutoRegister followed by registration
- /// of default INancyModuleCatalog and IRouteResolver.
- /// </summary>
- /// <param name="container">Container instance</param>
- protected override void ConfigureApplicationContainer(TinyIoCContainer container)
- {
- if (this.enableAutoRegistration)
- {
- container.AutoRegister();
- this.RegisterBootstrapperTypes(container);
- }
-
- RegisterTypesInternal(this.ApplicationContainer, this.GetTypeRegistrations());
- RegisterCollectionTypesInternal(this.ApplicationContainer, this.GetCollectionTypeRegistrations());
- RegisterInstancesInternal(this.ApplicationContainer, this.registeredInstances);
- }
-
- /// <summary>
- /// Creates a per request child/nested container
- /// </summary>
- /// <param name="context">Current context</param>
- /// <returns>Request container instance</returns>
- protected override TinyIoCContainer CreateRequestContainer(NancyContext context)
- {
- return this.ApplicationContainer.GetChildContainer();
- }
-
- /// <summary>
- /// Retrieve all module instances from the container
- /// </summary>
- /// <param name="container">Container to use</param>
- /// <returns>Collection of INancyModule instances</returns>
- protected override IEnumerable<INancyModule> GetAllModules(TinyIoCContainer container)
- {
- return container.ResolveAll<INancyModule>(false);
- }
-
- /// <summary>
- /// Gets the application level container
- /// </summary>
- /// <returns>Container instance</returns>
- protected override TinyIoCContainer GetApplicationContainer()
- {
- return new TinyIoCContainer();
- }
-
- /// <summary>
- /// Resolve INancyEngine
- /// </summary>
- /// <returns>INancyEngine implementation</returns>
- protected override INancyEngine GetEngineInternal()
- {
- try
- {
- return this.ApplicationContainer.Resolve<INancyEngine>();
- }
- catch (InvalidOperationException ex)
- {
- throw new InvalidOperationException(
- "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.",
- ex.InnerException);
- }
- }
-
-
- /// <summary>
- /// Gets the diagnostics for initialization
- /// </summary>
- /// <returns>IDiagnostics implementation</returns>
- protected override IDiagnostics GetDiagnostics()
- {
- return this.ApplicationContainer.Resolve<IDiagnostics>();
- }
-
- /// <summary>
- /// Gets all registered startup tasks
- /// </summary>
- /// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="IApplicationStartup"/> instances. </returns>
- protected override IEnumerable<IApplicationStartup> GetApplicationStartupTasks()
- {
- return this.ApplicationContainer.ResolveAll<IApplicationStartup>(false);
- }
-
- /// <summary>
- /// Gets all registered request startup tasks
- /// </summary>
- /// <returns>An <see cref="System.Collections.Generic.IEnumerable{T}"/> instance containing <see cref="IRequestStartup"/> instances.</returns>
- protected override IEnumerable<IRequestStartup> RegisterAndGetRequestStartupTasks(TinyIoCContainer container, Type[] requestStartupTypes)
- {
- container.RegisterMultiple(typeof(IRequestStartup), requestStartupTypes);
-
- return container.ResolveAll<IRequestStartup>(false);
- }
-
- /// <summary>
- /// Gets all registered application registration tasks
- /// </summary>
- /// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="IRegistrations"/> instances.</returns>
- protected override IEnumerable<IRegistrations> GetRegistrationTasks()
- {
- if (this.autoRegistrations)
- {
- return this.ApplicationContainer.ResolveAll<IRegistrations>(false);
- }
-
- return this.ApplicationContainer.ResolveAll<IRegistrations>(false)
- .Where(x => x.GetType().GetTypeInfo().Assembly == nancyAssembly);
- }
-
- /// <summary>
- /// Register the bootstrapper's implemented types into the container.
- /// This is necessary so a user can pass in a populated container but not have
- /// to take the responsibility of registering things like INancyModuleCatalog manually.
- /// </summary>
- /// <param name="applicationContainer">Application container to register into</param>
- protected override void RegisterBootstrapperTypes(TinyIoCContainer applicationContainer)
- {
- var moduleCatalog = this.registeredInstances
- .Where(x => x.RegistrationType == typeof(INancyModuleCatalog))
- .Select(x => x.Implementation)
- .Cast<INancyModuleCatalog>()
- .FirstOrDefault() ?? this;
-
- applicationContainer.Register<INancyModuleCatalog>(moduleCatalog);
- }
-
- /// <summary>
- /// Register the default implementations of internally used types into the container as singletons
- /// </summary>
- /// <param name="container">Container to register into</param>
- /// <param name="typeRegistrations">Type registrations to register</param>
- protected override void RegisterTypes(TinyIoCContainer container, IEnumerable<TypeRegistration> typeRegistrations)
- {
- var configuredTypes =
- this.GetTypeRegistrations().ToList();
-
- var filtered = typeRegistrations
- .Where(x => !configuredTypes.Any(y => y.RegistrationType == x.RegistrationType))
- .Where(x => !this.registeredInstances.Any(y => y.RegistrationType == x.RegistrationType));
-
- RegisterTypesInternal(container, filtered);
- }
-
- private static void RegisterTypesInternal(TinyIoCContainer container, IEnumerable<TypeRegistration> filtered)
- {
- foreach (var typeRegistration in filtered)
- {
- container.Register(typeRegistration.RegistrationType, typeRegistration.ImplementationType).AsSingleton();
- }
- }
-
- /// <summary>
- /// Register the various collections into the container as singletons to later be resolved
- /// by IEnumerable{Type} constructor dependencies.
- /// </summary>
- /// <param name="container">Container to register into</param>
- /// <param name="collectionTypeRegistrations">Collection type registrations to register</param>
- protected override void RegisterCollectionTypes(TinyIoCContainer container, IEnumerable<CollectionTypeRegistration> collectionTypeRegistrations)
- {
- var configuredCollectionTypes =
- this.GetCollectionTypeRegistrations().ToList();
-
- var filtered = collectionTypeRegistrations
- .Where(x => !configuredCollectionTypes.Any(y => y.RegistrationType == x.RegistrationType));
-
- RegisterCollectionTypesInternal(container, filtered);
- }
-
- private static void RegisterCollectionTypesInternal(TinyIoCContainer container, IEnumerable<CollectionTypeRegistration> filtered)
- {
- foreach (var collectionTypeRegistration in filtered)
- {
- container.RegisterMultiple(collectionTypeRegistration.RegistrationType,
- collectionTypeRegistration.ImplementationTypes);
- }
- }
-
- /// <summary>
- /// Register the given instances into the container
- /// </summary>
- /// <param name="container">Container to register into</param>
- /// <param name="instanceRegistrations">Instance registration types</param>
- protected override void RegisterInstances(TinyIoCContainer container, IEnumerable<InstanceRegistration> instanceRegistrations)
- {
- var configuredInstanceRegistrations = this.GetTypeRegistrations();
-
- var fileteredInstanceRegistrations = instanceRegistrations
- .Where(x => !this.registeredInstances.Any(y => y.RegistrationType == x.RegistrationType))
- .Where(x => !configuredInstanceRegistrations.Any(y => y.RegistrationType == x.RegistrationType))
- .ToList();
-
- RegisterInstancesInternal(container, fileteredInstanceRegistrations);
- }
-
- private static void RegisterInstancesInternal(TinyIoCContainer container, IEnumerable<InstanceRegistration> fileteredInstanceRegistrations)
- {
- foreach (var instanceRegistration in fileteredInstanceRegistrations)
- {
- container.Register(
- instanceRegistration.RegistrationType,
- instanceRegistration.Implementation);
- }
- }
-
- /// <summary>
- /// Register the given module types into the request container
- /// </summary>
- /// <param name="container">Container to register into</param>
- /// <param name="moduleRegistrationTypes">NancyModule types</param>
- protected override void RegisterRequestContainerModules(TinyIoCContainer container, IEnumerable<ModuleRegistration> moduleRegistrationTypes)
- {
- foreach (var moduleRegistrationType in moduleRegistrationTypes)
- {
- container.Register(
- typeof(INancyModule),
- moduleRegistrationType.ModuleType,
- moduleRegistrationType.ModuleType.FullName).
- AsSingleton();
- }
- }
-
- /// <summary>
- /// Gets the <see cref="INancyEnvironmentConfigurator"/> used by th.
- /// </summary>
- /// <returns>An <see cref="INancyEnvironmentConfigurator"/> instance.</returns>
- protected override INancyEnvironmentConfigurator GetEnvironmentConfigurator()
- {
- return this.ApplicationContainer.Resolve<INancyEnvironmentConfigurator>();
- }
-
- /// <summary>
- /// Registers an <see cref="INancyEnvironment"/> instance in the container.
- /// </summary>
- /// <param name="container">The container to register into.</param>
- /// <param name="environment">The <see cref="INancyEnvironment"/> instance to register.</param>
- protected override void RegisterNancyEnvironment(TinyIoCContainer container, INancyEnvironment environment)
- {
- container.Register(environment);
- }
-
- /// <summary>
- /// <para>
- /// The pre-request hook
- /// </para>
- /// <para>
- /// The PreRequest hook is called prior to processing a request. If a hook returns
- /// a non-null response then processing is aborted and the response provided is
- /// returned.
- /// </para>
- /// </summary>
- public BeforePipeline BeforeRequest
- {
- get { return this.ApplicationPipelines.BeforeRequest; }
- set { this.ApplicationPipelines.BeforeRequest = value; }
- }
-
- /// <summary>
- /// <para>
- /// The post-request hook
- /// </para>
- /// <para>
- /// The post-request hook is called after the response is created. It can be used
- /// to rewrite the response or add/remove items from the context.
- /// </para>
- /// </summary>
- public AfterPipeline AfterRequest
- {
- get { return this.ApplicationPipelines.AfterRequest; }
- set { this.ApplicationPipelines.AfterRequest = value; }
- }
-
- /// <summary>
- /// <para>
- /// The error hook
- /// </para>
- /// <para>
- /// The error hook is called if an exception is thrown at any time during the pipeline.
- /// If no error hook exists a standard InternalServerError response is returned
- /// </para>
- /// </summary>
- public ErrorPipeline OnError
- {
- get { return this.ApplicationPipelines.OnError; }
- set { this.ApplicationPipelines.OnError = value; }
- }
-
- /// <summary>
- /// Provides an API for configuring a <see cref="ConfigurableBootstrapper"/> instance.
- /// </summary>
- public class ConfigurableBootstrapperConfigurator
- {
- private readonly ConfigurableBootstrapper bootstrapper;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="ConfigurableBootstrapperConfigurator"/> class.
- /// </summary>
- /// <param name="bootstrapper">The bootstrapper that should be configured.</param>
- public ConfigurableBootstrapperConfigurator(ConfigurableBootstrapper bootstrapper)
- {
- this.bootstrapper = bootstrapper;
- this.Diagnostics<DisabledDiagnostics>();
- }
-
- public ConfigurableBootstrapperConfigurator AllDiscoveredModules()
- {
- this.bootstrapper.allDiscoveredModules = true;
-
- return this;
- }
-
- public ConfigurableBootstrapperConfigurator Binder(IBinder binder)
- {
- this.bootstrapper.registeredInstances.Add(
- new InstanceRegistration(typeof(IBinder), binder));
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to create an <see cref="IBinder"/> instance of the specified type.
- /// </summary>
- /// <typeparam name="T">The type of the <see cref="IBinder"/> that the bootstrapper should use.</typeparam>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator Binder<T>() where T : IBinder
- {
- this.bootstrapper.configurationOverrides.Add(x => x.Binder = typeof(T));
- return this;
- }
-
- /// <summary>
- /// Configures the <see cref="INancyEnvironment"/>.
- /// </summary>
- /// <param name="configuration">The configuration to apply to the environment.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator Configure(Action<INancyEnvironment> configuration)
- {
- this.bootstrapper.configure = configuration;
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to use the provided instance of <see cref="INancyContextFactory"/>.
- /// </summary>
- /// <param name="contextFactory">The <see cref="INancyContextFactory"/> instance that should be used by the bootstrapper.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator ContextFactory(INancyContextFactory contextFactory)
- {
- this.bootstrapper.registeredInstances.Add(
- new InstanceRegistration(typeof(INancyContextFactory), contextFactory));
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to create an <see cref="INancyContextFactory"/> instance of the specified type.
- /// </summary>
- /// <typeparam name="T">The type of the <see cref="INancyContextFactory"/> that the bootstrapper should use.</typeparam>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator ContextFactory<T>() where T : INancyContextFactory
- {
- this.bootstrapper.configurationOverrides.Add(x => x.ContextFactory = typeof(T));
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to create an <see cref="INancyDefaultConfigurationProvider"/> instance of the specified type.
- /// </summary>
- /// <typeparam name="T">The type of the <see cref="IRouteMetadataProvider"/> that the bootstrapper should use.</typeparam>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator DefaultConfigurationProvider<T>() where T : INancyDefaultConfigurationProvider
- {
- this.bootstrapper.configurationOverrides.Add(x => x.DefaultConfigurationProviders = new List<Type>(new[] { typeof(T) }));
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to use the provided <see cref="INancyDefaultConfigurationProvider"/> types.
- /// </summary>
- /// <param name="defaultConfigurationProviders">The <see cref="INancyDefaultConfigurationProvider"/> types that should be used by the bootstrapper.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator DefaultConfigurationProviders(params Type[] defaultConfigurationProviders)
- {
- this.bootstrapper.configurationOverrides.Add(x => x.DefaultConfigurationProviders = defaultConfigurationProviders);
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to use the provided instances of <see cref="INancyDefaultConfigurationProvider"/>.
- /// </summary>
- /// <param name="defaultConfigurationProvider">The <see cref="INancyDefaultConfigurationProvider"/> types that should be used by the bootstrapper.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator DefaultConfigurationProviders(INancyDefaultConfigurationProvider defaultConfigurationProvider)
- {
- this.bootstrapper.registeredInstances.Add(
- new InstanceRegistration(typeof(INancyDefaultConfigurationProvider), defaultConfigurationProvider));
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to use the provided instances of <see cref="INancyDefaultConfigurationProvider"/>.
- /// </summary>
- /// <param name="defaultConfigurationProviders">The <see cref="INancyDefaultConfigurationProvider"/> types that should be used by the bootstrapper.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator DefaultConfigurationProviders(params INancyDefaultConfigurationProvider[] defaultConfigurationProviders)
- {
- foreach (var defaultConfigurationProvider in defaultConfigurationProviders)
- {
- this.bootstrapper.registeredInstances.Add(
- new InstanceRegistration(typeof(INancyDefaultConfigurationProvider), defaultConfigurationProvider));
- }
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to use the provided type as a dependency.
- /// </summary>
- /// <param name="type">The type of the dependency that should be used registered with the bootstrapper.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator Dependency<T>(Type type)
- {
- this.bootstrapper.registeredTypes.Add(new TypeRegistration(typeof(T), type));
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to register the specified type as a dependency.
- /// </summary>
- /// <typeparam name="T">The type of the dependency that should be registered with the bootstrapper.</typeparam>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- /// <remarks>This method will register the type for all the interfaces it implements and the type itself.</remarks>
- public ConfigurableBootstrapperConfigurator Dependency<T>()
- {
- this.bootstrapper.registeredTypes.Add(new TypeRegistration(typeof(T), typeof(T)));
-
- foreach (var interfaceType in GetSafeInterfaces(typeof(T)))
- {
- this.bootstrapper.registeredTypes.Add(new TypeRegistration(interfaceType, typeof(T)));
- }
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to use the provided instance as a dependency.
- /// </summary>
- /// <param name="instance">The dependency instance that should be used registered with the bootstrapper.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- /// <remarks>This method will register the instance for all the interfaces it implements and the type itself.</remarks>
- public ConfigurableBootstrapperConfigurator Dependency<T>(T instance)
- {
- this.bootstrapper.registeredInstances.Add(new InstanceRegistration(typeof(T), instance));
-
- var interfacesToRegisterBy = GetSafeInterfaces(instance.GetType()).Where(i => !i.Equals(typeof(T)));
- foreach (var interfaceType in interfacesToRegisterBy)
- {
- this.bootstrapper.registeredInstances.Add(new InstanceRegistration(interfaceType, instance));
- }
-
- return this;
- }
-
- private static IEnumerable<Type> GetSafeInterfaces(Type type)
- {
- return type.GetInterfaces().Where(x => x != typeof(IDisposable));
- }
-
- /// <summary>
- /// Configures the bootstrapper to register the specified types and instances as a dependencies.
- /// </summary>
- /// <param name="dependencies">An array of maps between the interfaces and instances that should be registered with the bootstrapper.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator MappedDependencies<T, K>(IEnumerable<Tuple<T, K>> dependencies)
- where T : Type
- where K : class
- {
- foreach (var dependency in dependencies)
- {
- this.bootstrapper.registeredInstances.Add(
- new InstanceRegistration(dependency.Item1, dependency.Item2));
- }
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to register the specified instances as a dependencies.
- /// </summary>
- /// <param name="dependencies">The instances of the dependencies that should be registered with the bootstrapper.</param>
- /// <typeparam name="T">The type that the dependencies should be registered as.</typeparam>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator Dependencies<T>(params T[] dependencies)
- {
- foreach (var dependency in dependencies)
- {
- this.Dependency(dependency);
- }
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to use the provided types as a dependency.
- /// </summary>
- /// <param name="dependencies">The types that should be used registered as dependencies with the bootstrapper.</param>
- /// <typeparam name="T">The type that the dependencies should be registered as.</typeparam>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator Dependencies<T>(params Type[] dependencies)
- {
- foreach (var dependency in dependencies)
- {
- this.Dependency<T>(dependency);
- }
-
- return this;
- }
-
- /// <summary>
- /// Enables the auto registration behavior of the bootstrapper
- /// </summary>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator EnableAutoRegistration()
- {
- this.bootstrapper.enableAutoRegistration = true;
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to use the provided instance of <see cref="IStatusCodeHandler"/>.
- /// </summary>
- /// <param name="statusCodeHandlers">The <see cref="IStatusCodeHandler"/> types that should be used by the bootstrapper.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator StatusCodeHandlers(params Type[] statusCodeHandlers)
- {
- this.bootstrapper.configurationOverrides.Add(x => x.StatusCodeHandlers = new List<Type>(statusCodeHandlers));
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to create an <see cref="IStatusCodeHandler"/> instance of the specified type.
- /// </summary>
- /// <typeparam name="T">The type of the <see cref="IStatusCodeHandler"/> that the bootstrapper should use.</typeparam>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator StatusCodeHandler<T>() where T : IStatusCodeHandler
- {
- this.bootstrapper.configurationOverrides.Add(x => x.StatusCodeHandlers = new List<Type>(new[] { typeof(T) }));
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to create an <see cref="INancyEnvironmentConfigurator"/> instance of the specified type.
- /// </summary>
- /// <typeparam name="T">The type of the <see cref="INancyEnvironmentConfigurator"/> that the bootstrapper should use.</typeparam>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator EnvironmentConfigurator<T>() where T : INancyEnvironmentConfigurator
- {
- this.bootstrapper.configurationOverrides.Add(x => x.EnvironmentConfigurator = typeof(T));
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to use the provided instance of <see cref="INancyEnvironmentConfigurator"/>.
- /// </summary>
- /// <param name="environmentConfigurator">The <see cref="INancyEnvironmentConfigurator"/> instance that should be used by the bootstrapper.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator EnvironmentConfigurator(INancyEnvironmentConfigurator environmentConfigurator)
- {
- this.bootstrapper.registeredInstances.Add(
- new InstanceRegistration(typeof(INancyEnvironmentConfigurator), environmentConfigurator));
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to use the provided instance of <see cref="INancyEnvironmentFactory"/>.
- /// </summary>
- /// <param name="environmentFactory">The <see cref="INancyEnvironmentFactory"/> instance that should be used by the bootstrapper.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator EnvironmentFactory(INancyEnvironmentFactory environmentFactory)
- {
- this.bootstrapper.registeredInstances.Add(
- new InstanceRegistration(typeof(INancyEnvironmentConfigurator), environmentFactory));
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to create an <see cref="INancyEnvironmentFactory"/> instance of the specified type.
- /// </summary>
- /// <typeparam name="T">The type of the <see cref="INancyEnvironmentFactory"/> that the bootstrapper should use.</typeparam>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator EnvironmentFactory<T>() where T : INancyEnvironmentFactory
- {
- this.bootstrapper.configurationOverrides.Add(x => x.EnvironmentFactory = typeof(T));
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to use the provided instance of <see cref="IFieldNameConverter"/>.
- /// </summary>
- /// <param name="fieldNameConverter">The <see cref="IFieldNameConverter"/> instance that should be used by the bootstrapper.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator FieldNameConverter(IFieldNameConverter fieldNameConverter)
- {
- this.bootstrapper.registeredInstances.Add(
- new InstanceRegistration(typeof(IFieldNameConverter), fieldNameConverter));
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to create an <see cref="IFieldNameConverter"/> instance of the specified type.
- /// </summary>
- /// <typeparam name="T">The type of the <see cref="IFieldNameConverter"/> that the bootstrapper should use.</typeparam>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator FieldNameConverter<T>() where T : IFieldNameConverter
- {
- this.bootstrapper.configurationOverrides.Add(x => x.FieldNameConverter = typeof(T));
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to use the provided instance of <see cref="IModelBinderLocator"/>.
- /// </summary>
- /// <param name="modelBinderLocator">The <see cref="IModelBinderLocator"/> instance that should be used by the bootstrapper.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator ModelBinderLocator(IModelBinderLocator modelBinderLocator)
- {
- this.bootstrapper.registeredInstances.Add(
- new InstanceRegistration(typeof(IModelBinderLocator), modelBinderLocator));
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to create an <see cref="IModelBinderLocator"/> instance of the specified type.
- /// </summary>
- /// <typeparam name="T">The type of the <see cref="IModelBinderLocator"/> that the bootstrapper should use.</typeparam>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator ModelBinderLocator<T>() where T : IModelBinderLocator
- {
- this.bootstrapper.configurationOverrides.Add(x => x.ModelBinderLocator = typeof(T));
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to create a <see cref="INancyModule"/> instance of the specified type.
- /// </summary>
- /// <typeparam name="T">The type of the <see cref="INancyModule"/> that the bootstrapper should use.</typeparam>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator Module<T>() where T : INancyModule
- {
- return this.Modules(typeof(T));
- }
-
- /// <summary>
- /// Configures the bootstrapper to register the provided <see cref="INancyModule"/> instance.
- /// </summary>
- /// <param name="module">The <see cref="INancyModule"/> instance to register.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator Module(INancyModule module)
- {
- this.bootstrapper.catalog.RegisterModuleInstance(module);
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to create <see cref="INancyModule"/> instances of the specified types.
- /// </summary>
- /// <param name="modules">The types of the <see cref="INancyModule"/> that the bootstrapper should use.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator Modules(params Type[] modules)
- {
- var moduleRegistrations =
- from module in modules
- select new ModuleRegistration(module);
-
- this.bootstrapper.registeredTypes.AddRange(moduleRegistrations);
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to use the provided instance of <see cref="INancyEngine"/>.
- /// </summary>
- /// <param name="engine">The <see cref="INancyEngine"/> instance that should be used by the bootstrapper.</param>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator NancyEngine(INancyEngine engine)
- {
- this.bootstrapper.registeredInstances.Add(
- new InstanceRegistration(typeof(INancyEngine), engine));
-
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to create an <see cref="INancyEngine"/> instance of the specified type.
- /// </summary>
- /// <typeparam name="T">The type of the <see cref="INancyEngine"/> that the bootstrapper should use.</typeparam>
- /// <returns>A reference to the current <see cref="ConfigurableBootstrapperConfigurator"/>.</returns>
- public ConfigurableBootstrapperConfigurator NancyEngine<T>() where T : INancyEngine
- {
- this.bootstrapper.configurationOverrides.Add(x => x.NancyEngine = typeof(T));
- return this;
- }
-
- /// <summary>
- /// Configures the bootstrapper to use the provided instance of <see cref="INancyModuleBuilder…
Large files files are truncated, but you can click here to view the full file