public void CollectionPropertyNoKeys() { ServiceLocatorConfiguration configuration = ServiceLocatorConfigurationTests.LoadConfiguration("DelayStrategiesNoKeys.config"); CollectionPropertyConfigurationElement configElement = configuration.Services .OfType <ServiceConfigurationElement>() .Last() .ImplementationProperties .OfType <CollectionPropertyConfigurationElement>() .Single(); Assert.IsNull(configElement.ResolveKeys); }
public void CollectionPropertyEmptyKey() { ServiceLocatorConfiguration configuration = ServiceLocatorConfigurationTests.LoadConfiguration("DelayStrategiesEmptyKey.config"); CollectionPropertyConfigurationElement configElement = configuration.Services .OfType <ServiceConfigurationElement>() .Last() .ImplementationProperties .OfType <CollectionPropertyConfigurationElement>() .Single(); Assert.AreEqual(0, configElement.ResolveKeys.Count); }
public void CollectionPropertyTwoKeys() { ServiceLocatorConfiguration configuration = ServiceLocatorConfigurationTests.LoadConfiguration("DelayStrategiesTwoKeys.config"); CollectionPropertyConfigurationElement configElement = configuration.Services .OfType <ServiceConfigurationElement>() .Last() .ImplementationProperties .OfType <CollectionPropertyConfigurationElement>() .Single(); Assert.AreEqual(2, configElement.ResolveKeys.Count); Assert.AreEqual("None", configElement.ResolveKeys[0]); Assert.AreEqual("Exponential", configElement.ResolveKeys[1]); }
private void InitializeProperties(ServiceConfigurationElement configElement, object result) { foreach (PropertyConfigurationElementBase propertyConfig in configElement.ImplementationProperties.OfType <PropertyConfigurationElementBase>()) { ValuePropertyConfigurationElement valueConfig = propertyConfig as ValuePropertyConfigurationElement; if (valueConfig != null) { valueConfig.SetPropertyValue(result, valueConfig.ConvertedValue); continue; } ResolvePropertyConfigurationElement dependencyConfig = propertyConfig as ResolvePropertyConfigurationElement; if (dependencyConfig != null) { object dependency; if (string.IsNullOrEmpty(dependencyConfig.ResolveKey)) { dependency = this.ResolveSingle(dependencyConfig.DependencyType); } else { dependency = this.ResolveSingle(dependencyConfig.DependencyType, dependencyConfig.ResolveKey); } dependencyConfig.SetPropertyValue(result, dependency); continue; } CollectionPropertyConfigurationElement collectionConfig = propertyConfig as CollectionPropertyConfigurationElement; if (collectionConfig != null) { switch (collectionConfig.ResolveType) { case CollectionPropertyConfigurationElement.CollectionResolveType.Dependency: { IEnumerable items = null; if (collectionConfig.ResolveKeys == null) { if (collectionConfig.OptionalKeys == null) { items = this.configuration.Services.ResolveMultiple(collectionConfig.CollectionElementType) .Except(configElement) .Select(e => this.ResolveInstance(e)); } } else if (collectionConfig.ResolveKeys.Count == 0) { items = this.configuration.Services.ResolveMultiple(collectionConfig.CollectionElementType, string.Empty) .Except(configElement) .Select(e => this.ResolveInstance(e)); } else { items = collectionConfig.ResolveKeys .OfType <string>() .Select(key => this.ResolveSingle(collectionConfig.CollectionElementType, key)) .ToArray(); } if (items != null) { collectionConfig.AddToCollection(result, items); } if (collectionConfig.OptionalKeys != null) { items = collectionConfig.OptionalKeys .OfType <string>() .Where(key => this.configuration.Services.CanResolve(collectionConfig.CollectionElementType, key)) .Select(key => this.ResolveSingle(collectionConfig.CollectionElementType, key)) .ToArray(); collectionConfig.AddToCollection(result, items); } } break; case CollectionPropertyConfigurationElement.CollectionResolveType.Values: collectionConfig.AddValuesToCollection(result); break; default: throw new NotSupportedException <CollectionPropertyConfigurationElement.CollectionResolveType>(collectionConfig.ResolveType); } continue; } throw new NotSupportedException <PropertyConfigurationElementBase>(propertyConfig); } }