public void CanExportFromFactoryObjectIfObjectTypeIsInterface()
        {
            using (DefaultListableObjectFactory of = new DefaultListableObjectFactory())
            {
                MockRepository mocks = new MockRepository();
                IFactoryObject simpleCounterFactory = (IFactoryObject)mocks.DynamicMock(typeof(IFactoryObject));
                Expect.Call(simpleCounterFactory.ObjectType).Return(typeof(ISimpleCounter));
                Expect.Call(simpleCounterFactory.IsSingleton).Return(true);
                Expect.Call(simpleCounterFactory.GetObject()).Return(new SimpleCounter());

                mocks.ReplayAll();

                of.RegisterSingleton("simpleCounter", simpleCounterFactory);
                SaoExporter saoExporter = new SaoExporter();
                saoExporter.ObjectFactory = of;
                saoExporter.TargetName    = "simpleCounter";
                saoExporter.ServiceName   = "RemotedSaoCallCounter";
                saoExporter.AfterPropertiesSet();
                of.RegisterSingleton("simpleCounterExporter", saoExporter); // also tests SaoExporter.Dispose()!

                AssertExportedService(saoExporter.ServiceName, 2);

                mocks.VerifyAll();
            }
        }
示例#2
0
        public void CanExportFromFactoryObjectIfObjectTypeIsInterface()
        {
            using (DefaultListableObjectFactory of = new DefaultListableObjectFactory())
            {
                IFactoryObject simpleCounterFactory = A.Fake <IFactoryObject>();
                A.CallTo(() => simpleCounterFactory.ObjectType).Returns(typeof(ISimpleCounter));
                A.CallTo(() => simpleCounterFactory.IsSingleton).Returns(true);
                A.CallTo(() => simpleCounterFactory.GetObject()).Returns(new SimpleCounter());


                of.RegisterSingleton("simpleCounter", simpleCounterFactory);
                SaoExporter saoExporter = new SaoExporter();
                saoExporter.ObjectFactory = of;
                saoExporter.TargetName    = "simpleCounter";
                saoExporter.ServiceName   = "RemotedSaoCallCounter";
                saoExporter.AfterPropertiesSet();
                of.RegisterSingleton("simpleCounterExporter", saoExporter); // also tests SaoExporter.Dispose()!

                AssertExportedService(saoExporter.ServiceName, 2);
            }
        }
        /// <summary>
        /// Obtain an object to expose from the given IFactoryObject.
        /// </summary>
        /// <param name="factory">The IFactoryObject instance.</param>
        /// <param name="objectName">Name of the object.</param>
        /// <param name="rod">The merged object definition.</param>
        /// <returns>The object obtained from the IFactoryObject</returns>
        /// <exception cref="ObjectCreationException">If IFactoryObject object creation failed.</exception>
        private object GetObjectFromFactoryObject(IFactoryObject factory, string objectName, RootObjectDefinition rod)
        {
            object instance;

            try
            {
                instance = factory.GetObject();
            }
            catch (FactoryObjectNotInitializedException ex)
            {
                throw new ObjectCurrentlyInCreationException(
                    rod.ResourceDescription, objectName, ex);
            }
            catch (Exception ex)
            {
                throw new ObjectCreationException(rod.ResourceDescription, objectName,
                    "FactoryObject threw exception on object creation.", ex);
            }

            // Do not accept a null value for a FactoryBean that's not fully
            // initialized yet: Many FactoryBeans just return null then.
            if (instance == null && IsSingletonCurrentlyInCreation(objectName))
            {
                throw new ObjectCurrentlyInCreationException(rod.ResourceDescription, objectName,
                    "FactoryObject which is currently in creation returned null from GetObject.");
            }

            if (factory is IConfigurableFactoryObject)
            {
                IConfigurableFactoryObject configurableFactory = (IConfigurableFactoryObject)factory;

                #region Instrumentation

                if (log.IsDebugEnabled)
                {
                    log.Debug(string.Format("Factory object with name '{0}' is configurable.", TransformedObjectName(objectName)));
                }

                #endregion

                if (configurableFactory.ProductTemplate != null)
                {
                    ApplyObjectPropertyValues(instance, objectName, configurableFactory.ProductTemplate);
                }
            }

            if (instance != null)
            {
                try
                {
                    instance = PostProcessObjectFromFactoryObject(instance, objectName);
                }
                catch (Exception ex)
                {
                    throw new ObjectCreationException(rod.ResourceDescription, objectName,
                                "Post-processing of the FactoryObject's object failed.", ex);
                }
            }

            return instance;
        }