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 GetObjectIsThreadSafe()
        {
            ObjectFactory = CreateObjectFactory(true);

            GenericObjectDefinition theSpouse = new GenericObjectDefinition();

            theSpouse.ObjectTypeName = typeof(TestObject).FullName;
            theSpouse.IsSingleton    = false;
            ObjectFactory.RegisterObjectDefinition("theSpouse", theSpouse);

            GenericObjectDefinition theObject = new GenericObjectDefinition();

            theObject.ObjectTypeName = typeof(TestObject).FullName;
            theObject.IsSingleton    = false;
            theObject.PropertyValues.Add("Spouse", theSpouse);
            ObjectFactory.RegisterObjectDefinition("theObject", theObject);

            AsyncTestTask t1 = new AsyncTestMethod(20000, new ThreadStart(GetTheTestObject)).Start();
            AsyncTestTask t2 = new AsyncTestMethod(20000, new ThreadStart(GetTheTestObject)).Start();
            AsyncTestTask t3 = new AsyncTestMethod(20000, new ThreadStart(GetTheTestObject)).Start();
            AsyncTestTask t4 = new AsyncTestMethod(20000, new ThreadStart(GetTheTestObject)).Start();

            t1.AssertNoException();
            t2.AssertNoException();
            t3.AssertNoException();
            t4.AssertNoException();
        }
        public void DoesNotAcceptInfrastructureAdvisorsDuringScanning()
        {
            DefaultListableObjectFactory of = new DefaultListableObjectFactory();

            GenericObjectDefinition infrastructureAdvisorDefinition = new GenericObjectDefinition();

            infrastructureAdvisorDefinition.ObjectType = typeof(TestAdvisor);
            infrastructureAdvisorDefinition.PropertyValues.Add("Name", "InfrastructureAdvisor");
            infrastructureAdvisorDefinition.Role = ObjectRole.ROLE_INFRASTRUCTURE;
            of.RegisterObjectDefinition("infrastructure", infrastructureAdvisorDefinition);

            GenericObjectDefinition regularAdvisorDefinition = new GenericObjectDefinition();

            regularAdvisorDefinition.ObjectType = typeof(TestAdvisor);
            regularAdvisorDefinition.PropertyValues.Add("Name", "RegularAdvisor");
//            regularAdvisorDefinition.Role = ObjectRole.ROLE_APPLICATION;
            of.RegisterObjectDefinition("regular", regularAdvisorDefinition);

            TestAdvisorAutoProxyCreator apc = new TestAdvisorAutoProxyCreator();

            apc.ObjectFactory = of;
            object[] advisors = apc.GetAdvicesAndAdvisorsForObject(typeof(object), "dummyTarget");
            Assert.AreEqual(1, advisors.Length);
            Assert.AreEqual("RegularAdvisor", ((TestAdvisor)advisors[0]).Name);

            Assert.AreEqual(1, apc.CheckedAdvisors.Count);
            Assert.AreEqual("regular", apc.CheckedAdvisors[0]);
        }
        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 ShouldNotTryToResolveAbstractDefinitionsToType()
        {
            GenericObjectDefinition definition = new GenericObjectDefinition();

            definition.ObjectTypeName = "~/Default.aspx";
            definition.IsAbstract     = true;
            Assert.That(ConfigurationClassObjectDefinitionReader.CheckConfigurationClassCandidate(definition), Is.False);
        }
示例#6
0
        /// <summary>
        /// Creates definition of property retriever that returns property value of <c>target</c> object, where property is specified by <c>propertySelector</c>.
        /// </summary>
        /// <typeparam name="TTargetObject">Type of target object.</typeparam>
        /// <typeparam name="TPropertyType">Type of property value.</typeparam>
        /// <param name="target">Target object which property should be accessed.</param>
        /// <param name="propertySelector">Property selector.</param>
        /// <returns>Property retriever definition.</returns>
        public static IDefinition <TPropertyType> ObjectProperty <TTargetObject, TPropertyType>(IDefinition <TTargetObject> target, Expression <Func <TTargetObject, TPropertyType> > propertySelector)
        {
            var factoryDef = new GenericObjectDefinition {
                ObjectType = typeof(PropertyRetrievingFactoryObject)
            };

            factoryDef.PropertyValues.Add("TargetObject", target.DefinitionObject);
            factoryDef.PropertyValues.Add("TargetProperty", ReflectionUtils.GetPropertyPath(propertySelector));
            return(new Definition <TPropertyType>(factoryDef));
        }
        private static void WireParentAndChildWWithImpliedDependencyByConstructorInjection(bool parentIsSingleton, bool childIsSingleton)
        {
            GenericObjectDefinition child = new GenericObjectDefinition();
            child.ObjectTypeName = typeof(Child).FullName;
            child.IsSingleton = childIsSingleton;
            ObjectFactory.RegisterObjectDefinition("child", child);

            GenericObjectDefinition parent = new GenericObjectDefinition();
            parent.ObjectTypeName = typeof(Parent).FullName;
            parent.IsSingleton = parentIsSingleton;
            parent.ConstructorArgumentValues.AddIndexedArgumentValue(0, new RuntimeObjectReference("child"));
            ObjectFactory.RegisterObjectDefinition("parent", parent);
        }
        private void WireParentAndChildWithDependsOnDeclarationDependency(bool parentIsSingleton, bool childIsSingleton)
        {
            GenericObjectDefinition child = new GenericObjectDefinition();
            child.ObjectTypeName = typeof(Child).FullName;
            child.IsSingleton = childIsSingleton;
            ObjectFactory.RegisterObjectDefinition("child", child);

            GenericObjectDefinition parent = new GenericObjectDefinition();
            parent.ObjectTypeName = typeof(Parent).FullName;
            parent.IsSingleton = parentIsSingleton;
            parent.DependsOn = new string[] { "child" };
            ObjectFactory.RegisterObjectDefinition("parent", parent);
        }
        public void _SetUp()
        {
            ObjectFactory = CreateObjectFactory(true);

            GenericObjectDefinition threadCreatorInsideConstructor = new GenericObjectDefinition();
            threadCreatorInsideConstructor.ObjectTypeName = typeof(ThreadCreatorInsideConstructor).FullName;
            threadCreatorInsideConstructor.IsSingleton = true;
            ObjectFactory.RegisterObjectDefinition("threadCreatorInsideConstructor", threadCreatorInsideConstructor);

            GenericObjectDefinition threadCreatorInsideDispose = new GenericObjectDefinition();
            threadCreatorInsideDispose.ObjectTypeName = typeof(ThreadCreatorInsideDispose).FullName;
            threadCreatorInsideDispose.IsSingleton = true;
            ObjectFactory.RegisterObjectDefinition("threadCreatorInsideDispose", threadCreatorInsideDispose);
        }
示例#10
0
        private void LoadObjectDefinitionForConfigurationClassIfNecessary(ConfigurationClass configClass)
        {
            if (configClass.ObjectName != null)
            {
                // a Object definition already exists for this configuration class -> nothing to do
                return;
            }

            // no Object definition exists yet -> this must be an imported configuration class ([Import]).
            GenericObjectDefinition configObjectDef = new GenericObjectDefinition();
            String className = configClass.ConfigurationClassType.Name;

            configObjectDef.ObjectTypeName = className;
            configObjectDef.ObjectType     = configClass.ConfigurationClassType;
            if (CheckConfigurationClassCandidate(configClass.ConfigurationClassType))
            {
                String configObjectName = ObjectDefinitionReaderUtils.RegisterWithGeneratedName(configObjectDef,
                                                                                                _registry);
                configClass.ObjectName = configObjectName;
                Logger.LogDebug($"Registered object definition for imported [Configuration] class {configObjectName}");
            }
        }