public void CanProxyFactoryMethodProducts()
        {
            GenericApplicationContext ctx = new GenericApplicationContext();
            ctx.ObjectFactory.AddObjectPostProcessor(new DefaultAdvisorAutoProxyCreator());

            CapturingAdvice capturingAdvice = new CapturingAdvice();
            ctx.ObjectFactory.RegisterSingleton("logging", new DefaultPointcutAdvisor(TruePointcut.True, capturingAdvice));

            // register "factory" object 
            RootObjectDefinition rod;
            rod = new RootObjectDefinition(typeof(TestObjectFactoryObject));
            ctx.ObjectFactory.RegisterObjectDefinition("test", rod);

            // register product, referencing the factory object
            rod = new RootObjectDefinition(typeof(ITestObject));
            rod.FactoryObjectName = "test";
            rod.FactoryMethodName = "CreateTestObject";
            ctx.ObjectFactory.RegisterObjectDefinition("testProduct", rod);

            ctx.Refresh();

            ITestObjectFactoryObject fo = (ITestObjectFactoryObject) ctx.GetObject("test");
            Assert.IsTrue( AopUtils.IsAopProxy(fo) );
            Assert.AreEqual("CreateTestObject", ((IMethodInvocation)capturingAdvice.CapturedCalls[0]).Method.Name);

            capturingAdvice.CapturedCalls.Clear();
            ITestObject to = (ITestObject)ctx.GetObject("testProduct");
            Assert.IsTrue( AopUtils.IsAopProxy(to) );
            Assert.AreEqual("TheName", to.Name);
            Assert.AreEqual("get_Name", ((IMethodInvocation)capturingAdvice.CapturedCalls[0]).Method.Name);
        }
Пример #2
0
        public void CanProxyFactoryMethodProducts()
        {
            GenericApplicationContext ctx = new GenericApplicationContext();

            ctx.ObjectFactory.AddObjectPostProcessor(new DefaultAdvisorAutoProxyCreator());

            CapturingAdvice capturingAdvice = new CapturingAdvice();

            ctx.ObjectFactory.RegisterSingleton("logging", new DefaultPointcutAdvisor(TruePointcut.True, capturingAdvice));

            // register "factory" object
            RootObjectDefinition rod;

            rod = new RootObjectDefinition(typeof(TestObjectFactoryObject));
            ctx.ObjectFactory.RegisterObjectDefinition("test", rod);

            // register product, referencing the factory object
            rod = new RootObjectDefinition(typeof(ITestObject));
            rod.FactoryObjectName = "test";
            rod.FactoryMethodName = "CreateTestObject";
            ctx.ObjectFactory.RegisterObjectDefinition("testProduct", rod);

            ctx.Refresh();

            ITestObjectFactoryObject fo = (ITestObjectFactoryObject)ctx.GetObject("test");

            Assert.IsTrue(AopUtils.IsAopProxy(fo));
            Assert.AreEqual("CreateTestObject", ((IMethodInvocation)capturingAdvice.CapturedCalls[0]).Method.Name);

            capturingAdvice.CapturedCalls.Clear();
            ITestObject to = (ITestObject)ctx.GetObject("testProduct");

            Assert.IsTrue(AopUtils.IsAopProxy(to));
            Assert.AreEqual("TheName", to.Name);
            Assert.AreEqual("get_Name", ((IMethodInvocation)capturingAdvice.CapturedCalls[0]).Method.Name);
        }