private static Type[] GetTypesToRegister(Type serviceType, TypesToRegisterOptions options) { var container = new Container(); var serviceTypeAssembly = new[] { serviceType.GetTypeInfo().Assembly }; return(container.GetTypesToRegister(serviceType, serviceTypeAssembly, options).ToArray()); }
public void GetTypesToRegister_IncludeGenericTypeDefinitions_ContainsListOfExpectedTypes() { // Arrange var expectedTypes = new[] { typeof(NonGeneric1), typeof(NonGeneric2), typeof(GenericTypeDef <>), typeof(NonGenericComposite), typeof(GenericComposite <>), typeof(NonGenericWrappingCollection), typeof(GenericWrappingCollection <>), }; var options = new TypesToRegisterOptions { IncludeGenericTypeDefinitions = true }; // Act var actualTypes = GetTypesToRegister(typeof(IService <>), options); // Assert Assert_CollectionsContainExactSameTypes(expectedTypes, actualTypes, "Excluded should have been: Everything that is a decorator, so generic decorators as well."); }
public void GetTypesToRegister_IncludeDecoratorsAndGenericTypesButNoComposites_ContainsListOfExpectedTypes() { // Arrange var expectedTypes = new[] { typeof(NonGeneric1), typeof(NonGeneric2), typeof(GenericTypeDef <>), typeof(NonGenericDecorator), typeof(GenericDecorator <>), typeof(NonGenericWrappingCollection), typeof(GenericWrappingCollection <>), }; // IncludeComposites is true by default. var options = new TypesToRegisterOptions { IncludeDecorators = true, IncludeGenericTypeDefinitions = true, IncludeComposites = false, }; // Act var actualTypes = GetTypesToRegister(typeof(IService <>), options); // Assert Assert_CollectionsContainExactSameTypes(expectedTypes, actualTypes, "Excluded should have been: everything that's a decorator (also decorator-composites)."); }
public void GetTypesToRegister_IncludeDecoratorsCompositesAndGenericTypes_ContainsListOfExpectedTypes() { // Arrange var expectedTypes = new[] { typeof(NonGeneric1), typeof(NonGeneric2), typeof(GenericTypeDef <>), typeof(NonGenericDecorator), typeof(GenericDecorator <>), typeof(NonGenericComposite), typeof(GenericComposite <>), typeof(NonGenericWrappingCollection), typeof(GenericWrappingCollection <>), typeof(NonGenericCompositeDecorator), typeof(GenericCompositeDecorator <>), }; // IncludeComposites is true by default. var options = new TypesToRegisterOptions { IncludeDecorators = true, IncludeGenericTypeDefinitions = true }; // Act var actualTypes = GetTypesToRegister(typeof(IService <>), options); // Assert Assert_CollectionsContainExactSameTypes(expectedTypes, actualTypes, "Nothing should be included."); }
/// <summary> /// Automatically resolves an implementation of an unregistered abstraction, by searching the supplied /// list of assemblies. The implementation will be resolved only if the list of assemblies contains /// exactly one implementation of the missing abstraction. /// </summary> /// <param name="container">The container.</param> /// <param name="assemblies">The list of assemblies to search for implementations.</param> public static void AutoResolveMatchingImplementation(this Container container, params Assembly[] assemblies) { var options = new TypesToRegisterOptions { IncludeDecorators = false, IncludeGenericTypeDefinitions = false, IncludeComposites = false }; container.ResolveUnregisteredType += (s, e) => { Type serviceType = e.UnregisteredServiceType; if (serviceType.IsAbstract) { var types = container.GetTypesToRegister(serviceType, assemblies, options).ToArray(); // Only map when there is no ambiguity, meaning: exactly one implementation. if (types.Length == 1) { e.Register(container.Options.DefaultLifestyle.CreateRegistration(types[0], container)); } } }; }
public void TypesToRegisterOptions_WithDefaultValues_ContainsExpectedValues() { // Act var defaultOptions = new TypesToRegisterOptions(); // Assert Assert.IsFalse(defaultOptions.IncludeDecorators); Assert.IsFalse(defaultOptions.IncludeGenericTypeDefinitions); Assert.IsTrue(defaultOptions.IncludeComposites); }
public void GetTypesToRegister_WithDefaultOptions_ContainsListOfExpectedTypes() { // Arrange var expectedTypes = new[] { typeof(NonGeneric1), typeof(NonGeneric2), typeof(NonGenericComposite), typeof(NonGenericWrappingCollection), }; var defaultOptions = new TypesToRegisterOptions(); // Act var actualTypes = GetTypesToRegister(typeof(IService <>), defaultOptions); // Assert Assert_CollectionsContainExactSameTypes(expectedTypes, actualTypes, "Excluded should have been: Everything that is generic or decorator."); }