Пример #1
0
        public void CreateComplexObject()
        {
            Builder builder = new Builder();
            Locator locator = CreateLocator();

            ConstructorPolicy policy = new ConstructorPolicy();

            policy.AddParameter(new ValueParameter <int>(12));
            builder.Policies.Set <ICreationPolicy>(policy, typeof(SimpleObject), null);

            ConstructorPolicy policy2 = new ConstructorPolicy();

            policy2.AddParameter(new CreationParameter(typeof(SimpleObject)));
            builder.Policies.Set <ICreationPolicy>(policy2, typeof(ComplexObject), null);

            builder.Policies.Set <ISingletonPolicy>(new SingletonPolicy(true), typeof(SimpleObject), null);
            builder.Policies.Set <ISingletonPolicy>(new SingletonPolicy(true), typeof(ComplexObject), null);

            ComplexObject cm = builder.BuildUp <ComplexObject>(locator, null, null);
            SimpleObject  m  = builder.BuildUp <SimpleObject>(locator, null, null);

            Assert.AreSame(m, cm.SimpleObject);
            Assert.IsNotNull(cm);
            Assert.IsNotNull(cm.SimpleObject);
            Assert.AreEqual(12, cm.SimpleObject.IntParam);
        }
Пример #2
0
        /// <summary>
        /// 构建策略
        /// </summary>
        /// <param name="servicedesc">服务描述</param>
        /// <param name="issave">是否保存服务</param>
        /// <param name="parameters">服务传入参数</param>
        /// <returns></returns>
        public PolicyList[] BuildPolicy(ServiceDesc servicedesc, bool issave, object[] parameters)
        {
            PolicyList[] policies   = new PolicyList[1];
            PolicyList   policylist = new PolicyList();

            policies[0] = policylist;

            //构建影射策略
            TypeMappingPolicy typepolicy = new TypeMappingPolicy(servicedesc.ImpleType, servicedesc.Key);

            policylist.Set <ITypeMappingPolicy>(typepolicy, servicedesc.ServiceType, servicedesc.Key);

            //构建单一构建策略(只有创建了单一创建策略才能够Locate)
            if (issave)
            {
                SingletonPolicy singletonpolicy = new SingletonPolicy(true);
                policylist.Set <ISingletonPolicy>(singletonpolicy, servicedesc.ImpleType, servicedesc.Key);
            }

            //建立创建策略,可以添加参数
            if (parameters != null && parameters.Length != 0)
            {
                ConstructorPolicy constuctorpolicy = new ConstructorPolicy();
                for (int i = 0; i < parameters.Length; i++)
                {
                    constuctorpolicy.AddParameter(new ValueParameter(parameters[i].GetType(), parameters[i]));
                }
                policylist.Set <ICreationPolicy>(constuctorpolicy, servicedesc.ImpleType, servicedesc.Key);
            }
            return(policies);
        }
Пример #3
0
        public void RefParamsCanAskForSpecificallyNamedObjects()
        {
            Builder builder = new Builder();
            Locator locator = CreateLocator();

            ConstructorPolicy policy1 = new ConstructorPolicy();

            policy1.AddParameter(new ValueParameter <int>(12));
            builder.Policies.Set <ICreationPolicy>(policy1, typeof(SimpleObject), "Object1");
            builder.Policies.Set <ISingletonPolicy>(new SingletonPolicy(true), typeof(SimpleObject), "Object1");

            ConstructorPolicy policy2 = new ConstructorPolicy();

            policy2.AddParameter(new ValueParameter <int>(32));
            builder.Policies.Set <ICreationPolicy>(policy2, typeof(SimpleObject), "Object2");
            builder.Policies.Set <ISingletonPolicy>(new SingletonPolicy(true), typeof(SimpleObject), "Object2");

            ConstructorPolicy policy3 = new ConstructorPolicy();

            policy3.AddParameter(new CreationParameter(typeof(SimpleObject), "Object2"));
            builder.Policies.Set <ICreationPolicy>(policy3, typeof(ComplexObject), null);
            builder.Policies.Set <ISingletonPolicy>(new SingletonPolicy(true), typeof(ComplexObject), null);

            ComplexObject cm = builder.BuildUp <ComplexObject>(locator, null, null);
            SimpleObject  sm = builder.BuildUp <SimpleObject>(locator, "Object2", null);

            Assert.IsNotNull(cm);
            Assert.IsNotNull(cm.SimpleObject);
            Assert.AreEqual(32, cm.SimpleObject.IntParam);
            Assert.AreSame(sm, cm.SimpleObject);
        }
		public void GetConstructorReturnsTheCorrectOneWhenParamsPassedThruAddParameter()
		{
			ConstructorPolicy policy = new ConstructorPolicy();

			policy.AddParameter(new ValueParameter<int>(5));
			ConstructorInfo actual = policy.SelectConstructor(new MockBuilderContext(), typeof(MockObject), null);
			ConstructorInfo expected = typeof(MockObject).GetConstructors()[1];

			Assert.AreSame(expected, actual);
		}
        public void GetConstructorReturnsTheCorrectOneWhenParamsPassedThruAddParameter()
        {
            ConstructorPolicy policy = new ConstructorPolicy();

            policy.AddParameter(new ValueParameter <int>(5));
            ConstructorInfo actual   = policy.SelectConstructor(new MockBuilderContext(), typeof(MockObject), null);
            ConstructorInfo expected = typeof(MockObject).GetConstructors()[1];

            Assert.AreSame(expected, actual);
        }
Пример #6
0
        /// <summary>
        /// See <see cref="ReflectionStrategy{T}.AddParametersToPolicy"/> for more information.
        /// </summary>
        protected override void AddParametersToPolicy(IBuilderContext context, Type typeToBuild, string idToBuild,
                                                      IReflectionMemberInfo <ConstructorInfo> member,
                                                      IEnumerable <IParameter> parameters)
        {
            ConstructorPolicy policy = new ConstructorPolicy();

            foreach (IParameter parameter in parameters)
            {
                policy.AddParameter(parameter);
            }

            context.Policies.Set <ICreationPolicy>(policy, typeToBuild, idToBuild);
        }
Пример #7
0
        public void CanAddPoliciesToBuilder()
        {
            Builder builder = new Builder();
            Locator locator = CreateLocator();

            ConstructorPolicy policy = new ConstructorPolicy();
            policy.AddParameter(new ValueParameter<int>(12));
            builder.Policies.Set<ICreationPolicy>(policy, typeof(MockObject), null);

            MockObject obj = builder.BuildUp<MockObject>(locator, null, null);

            Assert.IsNotNull(obj);
            Assert.AreEqual(12, obj.IntValue);
        }
Пример #8
0
        public void CanAddPoliciesToBuilderForTypeAndID()
        {
            Builder           builder = new Builder();
            Locator           locator = CreateLocator();
            ConstructorPolicy policy  = new ConstructorPolicy();

            policy.AddParameter(new ValueParameter <int>(14));
            builder.Policies.Set <ICreationPolicy>(policy, typeof(MockObject), "foo");

            MockObject obj = builder.BuildUp <MockObject>(locator, "foo", null);

            Assert.IsNotNull(obj);
            Assert.AreEqual(14, obj.IntValue);
        }
Пример #9
0
        public void CanCreateSingleton()
        {
            Builder builder = new Builder();
            Locator locator = CreateLocator();

            ConstructorPolicy policy = new ConstructorPolicy();

            policy.AddParameter(new ValueParameter <int>(12));
            builder.Policies.Set <ICreationPolicy>(policy, typeof(SimpleObject), null);
            builder.Policies.Set <ISingletonPolicy>(new SingletonPolicy(true), typeof(SimpleObject), null);

            SimpleObject m1 = builder.BuildUp <SimpleObject>(locator, null, null);
            SimpleObject m2 = builder.BuildUp <SimpleObject>(locator, null, null);

            Assert.AreSame(m1, m2);
        }
Пример #10
0
        private void ProcessConstructor(BuildRuleElement buildRule, Type buildType)
        {
            if (buildRule.Constructor == null)
            {
                return;
            }

            ConstructorPolicy policy = new ConstructorPolicy();

            foreach (object param in buildRule.Constructor.Items)
            {
                policy.AddParameter(GetParameterFromConfigParam(param));
            }

            builder.Policies.Set <ICreationPolicy>(policy, buildType, null);
        }
Пример #11
0
        public void CanCreateInstances()
        {
            Builder builder = new Builder();
            Locator locator = CreateLocator();

            ConstructorPolicy policy = new ConstructorPolicy();

            policy.AddParameter(new ValueParameter <int>(12));
            builder.Policies.Set <ICreationPolicy>(policy, typeof(SimpleObject), null);

            SimpleObject m1 = builder.BuildUp <SimpleObject>(locator, null, null);
            SimpleObject m2 = builder.BuildUp <SimpleObject>(locator, null, null);

            Assert.IsNotNull(m1);
            Assert.IsNotNull(m2);
            Assert.AreEqual(12, m1.IntParam);
            Assert.AreEqual(12, m2.IntParam);
            Assert.IsTrue(m1 != m2);
        }
Пример #12
0
        public void CanInjectMultiplePropertiesIncludingCreatedObjects()
        {
            Builder builder = new Builder();
            Locator locator = CreateLocator();

            ConstructorPolicy policy = new ConstructorPolicy();

            policy.AddParameter(new ValueParameter <int>(15));
            builder.Policies.Set <ICreationPolicy>(policy, typeof(SimpleObject), null);

            PropertySetterPolicy policy1 = new PropertySetterPolicy();

            policy1.Properties.Add("StringProperty", new PropertySetterInfo("StringProperty", new ValueParameter <string>("Bar is here")));
            policy1.Properties.Add("SimpleObject", new PropertySetterInfo("SimpleObject", new CreationParameter(typeof(SimpleObject))));
            builder.Policies.Set <IPropertySetterPolicy>(policy1, typeof(ComplexObject), null);

            ComplexObject co = builder.BuildUp <ComplexObject>(locator, null, null);

            Assert.IsNotNull(co);
            Assert.IsNotNull(co.SimpleObject);
            Assert.AreEqual("Bar is here", co.StringProperty);
            Assert.AreEqual(15, co.SimpleObject.IntParam);
        }