コード例 #1
0
        public void CanChainGenericTypesViaRegisterTypeMethod()
        {
            IUnityContainer container = new UnityContainer()
                                        .RegisterType(typeof(ICommand <>), typeof(LoggingCommand <>), new InjectionConstructor(new ResolvedParameter(typeof(ICommand <>), "concrete")))
                                        .RegisterType(typeof(ICommand <>), typeof(ConcreteCommand <>), "concrete");

            ICommand <User>       cmd    = container.Resolve <ICommand <User> >();
            LoggingCommand <User> logCmd = (LoggingCommand <User>)cmd;

            Assert.IsNotNull(logCmd.Inner);
            AssertExtensions.IsInstanceOfType(logCmd.Inner, typeof(ConcreteCommand <User>));
        }
コード例 #2
0
        public void CanConfigureInjectionForNonGenericMethodOnGenericClass()
        {
            IUnityContainer container = new UnityContainer();

            container.RegisterType(typeof(ICommand <>), typeof(LoggingCommand <>),
                                   new InjectionConstructor(),
                                   new InjectionMethod("InjectMe"));

            ICommand <Account>       result    = container.Resolve <ICommand <Account> >();
            LoggingCommand <Account> logResult = (LoggingCommand <Account>)result;

            Assert.IsTrue(logResult.WasInjected);
        }
コード例 #3
0
        public void CanInjectNestedGenerics()
        {
            IUnityContainer container = new UnityContainer()
                                        .RegisterType(typeof(ICommand <>), typeof(LoggingCommand <>),
                                                      new InjectionConstructor(new ResolvedParameter(typeof(ICommand <>), "concrete")))
                                        .RegisterType(typeof(ICommand <>), typeof(ConcreteCommand <>), "concrete");

            ICommand <Nullable <Customer> >       cmd    = container.Resolve <ICommand <Nullable <Customer> > >();
            LoggingCommand <Nullable <Customer> > logCmd = (LoggingCommand <Nullable <Customer> >)cmd;

            Assert.IsNotNull(logCmd.Inner);
            Assert.IsInstanceOfType(logCmd.Inner, typeof(ConcreteCommand <Nullable <Customer> >));
        }
コード例 #4
0
        public void ConfiguredGenericMethodInjectionIsCalled()
        {
            IUnityContainer container = new UnityContainer()
                                        .RegisterType(typeof(ICommand <>), typeof(LoggingCommand <>), new InjectionConstructor(new ResolvedParameter(typeof(ICommand <>), "concrete")),
                                                      new InjectionMethod("ChainedExecute", new ResolvedParameter(typeof(ICommand <>), "inner")))
                                        .RegisterType(typeof(ICommand <>), typeof(ConcreteCommand <>), "concrete")
                                        .RegisterType(typeof(ICommand <>), typeof(ConcreteCommand <>), "inner");

            ICommand <Account>       result = container.Resolve <ICommand <Account> >();
            LoggingCommand <Account> lc     = (LoggingCommand <Account>)result;

            Assert.IsTrue(lc.ChainedExecuteWasCalled);
        }
コード例 #5
0
        public void GenericPropertyIsActuallyInjected()
        {
            IUnityContainer container = new UnityContainer()
                                        .RegisterType(typeof(ICommand <>), typeof(LoggingCommand <>),
                                                      new InjectionConstructor(),
                                                      new InjectionProperty("Inner",
                                                                            new ResolvedParameter(typeof(ICommand <>), "inner")))
                                        .RegisterType(typeof(ICommand <>), typeof(ConcreteCommand <>), "inner");

            ICommand <Account> result = container.Resolve <ICommand <Account> >();

            LoggingCommand <Account> actualResult = (LoggingCommand <Account>)result;

            Assert.IsNotNull(actualResult.Inner);
            Assert.IsInstanceOfType(actualResult.inner, typeof(ConcreteCommand <Account>));
        }