Skip to content

Instantly share code, notes, and snippets.

@thefringeninja
Created October 17, 2017 13:14
Show Gist options
  • Save thefringeninja/a41fa6847c26fe9c10d078fbd55c0e22 to your computer and use it in GitHub Desktop.
Save thefringeninja/a41fa6847c26fe9c10d078fbd55c0e22 to your computer and use it in GitHub Desktop.
Look ma no IoC!
internal class CustomerBootstrapper
: DefaultBootstrapper
{
private readonly ICustomerRepository _customers;
public CustomerBootstrapper(ICustomerRepository customers)
{
if (customers == null)
{
throw new ArgumentNullException(nameof(customers));
}
_customers = customers;
}
protected override void ApplyInternalConfiguration(NancyInternalConfiguration config)
{
base.ApplyInternalConfiguration(config);
config.BindingDefaults = typeof(BindingDefaults);
config.ResponseProcessors = new List<Type>
{
typeof(JsonProcessor),
typeof(AllProcessor)
};
}
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
base.ConfigureApplicationContainer(container);
container.Register<INancyModuleCatalog>(new ModuleCatalog(_customers));
container.Register<JsonSerializer>(new CustomJsonSerializer());
}
private class ModuleCatalog : INancyModuleCatalog
{
private readonly ICustomerRepository _customers;
private readonly IDictionary<Type, Func<INancyModule>> _modules;
public ModuleCatalog(ICustomerRepository customers)
{
_customers = customers;
_modules = new Dictionary<Type, Func<INancyModule>>
{
[typeof(CustomersModule)] = () => new CustomersModule(_customers)
};
}
public IEnumerable<INancyModule> GetAllModules(NancyContext context)
=> from factory in _modules.Values
select factory();
public INancyModule GetModule(Type moduleType, NancyContext context)
=> _modules[moduleType]();
}
private class CustomJsonSerializer : JsonSerializer
{
public CustomJsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver();
}
}
private class AllProcessor : IResponseProcessor
{
private readonly JsonProcessor _inner;
public AllProcessor(JsonProcessor inner)
{
_inner = inner;
}
public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context) =>
new ProcessorMatch
{
ModelResult = MatchResult.DontCare,
RequestedContentTypeResult = MatchResult.DontCare
};
public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context)
=> _inner.Process(requestedMediaRange, model, context);
public IEnumerable<Tuple<string, MediaRange>> ExtensionMappings => _inner.ExtensionMappings;
}
private class BindingDefaults : Nancy.ModelBinding.BindingDefaults
{
public BindingDefaults(INancyEnvironment environment)
: base(environment)
{ }
}
}
internal class DefaultBootstrapper : DefaultNancyBootstrapper
{
public override void Configure(INancyEnvironment environment)
{
environment.Tracing(true, true);
base.Configure(environment);
}
protected override void ConfigureConventions(NancyConventions nancyConventions)
{
new IConvention[]
{
new DefaultAcceptHeaderCoercionConventions(),
new DefaultCultureConventions(),
new DefaultStaticContentsConventions(),
new DefaultViewLocationConventions(),
}.ForEach(x => x.Initialise(nancyConventions));
}
protected override Func<ITypeCatalog, NancyInternalConfiguration> InternalConfiguration
=> NancyInternalConfiguration.WithOverrides(ApplyInternalConfiguration);
protected virtual void ApplyInternalConfiguration(NancyInternalConfiguration config)
{
config.DefaultConfigurationProviders
= new List<Type>
{
typeof(DefaultDiagnosticsConfigurationProvider),
typeof(DefaultGlobalizationConfigurationProvider),
typeof(DefaultJsonConfigurationProvider),
typeof(DefaultRouteConfigurationProvider),
typeof(DefaultStaticContentConfigurationProvider),
typeof(DefaultTraceConfigurationProvider),
typeof(DefaultViewConfigurationProvider),
typeof(DefaultXmlConfigurationProvider)
};
config.CultureService = typeof(CultureService);
config.StaticContentProvider = typeof(DisabledStaticContentProvider);
}
/// <summary>
/// Overriding this method to ensure that the base method is not called as we do not
/// want assembly scanning to occur.
/// </summary>
/// <param name="container"></param>
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
}
private class CultureService : ICultureService
{
public CultureInfo DetermineCurrentCulture(NancyContext context)
{
return CultureInfo.InstalledUICulture;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment