public void GetsNoTypes()
 {
     WithMappings.None(typeof(TypeWithoutInterfaces)).AssertHasNoItems();
     WithMappings.None(typeof(DisposableType)).AssertHasNoItems();
     WithMappings.None(typeof(TestObject)).AssertHasNoItems();
     WithMappings.None(typeof(AnotherTestObject)).AssertHasNoItems();
 }
Exemplo n.º 2
0
        public static void Initialize()
        {
            IUnityContainer container = new UnityContainer();

            container.RegisterTypes(
                AllClasses.FromAssembliesInBasePath(),
                (c) => WithMappings.FromMatchingInterface(c));
        }
Exemplo n.º 3
0
        private static void RegisterTypes(UnityContainer unityContainer, params AssemblyReferenceBase[] assemblyReferences)
        {
            // First validate that there aren't 2 assemblies with the same ImplementationType
            var duplicateImplementationTypes = assemblyReferences.GroupBy(r => r.ImplementationType).Where(r => r.Count() > 1);

            if (duplicateImplementationTypes.Any())
            {
                throw new Exception(duplicateImplementationTypes.Select(d => String.Format("Both {1} are registered as ImplementationType '{0}', there can be only a single assemble registered with any given ImplementationType.",
                                                                                           d.Key,
                                                                                           d.Select(r => "'" + r.GetType().Name + "'").Aggregate((s1, s2) => s1 + " and " + s2)))
                                    .Aggregate((s1, s2) => s1 + "\r\n-----------------------------------\r\n" + s2));
            }

            // Get all types that implement IAction
            var actionImplementations = assemblyReferences
                                        .SelectMany(r => AllClasses.FromAssemblies(r.GetType().Assembly)
                                                    .Where(t => typeof(IAction).IsAssignableFrom(t))
                                                    .Select(t => new
            {
                Type = t,
                r.ImplementationType
            }));

            // Get all interfaces from those types that inherit from IAction
            var actionsToRegister = actionImplementations
                                    .SelectMany(t => WithMappings.FromAllInterfaces(t.Type)
                                                .Where(i => i != typeof(IAction) && typeof(IAction).IsAssignableFrom(i))
                                                .Concat(new[] { t.Type }) // We also want to register the type directly
                                                .Select(i => new
            {
                Interface = i,
                t.Type,
                t.ImplementationType
            }));

            // Group the actions together and sort the implementations by priority
            var actionsGroupedByPriority = actionsToRegister
                                           .GroupBy(i => i.Interface)
                                           .Select(i => new
            {
                Interface       = i.Key,
                Implementations = i.OrderBy(t => ImplementationTypePriorities.Value[t.ImplementationType]).ToList()
            });

            foreach (var action in actionsGroupedByPriority)
            {
                // Register the default implementation (the implementation with the highest priority)
                unityContainer.RegisterType(action.Interface, action.Implementations.First().Type);

                foreach (var implementation in action.Implementations)
                {
                    // Register all implementations as named registrations
                    unityContainer.RegisterType(action.Interface, implementation.Type, implementation.ImplementationType.ToString());
                }
            }
        }
        public void WhenMappedToInterfacesInSameAssemblyIsNull()
        {
            IUnityContainer container = new UnityContainer();

            AssertHelper.ThrowsException <ArgumentNullException>(() => container.RegisterTypes(
                                                                     AllClasses.FromAssemblies(
                                                                         Assembly.Load(new AssemblyName(RegistrationByConventionAssembly1Name)),
                                                                         Assembly.Load(new AssemblyName(RegistrationByConventionAssembly2Name))),
                                                                     (t) => WithMappings.FromAllInterfacesInSameAssembly((null)),
                                                                     WithName.TypeName,
                                                                     WithLifetime.ContainerControlled));
        }
        public void GetsAllInterfacesInSameAssembly()
        {
            WithMappings.FromAllInterfacesInSameAssembly(typeof(TypeWithoutInterfaces)).AssertHasNoItems();
            WithMappings.FromAllInterfacesInSameAssembly(typeof(DisposableType)).AssertHasNoItems();
            WithMappings.FromAllInterfacesInSameAssembly(typeof(TestObject)).AssertContainsInAnyOrder(typeof(ITestObject), typeof(IAnotherInterface));
            WithMappings.FromAllInterfacesInSameAssembly(typeof(AnotherTestObject)).AssertContainsInAnyOrder(typeof(ITestObject), typeof(IAnotherInterface));

            // Generics
            WithMappings.FromAllInterfacesInSameAssembly(typeof(GenericTestObject <,>)).AssertContainsExactly(typeof(IGenericTestObject <,>));
            WithMappings.FromAllInterfacesInSameAssembly(typeof(GenericTestObjectAlt <,>)).AssertHasNoItems();
            WithMappings.FromAllInterfacesInSameAssembly(typeof(GenericTestObject <>)).AssertHasNoItems();
            WithMappings.FromAllInterfacesInSameAssembly(typeof(GenericTestObject)).AssertContainsExactly(typeof(IGenericTestObject <string, int>));
        }
        public void GetsMatchingInterface()
        {
            WithMappings.FromMatchingInterface(typeof(TypeWithoutInterfaces)).AssertHasNoItems();
            WithMappings.FromMatchingInterface(typeof(DisposableType)).AssertHasNoItems();
            WithMappings.FromMatchingInterface(typeof(TestObject)).AssertContainsInAnyOrder(typeof(ITestObject));
            WithMappings.FromMatchingInterface(typeof(AnotherTestObject)).AssertHasNoItems();

            // Generics
            WithMappings.FromMatchingInterface(typeof(GenericTestObject <,>)).AssertContainsExactly(typeof(IGenericTestObject <,>));
            WithMappings.FromMatchingInterface(typeof(GenericTestObjectAlt <,>)).AssertHasNoItems();
            WithMappings.FromMatchingInterface(typeof(GenericTestObject <>)).AssertHasNoItems();
            WithMappings.FromMatchingInterface(typeof(GenericTestObject)).AssertHasNoItems();
        }
        public void GetsAllInterfaces()
        {
            WithMappings.FromAllInterfaces(typeof(TypeWithoutInterfaces)).AssertHasNoItems();
            WithMappings.FromAllInterfaces(typeof(DisposableType)).AssertHasNoItems();
            WithMappings.FromAllInterfaces(typeof(TestObject)).AssertContainsInAnyOrder(typeof(IAnotherInterface), typeof(ITestObject), typeof(IComparable));
            WithMappings.FromAllInterfaces(typeof(AnotherTestObject)).AssertContainsInAnyOrder(typeof(IAnotherInterface), typeof(ITestObject), typeof(IComparable));

            // Generics
            WithMappings.FromAllInterfaces(typeof(GenericTestObject <,>)).AssertContainsInAnyOrder(typeof(IGenericTestObject <,>));
            WithMappings.FromAllInterfaces(typeof(GenericTestObjectAlt <,>)).AssertHasNoItems();
            WithMappings.FromAllInterfaces(typeof(GenericTestObject <>)).AssertContainsInAnyOrder(typeof(IComparable <>));
            WithMappings.FromAllInterfaces(typeof(GenericTestObject)).AssertContainsInAnyOrder(typeof(IGenericTestObject <string, int>), typeof(IComparable <int>), typeof(IEnumerable <IList <string> >), typeof(IEnumerable));
        }
Exemplo n.º 8
0
    public static void RegisterValidators(this IUnityContainer container)
    {
        var types = AllClasses.FromLoadedAssemblies().GetValidators();

        foreach (var type in types)
        {
            var interfaces = WithMappings.FromAllInterfaces(type);
            foreach (var @interface in interfaces)
            {
                container.RegisterType(@interface, type);
            }
        }
    }
        public void WhenMappedToTypeHasNoInterface()
        {
            IUnityContainer container = new UnityContainer();

            container.RegisterTypes(
                AllClasses.FromAssemblies(
                    Assembly.Load(new AssemblyName(RegistrationByConventionAssembly1Name)),
                    Assembly.Load(new AssemblyName(RegistrationByConventionAssembly2Name))),
                (t) => WithMappings.FromAllInterfaces(typeof(TypeWithNoInterface)),
                WithName.TypeName,
                null);

            Assert.AreEqual <int>(1, container.Registrations.Count());
        }
Exemplo n.º 10
0
        public static IUnityContainer RegisterAllValidators(this IUnityContainer container, IEnumerable <Type> validatorTypes, LifetimeManager lifetimeManager = null, bool mapInterfaces = true)
        {
            container.RegisterTypes(
                validatorTypes,
                t => new[] { t }.Concat(
                    mapInterfaces ?
                    WithMappings.FromAllInterfaces(t)
                    .Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IValidator <>)) :
                    Enumerable.Empty <Type>()),
                WithName.Default,
                _ => lifetimeManager ?? new SingletonLifetimeManager());

            return(container);
        }
        public void WhenMappedToTypeHasNoInterfaceAndMappedToNoType()
        {
            IUnityContainer container = new UnityContainer();

            container.RegisterTypes(
                AllClasses.FromAssemblies(
                    Assembly.Load(new AssemblyName(RegistrationByConventionAssembly1Name)),
                    Assembly.Load(new AssemblyName(RegistrationByConventionAssembly2Name))),
                (t) => WithMappings.None(typeof(TypeWithNoInterface)),
                WithName.TypeName,
                null);

            var resolved = container.Resolve <TypeWithNoInterface>("TypeWithNoInterface");

            Assert.IsInstanceOfType(resolved, typeof(TypeWithNoInterface));
        }
        public void WhenFilteringToMatchingInterface()
        {
            IUnityContainer container = new UnityContainer();

            var mappings = WithMappings.FromMatchingInterface(typeof(TypeImplementing8));

            container.RegisterTypes(
                AllClasses.FromAssemblies(Assembly.Load(new AssemblyName(RegistrationByConventionAssembly1Name))),
                WithMappings.FromMatchingInterface,
                WithName.TypeName,
                WithLifetime.Transient,
                null,
                true);

            var resolved = container.Resolve <ITypeImplementing8>("TypeImplementing8");

            Assert.IsInstanceOfType(resolved, typeof(TypeImplementing8));
        }
        public void WhenFilteringToNoTypes()
        {
            IUnityContainer container = new UnityContainer();

            container.RegisterTypes(
                AllClasses.FromAssemblies(
                    Assembly.Load(new AssemblyName(RegistrationByConventionAssembly1Name)),
                    Assembly.Load(new AssemblyName(RegistrationByConventionAssembly2Name))),
                (t) => { return(WithMappings.None(typeof(TypeImplementingI12))); },
                WithName.TypeName,
                null);

            foreach (var reg in container.Registrations)
            {
                System.Diagnostics.Debug.WriteLine(reg.Name);
            }

            var resolved = container.Resolve <TypeImplementingI12>("TypeImplementingI12");

            Assert.IsInstanceOfType(resolved, typeof(TypeImplementingI12));

            AssertHelper.ThrowsException <ResolutionFailedException>(() => container.Resolve <ITypeImplementingI1>("TypeImplementingI12"));
        }
Exemplo n.º 14
0
        private static IEnumerable <Type> SelectInterfacesToRegister(Type type)
        {
            var allInterfaces = WithMappings.FromAllInterfaces(type);

            return(from intf in allInterfaces where IsInNamespace(intf, "Aspen.DailyUpdates") select intf);
        }
Exemplo n.º 15
0
 private IEnumerable <Type> UserDefinedInterfaces(Type implementingType)
 {
     return(WithMappings.FromAllInterfaces(implementingType)
            .Where(iface => iface.Assembly.FullName.StartsWith(MatchingAssemblyPrefix)));
 }