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 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 GetConstructorReturnsTheCorrectOneWhenParamsPassedThruCtor() { ConstructorPolicy policy = new ConstructorPolicy(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 IncompatibleTypesThrows() { MockBuilderContext ctx = CreateContext(); ILifetimeContainer container = ctx.Locator.Get <ILifetimeContainer>(); ConstructorInfo ci = typeof(MockObject).GetConstructor(new Type[] { typeof(int) }); ICreationPolicy policy = new ConstructorPolicy(ci, new ValueParameter <string>(String.Empty)); ctx.Policies.Set <ICreationPolicy>(policy, typeof(MockObject), null); object obj = ctx.HeadOfChain.BuildUp(ctx, typeof(MockObject), null, null); }
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 CanCreateNamedConcreteObjectByAskingForNamedInterface() { Builder builder = new Builder(); ConstructorPolicy policy = new ConstructorPolicy(new ValueParameter <int>(12)); builder.Policies.Set <ICreationPolicy>(policy, typeof(SimpleObject), "Foo"); builder.Policies.Set <ITypeMappingPolicy>(new TypeMappingPolicy(typeof(SimpleObject), null), typeof(ISimpleObject), null); builder.Policies.Set <ITypeMappingPolicy>(new TypeMappingPolicy(typeof(SimpleObject), "Foo"), typeof(ISimpleObject), "sm2"); Locator locator = CreateLocator(); ISimpleObject sm1 = builder.BuildUp <ISimpleObject>(locator, null, null); ISimpleObject sm2 = builder.BuildUp <ISimpleObject>(locator, "sm2", null); Assert.IsNotNull(sm1); Assert.IsNotNull(sm2); Assert.IsTrue(sm1 is SimpleObject); Assert.IsTrue(sm2 is SimpleObject); Assert.AreEqual(0, ((SimpleObject)sm1).IntParam); Assert.AreEqual(12, ((SimpleObject)sm2).IntParam); }
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 IncompatibleTypesThrows() { MockBuilderContext ctx = CreateContext(); ILifetimeContainer container = ctx.Locator.Get<ILifetimeContainer>(); ConstructorInfo ci = typeof (MockObject).GetConstructor(new Type[] {typeof (int)}); ICreationPolicy policy = new ConstructorPolicy(ci, new ValueParameter<string>(String.Empty)); ctx.Policies.Set<ICreationPolicy>(policy, typeof (MockObject), null); object obj = ctx.HeadOfChain.BuildUp(ctx, typeof (MockObject), null, null); }