public void PropertyReflectionWillNotOverwriteAPreExistingPolicyForAProperty()
        {
            MockBuilderContext   context = CreateContext();
            PropertySetterPolicy policy  = new PropertySetterPolicy();

            policy.Properties.Add("Foo", new PropertySetterInfo("Foo", new ValueParameter(typeof(object), 12)));
            context.Policies.Set <IPropertySetterPolicy>(policy, typeof(MockRequiresNewObject), null);

            MockRequiresNewObject obj = (MockRequiresNewObject)context.HeadOfChain.BuildUp(context, typeof(MockRequiresNewObject), null, null);

            Assert.AreEqual(12, obj.Foo);
        }
Example #2
0
        public void CanCreateObjectWithPropertyInjectionWithDefaultObjectBuilder()
        {
            Builder builder = new Builder();
            Locator locator = CreateLocator();

            PropertySetterPolicy policy = new PropertySetterPolicy();

            policy.Properties.Add("IntProp", new PropertySetterInfo("IntProp", new ValueParameter <int>(64)));
            builder.Policies.Set <IPropertySetterPolicy>(policy, typeof(PropertyObject), null);

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

            Assert.AreEqual(64, obj.IntProp);
        }
        public void IncompatibleTypesThrow()
        {
            MockBuilderContext     ctx      = new MockBuilderContext();
            PropertySetterStrategy strategy = new PropertySetterStrategy();

            PropertySetterPolicy policy = new PropertySetterPolicy();

            policy.Properties.Add("Foo", new PropertySetterInfo("Foo", new ValueParameter <object>(new object())));
            ctx.Policies.Set <IPropertySetterPolicy>(policy, typeof(MockInjectionTarget), null);

            MockInjectionTarget target = new MockInjectionTarget();

            strategy.BuildUp <MockInjectionTarget>(ctx, target, null);
        }
        public void ThrowsIfPropertyInjectedIsReadOnly()
        {
            MockBuilderContext     ctx      = new MockBuilderContext();
            PropertySetterStrategy strategy = new PropertySetterStrategy();

            PropertySetterPolicy policy1 = new PropertySetterPolicy();

            policy1.Properties.Add("Bar", new PropertySetterInfo("Bar", new ValueParameter <string>("value for foo")));
            ctx.Policies.Set <IPropertySetterPolicy>(policy1, typeof(MockInjectionTarget), null);

            MockInjectionTarget target = new MockInjectionTarget();

            strategy.BuildUp <object>(ctx, target, null);
        }
Example #5
0
        public void CanInjectValuesIntoProperties()
        {
            Builder builder = new Builder();
            Locator locator = CreateLocator();

            PropertySetterPolicy policy = new PropertySetterPolicy();

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

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

            Assert.IsNotNull(sm);
            Assert.AreEqual("Bar is here", sm.StringProperty);
        }
Example #6
0
        public void BuilderCanTakeTransientPolicies()
        {
            Builder    builder  = new Builder();
            Locator    locator  = CreateLocator();
            PolicyList policies = new PolicyList();

            PropertySetterPolicy policy = new PropertySetterPolicy();

            policy.Properties.Add("IntProp", new PropertySetterInfo("IntProp", new ValueParameter <int>(96)));
            policies.Set <IPropertySetterPolicy>(policy, typeof(PropertyObject), null);

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

            Assert.AreEqual(96, obj.IntProp);
        }
Example #7
0
        private void ProcessProperties(BuildRule buildRule, Type buildType)
        {
            if (buildRule.property == null)
            {
                return;
            }

            PropertySetterPolicy policy = new PropertySetterPolicy();

            foreach (Property prop in buildRule.property)
            {
                policy.Properties.Add(prop.name, new PropertySetterInfo(prop.name, GetParameterFromConfigParam(prop.Item)));
            }

            builder.Policies.Set <IPropertySetterPolicy>(policy, buildType, buildRule.name);
        }
        public void InjectionIsBasedOnConcreteTypeNotRequestedType()
        {
            MockBuilderContext     ctx      = new MockBuilderContext();
            PropertySetterStrategy strategy = new PropertySetterStrategy();

            PropertySetterPolicy policy1 = new PropertySetterPolicy();

            policy1.Properties.Add("Foo", new PropertySetterInfo("Foo", new ValueParameter <string>("value for foo")));
            ctx.Policies.Set <IPropertySetterPolicy>(policy1, typeof(MockInjectionTarget), null);

            MockInjectionTarget target = new MockInjectionTarget();

            strategy.BuildUp <object>(ctx, target, null);

            Assert.AreEqual("value for foo", target.Foo);
        }
Example #9
0
        /// <summary>
        /// See <see cref="ReflectionStrategy{T}.AddParametersToPolicy"/> for more information.
        /// </summary>
        protected override void AddParametersToPolicy(IBuilderContext context, Type typeToBuild, string idToBuild, IReflectionMemberInfo <PropertyInfo> member, IEnumerable <IParameter> parameters)
        {
            PropertySetterPolicy result = context.Policies.Get <IPropertySetterPolicy>(typeToBuild, idToBuild) as PropertySetterPolicy;

            if (result == null)
            {
                result = new PropertySetterPolicy();
                context.Policies.Set <IPropertySetterPolicy>(result, typeToBuild, idToBuild);
            }

            foreach (IParameter parameter in parameters)
            {
                if (!result.Properties.ContainsKey(member.Name))
                {
                    result.Properties.Add(member.Name, new PropertySetterInfo(member.MemberInfo, parameter));
                }
            }
        }
Example #10
0
        public void TransientPoliciesOverrideBuilderPolicies()
        {
            Builder    builder  = new Builder();
            Locator    locator  = CreateLocator();
            PolicyList policies = new PolicyList();

            PropertySetterPolicy builderPolicy = new PropertySetterPolicy();

            builderPolicy.Properties.Add("IntProp", new PropertySetterInfo("IntProp", new ValueParameter <int>(11)));
            builder.Policies.Set <IPropertySetterPolicy>(builderPolicy, typeof(PropertyObject), null);

            PropertySetterPolicy transientPolicy = new PropertySetterPolicy();

            transientPolicy.Properties.Add("IntProp", new PropertySetterInfo("IntProp", new ValueParameter <int>(22)));
            policies.Set <IPropertySetterPolicy>(transientPolicy, typeof(PropertyObject), null);

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

            Assert.AreEqual(22, obj.IntProp);
        }
Example #11
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);
        }
        public void PropertyReflectionWillNotOverwriteAPreExistingPolicyForAProperty()
        {
            MockBuilderContext context = CreateContext();
            PropertySetterPolicy policy = new PropertySetterPolicy();
            policy.Properties.Add("Foo", new PropertySetterInfo("Foo", new ValueParameter(typeof (object), 12)));
            context.Policies.Set<IPropertySetterPolicy>(policy, typeof (MockRequiresNewObject), null);

            MockRequiresNewObject obj =
                (MockRequiresNewObject) context.HeadOfChain.BuildUp(context, typeof (MockRequiresNewObject), null, null);

            Assert.AreEqual(12, obj.Foo);
        }