/// <summary> /// 根据配置文件的设置, 将构造原则(IPolicy)实例添加至Builder对象的构造原则链中. /// </summary> /// <param name="configSection">配置文件对象.</param> /// <param name="builder">Builder对象</param> private void applyConfigurationToBuilder(FactoryConfigurationSection configSection, IBuilder <BuilderStage> builder) { foreach (InterfaceElement interfaceEle in configSection.Interfaces) { foreach (InstanceElement instanceEle in interfaceEle.Instances) { // 添加接口与实现的Map关系!! TypeMappingPolicy tmp = new TypeMappingPolicy(instanceEle.Type, instanceEle.Name); builder.Policies.Set <ITypeMappingPolicy>(tmp, interfaceEle.Type, instanceEle.Name); // 添加接口实例的构造原则(IPolicy)(添加实例构造函数的参数列表). List <IParameter> paras = new List <IParameter>(); foreach (ConstructorParameterElement conParaEle in instanceEle.ConstructorParameters) { paras.Add(new ValueParameter(conParaEle.Type, conParaEle.GetParameterValue())); } ConstructorPolicy csp = new ConstructorPolicy(paras.ToArray()); builder.Policies.Set <ICreationPolicy>(csp, instanceEle.Type, instanceEle.Name); // 添加是否单件的构造原则(IPolicy). SingletonPolicy slp = new SingletonPolicy(instanceEle.IsSingleton); builder.Policies.Set <ISingletonPolicy>(slp, instanceEle.Type, instanceEle.Name); } } }
/// <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); }
public void CanMapTypesWithDefaultObjectBuilder() { WCSFBuilder builder = new WCSFBuilder(); Locator locator = CreateLocator(); TypeMappingPolicy policy = new TypeMappingPolicy(typeof(MockObject), null); builder.Policies.Set <ITypeMappingPolicy>(policy, typeof(IMockObject), null); IMockObject obj = builder.BuildUp <IMockObject>(locator, null, null); Assert.IsTrue(obj is MockObject); }
public void CanMapTypesWithDefaultObjectBuilder() { Builder builder = new Builder(); Locator locator = CreateLocator(); TypeMappingPolicy policy = new TypeMappingPolicy(typeof(MockObject), null); builder.Policies.Set<ITypeMappingPolicy>(policy, typeof(IMockObject), null); IMockObject obj = builder.BuildUp<IMockObject>(locator, null, null); Assert.IsTrue(obj is MockObject); }
public void PolicyReturnsGivenType() { TypeMappingPolicy policy = new TypeMappingPolicy(typeof(Foo), null); Assert.AreEqual(new DependencyResolutionLocatorKey(typeof(Foo), null), policy.Map(new DependencyResolutionLocatorKey())); }
public void PolicyReturnsGivenType() { TypeMappingPolicy policy = new TypeMappingPolicy(typeof (Foo), null); Assert.AreEqual(new DependencyResolutionLocatorKey(typeof (Foo), null), policy.Map(new DependencyResolutionLocatorKey())); }