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 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); }
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); }
/// <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); }
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); }
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); }
private void ProcessConstructor(BuildRule buildRule, Type buildType) { if (buildRule.constructorparams == null) { return; } ConstructorPolicy policy = new ConstructorPolicy(); foreach (object param in buildRule.constructorparams.Items) { policy.AddParameter(GetParameterFromConfigParam(param)); } builder.Policies.Set <ICreationPolicy>(policy, buildType, buildRule.name); }
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); }
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); }