private static Type GetBootstrapperType() { var customBootstrappers = AppDomainAssemblyTypeScanner .TypesOf <INancyBootstrapper>(ScanMode.ExcludeNancy) .ToList(); if (!customBootstrappers.Any()) { return(typeof(DefaultNancyBootstrapper)); } if (customBootstrappers.Count == 1) { return(customBootstrappers.Single()); } Type bootstrapper; if (TryFindMostDerivedType(customBootstrappers, out bootstrapper)) { return(bootstrapper); } var errorMessage = GetMultipleBootstrappersMessage(customBootstrappers); throw new BootstrapperException(errorMessage); }
/// <summary> /// Scans for the implementation of <typeparamref name="TRegistration"/> and registers it. /// </summary> /// <param name="lifetime">Lifetime of the registration, defaults to singleton</param> /// <typeparam name="TRegistration">The <see cref="Type"/> to scan for and register as.</typeparam> public void Register <TRegistration>(Lifetime lifetime = Lifetime.Singleton) { var implementation = AppDomainAssemblyTypeScanner .TypesOf <TRegistration>() .Single(); this.typeRegistrations.Add(new TypeRegistration(typeof(TRegistration), implementation, lifetime)); }
/// <summary> /// Scans for a <see cref="Type"/> that implements <typeparamref name="TRegistration"/>. If found, then it /// will be used for the registration, else it will use <paramref name="defaultImplementation"/>. /// </summary> /// <param name="lifetime">Lifetime of the registration, defaults to singleton</param> /// <typeparam name="TRegistration">The <see cref="Type"/> to register as.</typeparam> /// <param name="defaultImplementation">The implementation of <typeparamref name="TRegistration"/> that will be use if no other implementation can be found.</param> /// <remarks> /// When scanning, it will exclude the assembly that the <see cref="Registrations"/> instance is defined in and it will also ignore /// the type specified by <paramref name="defaultImplementation"/>. /// </remarks> public void RegisterWithDefault <TRegistration>(Type defaultImplementation, Lifetime lifetime = Lifetime.Singleton) { var implementation = AppDomainAssemblyTypeScanner .TypesOf <TRegistration>() .Where(type => type.Assembly != this.GetType().Assembly) .SingleOrDefault(type => type != defaultImplementation); this.typeRegistrations.Add(new TypeRegistration(typeof(TRegistration), implementation ?? defaultImplementation, lifetime)); }
/// <summary> /// Scans for all implementations of <typeparamref name="TRegistration"/> and registers them. /// </summary> /// <param name="lifetime">Lifetime of the registration, defaults to singleton</param> /// <typeparam name="TRegistration">The <see cref="Type"/> to scan for and register as.</typeparam> public void RegisterAll <TRegistration>(Lifetime lifetime = Lifetime.Singleton) { var implementations = AppDomainAssemblyTypeScanner .TypesOf <TRegistration>(); var registration = new CollectionTypeRegistration(typeof(TRegistration), implementations, lifetime); this.collectionRegistrations.Add(registration); }
/// <summary> /// Scans for all implementations of <typeparamref name="TRegistration"/> and registers them, followed by the /// types defined by the <paramref name="defaultImplementations"/> parameter. /// </summary> /// <typeparam name="TRegistration">The <see cref="Type"/> to register as.</typeparam> /// <param name="defaultImplementations">The types to register last.</param> /// <param name="lifetime">Lifetime of the registration, defaults to singleton</param> /// <remarks> /// When scanning, it will exclude the assembly that the <see cref="Registrations"/> instance is defined in and it will also ignore /// the types specified by <paramref name="defaultImplementations"/>. /// </remarks> public void RegisterWithUserThenDefault <TRegistration>(IEnumerable <Type> defaultImplementations, Lifetime lifetime = Lifetime.Singleton) { var implementations = AppDomainAssemblyTypeScanner .TypesOf <TRegistration>() .Where(type => type.Assembly != this.GetType().Assembly) .Where(type => !defaultImplementations.Contains(type)) .ToList(); this.collectionRegistrations.Add(new CollectionTypeRegistration(typeof(TRegistration), implementations.Union(defaultImplementations), lifetime)); }
private static IRootPathProvider GetRootPathProvider() { var providerType = AppDomainAssemblyTypeScanner .TypesOf <IRootPathProvider>(ScanMode.ExcludeNancy) .SingleOrDefault(); if (providerType == null) { providerType = typeof(DefaultRootPathProvider); } return(Activator.CreateInstance(providerType) as IRootPathProvider); }
/// <summary> /// Scans for an implementation of <typeparamref name="TRegistration"/> and registers it if found. If no implementation could /// be found, it will retrieve an instance of <typeparamref name="TRegistration"/> using the provided <paramref name="defaultImplementationFactory"/>, /// which will be used in the registration. /// </summary> /// <typeparam name="TRegistration">The <see cref="Type"/> to register as.</typeparam> /// <param name="defaultImplementationFactory">Factory that provides an instance of <typeparamref name="TRegistration"/>.</param> /// <remarks>When scanning, it will exclude the assembly that the <see cref="Registrations"/> instance is defined in</remarks> public void RegisterWithDefault <TRegistration>(Func <TRegistration> defaultImplementationFactory) { var implementation = AppDomainAssemblyTypeScanner .TypesOf <TRegistration>() .SingleOrDefault(type => type.Assembly != this.GetType().Assembly); if (implementation != null) { this.typeRegistrations.Add(new TypeRegistration(typeof(TRegistration), implementation)); } else { this.instanceRegistrations.Add(new InstanceRegistration(typeof(TRegistration), defaultImplementationFactory.Invoke())); } }
/// <summary> /// Scans for all implementations of <typeparamref name="TRegistration"/>. If no implementations could be found, then it /// will register the types specified by <paramref name="defaultImplementations"/>. /// </summary> /// <typeparam name="TRegistration">The <see cref="Type"/> to register as.</typeparam> /// <param name="defaultImplementations">The types to register if non could be located while scanning.</param> /// <remarks> /// When scanning, it will exclude the assembly that the <see cref="ApplicationRegistrations"/> instance is defined in and it will also ignore /// the types specified by <paramref name="defaultImplementations"/>. /// </remarks> public void RegisterWithDefault <TRegistration>(IEnumerable <Type> defaultImplementations) { var implementations = AppDomainAssemblyTypeScanner .TypesOf <TRegistration>() .Where(type => type.Assembly != this.GetType().Assembly) .Where(type => !defaultImplementations.Contains(type)) .ToList(); if (!implementations.Any()) { implementations = defaultImplementations.ToList(); } this.collectionRegistrations.Add(new CollectionTypeRegistration(typeof(TRegistration), implementations)); }
private static IRootPathProvider GetRootPathProvider() { var providerTypes = AppDomainAssemblyTypeScanner .TypesOf <IRootPathProvider>(ScanMode.ExcludeNancy) .ToArray(); if (providerTypes.Length > 1) { throw new MultipleRootPathProvidersLocatedException(providerTypes); } var providerType = providerTypes.SingleOrDefault() ?? typeof(DefaultRootPathProvider); return(Activator.CreateInstance(providerType) as IRootPathProvider); }