Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Removed ADATS dependency in NancyInternalConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodejunkie committed Feb 4, 2016
1 parent fd118cd commit 6fbce8e
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 155 deletions.
3 changes: 2 additions & 1 deletion samples/Nancy.Demo.Hosting.Aspnet/DemoBootstrapper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Nancy.Demo.Hosting.Aspnet
{
using System;
using System.Collections.Generic;
using System.Reflection;

Expand Down Expand Up @@ -42,7 +43,7 @@ public override void Configure(INancyEnvironment environment)
environment.MyConfig("Hello World");
}

protected override NancyInternalConfiguration InternalConfiguration
protected override Func<ITypeCatalog, NancyInternalConfiguration> InternalConfiguration
{
get
{
Expand Down
5 changes: 3 additions & 2 deletions samples/Nancy.Demo.Razor.Localization/DemoBootstrapper.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
namespace Nancy.Demo.Razor.Localization
{
using System;
using Nancy.Bootstrapper;

public class DemoBootstrapper : DefaultNancyBootstrapper
{
protected override NancyInternalConfiguration InternalConfiguration
protected override Func<ITypeCatalog, NancyInternalConfiguration> InternalConfiguration
{
get
{
return NancyInternalConfiguration.WithOverrides(x => x.ResourceAssemblyProvider = typeof(CustomResourceAssemblyProvider));
}
}
}
}
}
104 changes: 55 additions & 49 deletions src/Nancy.Testing/ConfigurableBootstrapper.cs

Large diffs are not rendered by default.

57 changes: 25 additions & 32 deletions src/Nancy/Bootstrapper/NancyBootstrapperBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public abstract class NancyBootstrapperBase<TContainer> : INancyBootstrapper, IN
/// <summary>
/// Internal configuration
/// </summary>
private Func<ITypeCatalog, NancyInternalConfiguration> internalConfigurationFactory;
private NancyInternalConfiguration internalConfiguration;

/// <summary>
Expand Down Expand Up @@ -102,12 +103,9 @@ protected virtual ITypeCatalog TypeCatalog
/// <summary>
/// Nancy internal configuration
/// </summary>
protected virtual NancyInternalConfiguration InternalConfiguration
protected virtual Func<ITypeCatalog, NancyInternalConfiguration> InternalConfiguration
{
get
{
return this.internalConfiguration ?? (this.internalConfiguration = NancyInternalConfiguration.Default);
}
get { return this.internalConfigurationFactory ?? (this.internalConfigurationFactory = NancyInternalConfiguration.Default); }
}

/// <summary>
Expand All @@ -128,15 +126,11 @@ protected virtual IEnumerable<ModuleRegistration> Modules
{
get
{
// Shouldn't need thread safety here?
return
this.modules
??
(this.modules = this.TypeCatalog
.GetTypesAssignableTo<INancyModule>(TypeResolveStrategies.ExcludeNancy)
.NotOfType<DiagnosticModule>()
.Select(t => new ModuleRegistration(t))
.ToArray());
return this.modules ?? (this.modules = this.TypeCatalog
.GetTypesAssignableTo<INancyModule>(TypeResolveStrategies.ExcludeNancy)
.NotOfType<DiagnosticModule>()
.Select(t => new ModuleRegistration(t))
.ToArray());
}
}

Expand All @@ -145,32 +139,23 @@ protected virtual IEnumerable<ModuleRegistration> Modules
/// </summary>
protected virtual IEnumerable<Type> ViewEngines
{
get
{
return this.TypeCatalog.GetTypesAssignableTo<IViewEngine>();
}
get { return this.TypeCatalog.GetTypesAssignableTo<IViewEngine>(); }
}

/// <summary>
/// Gets the available custom model binders
/// </summary>
protected virtual IEnumerable<Type> ModelBinders
{
get
{
return this.TypeCatalog.GetTypesAssignableTo<IModelBinder>();
}
get { return this.TypeCatalog.GetTypesAssignableTo<IModelBinder>(); }
}

/// <summary>
/// Gets the available custom type converters
/// </summary>
protected virtual IEnumerable<Type> TypeConverters
{
get
{
return this.TypeCatalog.GetTypesAssignableTo<ITypeConverter>(TypeResolveStrategies.ExcludeNancy);
}
get { return this.TypeCatalog.GetTypesAssignableTo<ITypeConverter>(TypeResolveStrategies.ExcludeNancy); }
}

/// <summary>
Expand All @@ -186,7 +171,7 @@ protected virtual IEnumerable<Type> BodyDeserializers
/// </summary>
protected virtual IEnumerable<Type> ApplicationStartupTasks
{
get { return this.typeCatalog.GetTypesAssignableTo<IApplicationStartup>(); }
get { return this.TypeCatalog.GetTypesAssignableTo<IApplicationStartup>(); }
}

/// <summary>
Expand Down Expand Up @@ -237,17 +222,25 @@ protected virtual CryptographyConfiguration CryptographyConfiguration
get { return CryptographyConfiguration.Default; }
}

private NancyInternalConfiguration GetInitializedInternalConfiguration()
{
return this.internalConfiguration ?? (this.internalConfiguration = this.InternalConfiguration.Invoke(this.TypeCatalog));
}

/// <summary>
/// Initialise the bootstrapper. Must be called prior to GetEngine.
/// </summary>
public void Initialise()
{
if (this.InternalConfiguration == null)
var configuration =
this.GetInitializedInternalConfiguration();

if (configuration == null)
{
throw new InvalidOperationException("Configuration cannot be null");
}

if (!this.InternalConfiguration.IsValid)
if (!configuration.IsValid)
{
throw new InvalidOperationException("Configuration is invalid");
}
Expand All @@ -258,11 +251,11 @@ public void Initialise()

this.ConfigureApplicationContainer(this.ApplicationContainer);

var typeRegistrations = this.InternalConfiguration
var typeRegistrations = configuration
.GetTypeRegistrations()
.Concat(this.GetAdditionalTypes());

var collectionTypeRegistrations = this.InternalConfiguration
var collectionTypeRegistrations = configuration
.GetCollectionTypeRegistrations()
.Concat(this.GetApplicationCollections());

Expand Down Expand Up @@ -621,7 +614,7 @@ private IEnumerable<InstanceRegistration> GetAdditionalInstances()
{
return new[] {
new InstanceRegistration(typeof(CryptographyConfiguration), this.CryptographyConfiguration),
new InstanceRegistration(typeof(NancyInternalConfiguration), this.InternalConfiguration),
new InstanceRegistration(typeof(NancyInternalConfiguration), this.GetInitializedInternalConfiguration()),
new InstanceRegistration(typeof(IRootPathProvider), this.RootPathProvider),
new InstanceRegistration(typeof(IAssemblyCatalog), this.AssemblyCatalog),
new InstanceRegistration(typeof(ITypeCatalog), this.TypeCatalog),
Expand Down
94 changes: 46 additions & 48 deletions src/Nancy/Bootstrapper/NancyInternalConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace Nancy.Bootstrapper
using System;
using System.Collections.Generic;
using System.Linq;

using Nancy.Configuration;
using Nancy.Culture;
using Nancy.Diagnostics;
Expand All @@ -29,59 +28,59 @@ public sealed class NancyInternalConfiguration
/// <summary>
/// Gets the Nancy default configuration
/// </summary>
public static NancyInternalConfiguration Default
public static Func<ITypeCatalog, NancyInternalConfiguration> Default
{
get
{
return new NancyInternalConfiguration
return typeCatalog => new NancyInternalConfiguration
{
RouteResolver = typeof(DefaultRouteResolver),
RoutePatternMatcher = typeof(DefaultRoutePatternMatcher),
ContextFactory = typeof(DefaultNancyContextFactory),
NancyEngine = typeof(NancyEngine),
RouteCache = typeof(RouteCache),
RouteCacheProvider = typeof(DefaultRouteCacheProvider),
ViewLocator = typeof(DefaultViewLocator),
ViewFactory = typeof(DefaultViewFactory),
NancyModuleBuilder = typeof(DefaultNancyModuleBuilder),
ResponseFormatterFactory = typeof(DefaultResponseFormatterFactory),
ModelBinderLocator = typeof(DefaultModelBinderLocator),
Binder = typeof(DefaultBinder),
BindingDefaults = typeof(BindingDefaults),
ContextFactory = typeof(DefaultNancyContextFactory),
CsrfTokenValidator = typeof(DefaultCsrfTokenValidator),
CultureService = typeof(DefaultCultureService),
DefaultConfigurationProviders = typeCatalog.GetTypesAssignableTo<INancyDefaultConfigurationProvider>().ToList(),
Diagnostics = typeof(DefaultDiagnostics),
EnvironmentFactory = typeof(DefaultNancyEnvironmentFactory),
EnvironmentConfigurator = typeof(DefaultNancyEnvironmentConfigurator),
FieldNameConverter = typeof(DefaultFieldNameConverter),
ViewResolver = typeof(DefaultViewResolver),
ViewCache = typeof(DefaultViewCache),
RenderContextFactory = typeof(DefaultRenderContextFactory),
InteractiveDiagnosticProviders = new List<Type>(typeCatalog.GetTypesAssignableTo<IDiagnosticsProvider>()),
ModelBinderLocator = typeof(DefaultModelBinderLocator),
ModelValidatorLocator = typeof(DefaultValidatorLocator),
ViewLocationProvider = typeof(FileSystemViewLocationProvider),
StatusCodeHandlers = new List<Type>(AppDomainAssemblyTypeScanner.TypesOf<IStatusCodeHandler>(ScanMode.ExcludeNancy).Concat(new[] { typeof(DefaultStatusCodeHandler) })),
CsrfTokenValidator = typeof(DefaultCsrfTokenValidator),
NancyEngine = typeof(NancyEngine),
NancyModuleBuilder = typeof(DefaultNancyModuleBuilder),
ObjectSerializer = typeof(DefaultObjectSerializer),
Serializers = AppDomainAssemblyTypeScanner.TypesOf<ISerializer>(ScanMode.ExcludeNancy).Union(new List<Type>(new[] { typeof(DefaultJsonSerializer), typeof(DefaultXmlSerializer) })).ToList(),
InteractiveDiagnosticProviders = new List<Type>(AppDomainAssemblyTypeScanner.TypesOf<IDiagnosticsProvider>()),
RenderContextFactory = typeof(DefaultRenderContextFactory),
RequestDispatcher = typeof(DefaultRequestDispatcher),
RequestTraceFactory = typeof(DefaultRequestTraceFactory),
RequestTracing = typeof(DefaultRequestTracing),
ResourceAssemblyProvider = typeof(ResourceAssemblyProvider),
ResourceReader = typeof(DefaultResourceReader),
ResponseFormatterFactory = typeof(DefaultResponseFormatterFactory),
ResponseNegotiator = typeof(DefaultResponseNegotiator),
ResponseProcessors = typeCatalog.GetTypesAssignableTo<IResponseProcessor>().ToList(),
RouteCache = typeof(RouteCache),
RouteCacheProvider = typeof(DefaultRouteCacheProvider),
RouteInvoker = typeof(DefaultRouteInvoker),
ResponseProcessors = AppDomainAssemblyTypeScanner.TypesOf<IResponseProcessor>().ToList(),
RequestDispatcher = typeof(DefaultRequestDispatcher),
Diagnostics = typeof(DefaultDiagnostics),
RoutePatternMatcher = typeof(DefaultRoutePatternMatcher),
RouteResolver = typeof(DefaultRouteResolver),
RouteResolverTrie = typeof(RouteResolverTrie),
RouteSegmentConstraints = typeCatalog.GetTypesAssignableTo<IRouteSegmentConstraint>().ToList(),
RouteSegmentExtractor = typeof(DefaultRouteSegmentExtractor),
RouteMetadataProviders = typeCatalog.GetTypesAssignableTo<IRouteMetadataProvider>().ToList(),
RouteDescriptionProvider = typeof(DefaultRouteDescriptionProvider),
CultureService = typeof(DefaultCultureService),
TextResource = typeof(ResourceBasedTextResource),
ResourceAssemblyProvider = typeof(ResourceAssemblyProvider),
ResourceReader = typeof(DefaultResourceReader),
RuntimeEnvironmentInformation = typeof(DefaultRuntimeEnvironmentInformation),
SerializerFactory = typeof(DefaultSerializerFactory),
Serializers = typeCatalog.GetTypesAssignableTo<ISerializer>(TypeResolveStrategies.ExcludeNancy).Union(new List<Type>(new[] { typeof(DefaultJsonSerializer), typeof(DefaultXmlSerializer) })).ToList(),
StaticContentProvider = typeof(DefaultStaticContentProvider),
RouteResolverTrie = typeof(RouteResolverTrie),
StatusCodeHandlers = new List<Type>(typeCatalog.GetTypesAssignableTo<IStatusCodeHandler>(TypeResolveStrategies.ExcludeNancy).Concat(new[] { typeof(DefaultStatusCodeHandler) })),
TextResource = typeof(ResourceBasedTextResource),
TrieNodeFactory = typeof(TrieNodeFactory),
RouteSegmentConstraints = AppDomainAssemblyTypeScanner.TypesOf<IRouteSegmentConstraint>().ToList(),
RequestTraceFactory = typeof(DefaultRequestTraceFactory),
ResponseNegotiator = typeof(DefaultResponseNegotiator),
RouteMetadataProviders = AppDomainAssemblyTypeScanner.TypesOf<IRouteMetadataProvider>().ToList(),
EnvironmentFactory = typeof(DefaultNancyEnvironmentFactory),
EnvironmentConfigurator = typeof(DefaultNancyEnvironmentConfigurator),
DefaultConfigurationProviders = AppDomainAssemblyTypeScanner.TypesOf<INancyDefaultConfigurationProvider>().ToList(),
SerializerFactory = typeof(DefaultSerializerFactory),
RuntimeEnvironmentInformation = typeof(DefaultRuntimeEnvironmentInformation),
ViewLocator = typeof(DefaultViewLocator),
ViewFactory = typeof(DefaultViewFactory),
ViewResolver = typeof(DefaultViewResolver),
ViewCache = typeof(DefaultViewCache),
ViewLocationProvider = typeof(FileSystemViewLocationProvider),
};
}
}
Expand Down Expand Up @@ -198,18 +197,17 @@ public bool IsValid
}
}

/// <summary>
/// Default Nancy configuration with specific overloads
/// </summary>
/// <param name="configurationBuilder">Configuration builder for overriding the default configuration properties.</param>
/// <returns>Nancy configuration instance</returns>
public static NancyInternalConfiguration WithOverrides(Action<NancyInternalConfiguration> configurationBuilder)
public static Func<ITypeCatalog, NancyInternalConfiguration> WithOverrides(Action<NancyInternalConfiguration> builder)
{
var configuration = Default;
return catalog =>
{
var configuration =
Default.Invoke(catalog);
configurationBuilder.Invoke(configuration);
builder.Invoke(configuration);
return configuration;
return configuration;
};
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions test/Nancy.Tests/Fakes/FakeDefaultNancyBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class FakeDefaultNancyBootstrapper : DefaultNancyBootstrapper
{
public IEnumerable<Type> OverriddenRegistrationTasks { get; set; }

private NancyInternalConfiguration configuration;
private Func<ITypeCatalog, NancyInternalConfiguration> configuration;

protected override IEnumerable<ModuleRegistration> Modules
{
Expand All @@ -25,7 +25,7 @@ protected override IEnumerable<ModuleRegistration> Modules
public FakeDefaultNancyBootstrapper()
: this(NancyInternalConfiguration.WithOverrides(b => b.StatusCodeHandlers = new List<Type>(new[] { typeof(DefaultStatusCodeHandler) })))
{

}

protected override IEnumerable<Type> RegistrationTasks
Expand All @@ -43,13 +43,13 @@ protected override IEnumerable<Func<Assembly, bool>> AutoRegisterIgnoredAssembli
return base.AutoRegisterIgnoredAssemblies.Union(new Func<Assembly, bool>[] { asm => asm.FullName.StartsWith("TestAssembly") });
}
}
public FakeDefaultNancyBootstrapper(NancyInternalConfiguration configuration)
public FakeDefaultNancyBootstrapper(Func<ITypeCatalog, NancyInternalConfiguration> configuration)
{
this.configuration = configuration;
this.RequestContainerInitialisations = new Dictionary<NancyContext, int>();
}

protected override NancyInternalConfiguration InternalConfiguration
protected override Func<ITypeCatalog, NancyInternalConfiguration> InternalConfiguration
{
get { return configuration; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace Nancy.Tests.Unit.Bootstrapper.Base
public abstract class BootstrapperBaseFixtureBase<TContainer>
where TContainer : class
{
private readonly NancyInternalConfiguration configuration;
private readonly Func<ITypeCatalog, NancyInternalConfiguration> configuration;

protected abstract NancyBootstrapperBase<TContainer> Bootstrapper { get; }

protected NancyInternalConfiguration Configuration
protected Func<ITypeCatalog, NancyInternalConfiguration> Configuration
{
get { return this.configuration; }
}
Expand Down Expand Up @@ -84,7 +84,7 @@ public virtual void Should_register_config_types_as_singletons()
// When
var result1 = this.Bootstrapper.GetEngine();
var result2 = this.Bootstrapper.GetEngine();

// Then
result1.ShouldBeSameAs(result2);
}
Expand All @@ -94,7 +94,7 @@ public void Should_honour_typeregistration_singleton_lifetimes()
{
// Given
this.Bootstrapper.Initialise();

// When
var result1 = ((TestDependencyModule)this.Bootstrapper.GetModule(typeof(TestDependencyModule), new NancyContext()));
var result2 = ((TestDependencyModule)this.Bootstrapper.GetModule(typeof(TestDependencyModule), new NancyContext()));
Expand Down
Loading

0 comments on commit 6fbce8e

Please sign in to comment.