Lidarr/src/NzbDrone.Api/TinyIoCNancyBootstrapper.cs

242 lines
11 KiB
C#
Raw Normal View History

2013-04-03 02:20:05 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Nancy;
using Nancy.Bootstrapper;
using Nancy.Diagnostics;
using TinyIoC;
namespace NzbDrone.Api
{
2013-08-20 01:45:19 +00:00
public class TinyIoCNancyBootstrapper : NancyBootstrapperWithRequestContainerBase<TinyIoCContainer>
2013-04-03 02:20:05 +00:00
{
2013-08-20 01:45:19 +00:00
// <summary>
/// Default assemblies that are ignored for autoregister
/// </summary>
private static readonly IEnumerable<Func<Assembly, bool>> DefaultAutoRegisterIgnoredAssemblies = new Func<Assembly, bool>[]
{
asm => !asm.FullName.StartsWith("Nancy.", StringComparison.InvariantCulture),
};
2013-04-03 02:20:05 +00:00
/// <summary>
2013-08-20 01:45:19 +00:00
/// Gets the assemblies to ignore when autoregistering the application container
/// Return true from the delegate to ignore that particular assembly, returning true
/// does not mean the assembly *will* be included, a false from another delegate will
/// take precedence.
2013-04-03 02:20:05 +00:00
/// </summary>
2013-08-20 01:45:19 +00:00
protected virtual IEnumerable<Func<Assembly, bool>> AutoRegisterIgnoredAssemblies
2013-04-03 02:20:05 +00:00
{
2013-08-20 01:45:19 +00:00
get { return DefaultAutoRegisterIgnoredAssemblies; }
2013-04-03 02:20:05 +00:00
}
/// <summary>
2013-08-20 01:45:19 +00:00
/// Configures the container using AutoRegister followed by registration
/// of default INancyModuleCatalog and IRouteResolver.
/// </summary>
2013-08-20 01:45:19 +00:00
/// <param name="container">Container instance</param>
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
2013-08-20 01:45:19 +00:00
AutoRegister(container, this.AutoRegisterIgnoredAssemblies);
}
2013-08-20 01:45:19 +00:00
/// <summary>
/// Resolve INancyEngine
/// </summary>
/// <returns>INancyEngine implementation</returns>
protected override sealed INancyEngine GetEngineInternal()
{
return this.ApplicationContainer.Resolve<INancyEngine>();
}
/* protected override IModuleKeyGenerator GetModuleKeyGenerator()
{
return ApplicationContainer.Resolve<IModuleKeyGenerator>();
}*/
2013-04-03 02:20:05 +00:00
/// <summary>
/// Create a default, unconfigured, container
/// </summary>
/// <returns>Container instance</returns>
protected override TinyIoCContainer GetApplicationContainer()
{
return new TinyIoCContainer();
}
/// <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 sealed void RegisterBootstrapperTypes(TinyIoCContainer applicationContainer)
{
applicationContainer.Register<INancyModuleCatalog>(this);
}
/// <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 sealed void RegisterTypes(TinyIoCContainer container, IEnumerable<TypeRegistration> typeRegistrations)
{
foreach (var typeRegistration in typeRegistrations)
{
2014-08-24 17:27:36 +00:00
switch (typeRegistration.Lifetime)
{
case Lifetime.Transient:
container.Register(typeRegistration.RegistrationType, typeRegistration.ImplementationType).AsMultiInstance();
break;
case Lifetime.Singleton:
container.Register(typeRegistration.RegistrationType, typeRegistration.ImplementationType).AsSingleton();
break;
case Lifetime.PerRequest:
throw new InvalidOperationException("Unable to directly register a per request lifetime.");
default:
throw new ArgumentOutOfRangeException();
}
2013-04-03 02:20:05 +00:00
}
}
/// <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="collectionTypeRegistrationsn">Collection type registrations to register</param>
protected override sealed void RegisterCollectionTypes(TinyIoCContainer container, IEnumerable<CollectionTypeRegistration> collectionTypeRegistrationsn)
{
foreach (var collectionTypeRegistration in collectionTypeRegistrationsn)
{
2014-08-24 17:27:36 +00:00
switch (collectionTypeRegistration.Lifetime)
{
case Lifetime.Transient:
container.RegisterMultiple(collectionTypeRegistration.RegistrationType, collectionTypeRegistration.ImplementationTypes).AsMultiInstance();
break;
case Lifetime.Singleton:
container.RegisterMultiple(collectionTypeRegistration.RegistrationType, collectionTypeRegistration.ImplementationTypes).AsSingleton();
break;
case Lifetime.PerRequest:
throw new InvalidOperationException("Unable to directly register a per request lifetime.");
default:
throw new ArgumentOutOfRangeException();
}
2013-04-03 02:20:05 +00:00
}
}
/// <summary>
/// Register the given module types into the container
/// </summary>
/// <param name="container">Container to register into</param>
/// <param name="moduleRegistrationTypes">NancyModule types</param>
protected override sealed void RegisterRequestContainerModules(TinyIoCContainer container, IEnumerable<ModuleRegistration> moduleRegistrationTypes)
{
foreach (var moduleRegistrationType in moduleRegistrationTypes)
{
container.Register(
typeof(INancyModule),
moduleRegistrationType.ModuleType,
2013-08-20 01:45:19 +00:00
moduleRegistrationType.ModuleType.FullName).
AsSingleton();
2013-04-03 02:20:05 +00:00
}
}
/// <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)
{
foreach (var instanceRegistration in instanceRegistrations)
{
container.Register(
instanceRegistration.RegistrationType,
instanceRegistration.Implementation);
}
}
/// <summary>
/// Creates a per request child/nested container
/// </summary>
/// <returns>Request container instance</returns>
protected override sealed TinyIoCContainer CreateRequestContainer()
{
2013-08-20 01:45:19 +00:00
return this.ApplicationContainer.GetChildContainer();
2013-04-03 02:20:05 +00:00
}
/// <summary>
2013-08-20 01:45:19 +00:00
/// Gets the diagnostics for initialisation
2013-04-03 02:20:05 +00:00
/// </summary>
2013-08-20 01:45:19 +00:00
/// <returns>IDiagnostics implementation</returns>
2013-04-03 02:20:05 +00:00
protected override IDiagnostics GetDiagnostics()
{
2013-08-20 01:45:19 +00:00
return this.ApplicationContainer.Resolve<IDiagnostics>();
2013-04-03 02:20:05 +00:00
}
/// <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()
{
2013-08-20 01:45:19 +00:00
return this.ApplicationContainer.ResolveAll<IApplicationStartup>(false);
2013-04-03 02:20:05 +00:00
}
2014-08-24 17:27:36 +00:00
/// <summary>
/// Gets all registered request startup tasks
/// </summary>
/// <returns>An <see cref="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);
}
2013-04-03 02:20:05 +00:00
/// <summary>
/// Gets all registered application registration tasks
/// </summary>
2014-08-24 17:27:36 +00:00
/// <returns>An <see cref="IEnumerable{T}"/> instance containing <see cref="IRegistrations"/> instances.</returns>
protected override IEnumerable<IRegistrations> GetRegistrationTasks()
2013-04-03 02:20:05 +00:00
{
2014-08-24 17:27:36 +00:00
return this.ApplicationContainer.ResolveAll<IRegistrations>(false);
2013-04-03 02:20:05 +00:00
}
/// <summary>
/// Retrieve all module instances from the container
/// </summary>
/// <param name="container">Container to use</param>
/// <returns>Collection of NancyModule instances</returns>
protected override sealed IEnumerable<INancyModule> GetAllModules(TinyIoCContainer container)
{
var nancyModules = container.ResolveAll<INancyModule>(false);
return nancyModules;
}
/// <summary>
2013-08-20 01:45:19 +00:00
/// Retreive a specific module instance from the container
2013-04-03 02:20:05 +00:00
/// </summary>
/// <param name="container">Container to use</param>
2013-08-20 01:45:19 +00:00
/// <param name="moduleType">Type of the module</param>
2013-04-03 02:20:05 +00:00
/// <returns>NancyModule instance</returns>
2013-08-20 01:45:19 +00:00
protected override sealed INancyModule GetModule(TinyIoCContainer container, Type moduleType)
2013-04-03 02:20:05 +00:00
{
2013-08-20 01:45:19 +00:00
container.Register(typeof(INancyModule), moduleType);
return container.Resolve<INancyModule>();
2013-04-03 02:20:05 +00:00
}
/// <summary>
/// Executes auto registation with the given container.
/// </summary>
/// <param name="container">Container instance</param>
private static void AutoRegister(TinyIoCContainer container, IEnumerable<Func<Assembly, bool>> ignoredAssemblies)
{
var assembly = typeof(NancyEngine).Assembly;
2014-08-24 17:27:36 +00:00
container.AutoRegister(AppDomain.CurrentDomain.GetAssemblies().Where(a => !ignoredAssemblies.Any(ia => ia(a))), DuplicateImplementationActions.RegisterMultiple, t => t.Assembly != assembly);
2013-04-03 02:20:05 +00:00
}
}
2014-08-24 17:27:36 +00:00
}