Example #1
0
        public void InjectionRegistrar_Registers_For_Single_Interface()
        {
            var container = new UnityContainer();
            var registrar = new InjectionRegistrar(GetClassFinderFor <Test2>());

            registrar.RegisterTypes(container);

            container.Resolve <ITest2 <Test1> >().Should().BeOfType <Test2>();
        }
Example #2
0
        public void InjectionRegistrar_Throws_Exception_For_Multiple_Types_One_Interface()
        {
            var container = new UnityContainer();
            var registrar = new InjectionRegistrar(GetClassFinderFor(typeof(Test1), typeof(Test1b)));

            Action action = () => {
                registrar.RegisterTypes(container);
            };

            action.Should().Throw <InvalidOperationException>();
        }
Example #3
0
        public void InjectionRegistrar_Throw_Exception_For_Mismatched_Decorator()
        {
            var container = new UnityContainer();
            var registrar = new InjectionRegistrar(GetClassFinderFor <Test5>());

            registrar.RegisterTypes(container);

            Action action = () => {
                registrar.RegisterTypes(container);
            };

            action.Should().Throw <InvalidOperationException>();
        }
Example #4
0
        public void InjectionRegistrar_Registers_Decorators()
        {
            var container = new UnityContainer();
            var registrar = new InjectionRegistrar(GetClassFinderFor <Test3a>());

            registrar.RegisterTypes(container);

            var decorator = container.Resolve <ITest3 <string> >();

            decorator.Should().BeOfType <TestDecorator1 <string> >();
            ((TestDecorator1 <string>)decorator).Handler.Should().BeOfType <Test3a>();
            decorator.GetValue().Should().Be("Success");
        }
Example #5
0
        public void InjectionRegistrar_Registers_Chained_Decorators()
        {
            var container = new UnityContainer();
            var registrar = new InjectionRegistrar(GetClassFinderFor <Test3b>());

            registrar.RegisterTypes(container);

            var decorator = container.Resolve <ITest3 <int> >();

            decorator.Should().BeOfType <TestDecorator1 <int> >();
            ((TestDecorator1 <int>)decorator).Handler.Should().BeOfType <TestDecorator2 <int> >();
            ((TestDecorator2 <int>)((TestDecorator1 <int>)decorator).Handler).Handler.Should().BeOfType <Test3b>();
            decorator.GetValue().Should().Be(1);
        }
Example #6
0
        /// <summary>
        /// Registers the type mappings with the Unity container.
        /// </summary>
        /// <param name="container">The unity container to configure.</param>
        /// <remarks>
        /// There is no need to register concrete types such as controllers or
        /// API controllers (unless you want to change the defaults), as Unity
        /// allows resolving a concrete type even if it was not previously
        /// registered.
        /// </remarks>
        public static void RegisterTypes(IUnityContainer container)
        {
            var registrar = new InjectionRegistrar(new ClassFinder());

            registrar.RegisterTypes(container);
        }