/// <summary>
        /// Resolve a reference to another object in the factory.
        /// </summary>
        /// <param name="name">
        /// The name of the object that is having the value of one of its properties resolved.
        /// </param>
        /// <param name="definition">
        /// The definition of the named object.
        /// </param>
        /// <param name="argumentName">
        /// The name of the property the value of which is being resolved.
        /// </param>
        /// <param name="reference">
        /// The runtime reference containing the value of the property.
        /// </param>
        /// <returns>A reference to another object in the factory.</returns>
        protected virtual object ResolveReference(IObjectDefinition definition, string name, string argumentName, RuntimeObjectReference reference)
        {
            #region Instrumentation
            if (log.IsDebugEnabled)
            {
                log.Debug(
                    string.Format(CultureInfo.InvariantCulture, "Resolving reference from property '{0}' in object '{1}' to object '{2}'.",
                                  argumentName, name, reference.ObjectName));
            }
            #endregion

            try
            {
                if (reference.IsToParent)
                {
                    if (null == objectFactory.ParentObjectFactory)
                    {
                        throw new ObjectCreationException(definition.ResourceDescription, name,
                                                          string.Format(
                                                              "Can't resolve reference to '{0}' in parent factory: " + "no parent factory available.",
                                                              reference.ObjectName));
                    }
                    return(objectFactory.ParentObjectFactory.GetObject(reference.ObjectName));
                }
                return(objectFactory.GetObject(reference.ObjectName));
            }
            catch (ObjectsException ex)
            {
                throw ObjectCreationException.GetObjectCreationException(ex, name, argumentName, definition.ResourceDescription, reference.ObjectName);
            }
        }
        public void ThrowsOnCyclicDependenciesOnNonSingletons()
        {
            AbstractObjectFactory of = this.CreateObjectFactory(true);

            GenericObjectDefinition od = new GenericObjectDefinition();

            od.ObjectTypeName = typeof(TestObject).FullName;
            od.IsSingleton    = false;
            od.PropertyValues.Add(new PropertyValue("Spouse", new RuntimeObjectReference("product2")));
            of.RegisterObjectDefinition("product1", od);

            GenericObjectDefinition od2 = new GenericObjectDefinition();

            od2.ObjectTypeName = typeof(TestObject).FullName;
            od2.IsSingleton    = false;
            od2.PropertyValues.Add(new PropertyValue("Sibling", new RuntimeObjectReference("product1")));
            of.RegisterObjectDefinition("product2", od2);

            try
            {
                of.GetObject("product1");
                Assert.Fail();
            }
            catch (ObjectCurrentlyInCreationException ex)
            {
                Assert.AreEqual("product1", ex.ObjectName);
            }
        }
        public void CanResolveCyclicSingletonFactoryObjectProductDependencies()
        {
            AbstractObjectFactory of = this.CreateObjectFactory(true);

            GenericObjectDefinition od = new GenericObjectDefinition();

            od.ObjectTypeName = typeof(TestObject).FullName;
            od.IsSingleton    = true;
            od.PropertyValues.Add(new PropertyValue("Spouse", new RuntimeObjectReference("product2")));
            of.RegisterObjectDefinition("product1Target", od);

            GenericObjectDefinition od2 = new GenericObjectDefinition();

            od2.ObjectTypeName = typeof(TestObject).FullName;
            od2.IsSingleton    = true;
            od2.PropertyValues.Add(new PropertyValue("Sibling", new RuntimeObjectReference("product1")));
            of.RegisterObjectDefinition("product2Target", od2);

            of.RegisterSingleton("product1", new ObjectReferenceFactoryObject("product1Target", of));
            of.RegisterSingleton("product2", new ObjectReferenceFactoryObject("product2Target", of));

            TestObject to = (TestObject)of.GetObject("product1");

            Assert.NotNull(to);
            Assert.NotNull(to.Spouse);
            Assert.NotNull(((TestObject)to.Spouse).Sibling);
        }
        public void RespectsCaseInsensitiveNamesAndAliases()
        {
            AbstractObjectFactory of = CreateObjectFactory(false);

            object testObject = new object();
            of.RegisterSingleton("name", testObject);
            of.RegisterAlias("NAME", "alias");

            try
            {
                of.RegisterAlias("NaMe", "AlIaS");
                Assert.Fail();
            }
            catch (ObjectDefinitionStoreException ex)
            {
                Assert.IsTrue(-1 < ex.Message.IndexOf("already registered"));
            }

            Assert.AreEqual(1, of.GetAliases("nAmE").Length);
            Assert.AreEqual(testObject, of.GetObject("nAmE"));
            Assert.AreEqual(testObject, of.GetObject("ALIAS"));
        }
        public void CanDisposeFactoryWhenDependentObjectCallsFactoryInDispose()
        {
            AbstractObjectFactory factory = CreateObjectFactory(false);
            ConfigureObjectFactory(factory as IObjectDefinitionRegistry);

            ParentClass parent = (ParentClass)factory.GetObject("Parent");
            Assert.That(parent, Is.Not.Null);

            DisposableClass innerObject = (DisposableClass)parent.InnerObject;
            innerObject.ObjectFactory = factory;

            factory.Dispose();

            Assert.Pass("Test concluded successfully.");
        }
예제 #6
0
            /// <summary>
            /// Returns the CAO proxy.
            /// </summary>
            /// <returns>The remote object.</returns>
            public object GetObject()
            {
                remoteObjectFactory.Target = objectFactory.GetObject(targetName);

                return(remoteObjectFactory.GetObject());
            }