/// <summary>
        /// Applies the extension configuration section loading behavior to the extensions.
        /// </summary>
        /// <param name="extensions">The extensions.</param>
        public void Behave(IEnumerable <IExtension> extensions)
        {
            Ensure.ArgumentNotNull(extensions, "extensions");

            foreach (IExtension extension in extensions)
            {
                IHaveConfigurationSectionName sectionNameProvider = this.factory.CreateHaveConfigurationSectionName(extension);
                ILoadConfigurationSection     sectionProvider     = this.factory.CreateLoadConfigurationSection(extension);

                ExtensionConfigurationSection section = GetExtensionConfigurationSection(sectionNameProvider, sectionProvider);

                if (!section.Configuration.OfType <ExtensionSettingsElement>().Any())
                {
                    continue;
                }

                IConsumeConfiguration consumer = this.factory.CreateConsumeConfiguration(extension);

                FillConsumerConfiguration(section, consumer);

                IHaveConversionCallbacks       conversionCallbacksProvider       = this.factory.CreateHaveConversionCallbacks(extension);
                IHaveDefaultConversionCallback defaultConversionCallbackProvider = this.factory.CreateHaveDefaultConversionCallback(extension);

                this.assignExtensionProperties.Assign(this.reflectExtensionProperties, extension, consumer, conversionCallbacksProvider, defaultConversionCallbackProvider);
            }
        }
        /// <inheritdoc />
        public void Assign(IReflectExtensionProperties reflector, IExtension extension, IConsumeConfiguration consumer, IHaveConversionCallbacks conversionCallbacksProvider, IHaveDefaultConversionCallback defaultConversionCallbackProvider)
        {
            Ensure.ArgumentNotNull(reflector, "reflector");
            Ensure.ArgumentNotNull(consumer, "consumer");
            Ensure.ArgumentNotNull(conversionCallbacksProvider, "conversionCallbacksProvider");
            Ensure.ArgumentNotNull(defaultConversionCallbackProvider, "defaultConversionCallbackProvider");

            IEnumerable<PropertyInfo> properties = reflector.Reflect(extension);
            IDictionary<string, IConversionCallback> conversionCallbacks = conversionCallbacksProvider.ConversionCallbacks;
            IConversionCallback defaultCallback = defaultConversionCallbackProvider.DefaultConversionCallback;

            foreach (KeyValuePair<string, string> keyValuePair in consumer.Configuration)
            {
                KeyValuePair<string, string> pair = keyValuePair;

                var matchedProperty = properties.Where(property => property.Name.Equals(pair.Key, StringComparison.OrdinalIgnoreCase))
                    .SingleOrDefault();

                if (matchedProperty == null)
                {
                    continue;
                }

                IConversionCallback conversionCallback;
                if (!conversionCallbacks.TryGetValue(pair.Key, out conversionCallback))
                {
                    conversionCallback = defaultCallback;
                }

                matchedProperty.SetValue(extension, conversionCallback.Convert(pair.Value, matchedProperty), null);
            }
        }
        public AssignExtensionPropertiesTest()
        {
            this.consumer = A.Fake <IConsumeConfiguration>();
            this.extensionPropertyReflector        = A.Fake <IReflectExtensionProperties>();
            this.conversionCallbacksProvider       = A.Fake <IHaveConversionCallbacks>();
            this.defaultConversionCallbackProvider = A.Fake <IHaveDefaultConversionCallback>();
            this.conversionCallback = A.Fake <IConversionCallback>();

            this.testee = new AssignExtensionProperties();
        }
        public ExtensionConfigurationSectionBehaviorTest()
        {
            this.consumer = A.Fake <IConsumeConfiguration>();
            this.extensionPropertyReflector        = A.Fake <IReflectExtensionProperties>();
            this.sectionNameProvider               = A.Fake <IHaveConfigurationSectionName>();
            this.sectionProvider                   = A.Fake <ILoadConfigurationSection>();
            this.conversionCallbacksProvider       = A.Fake <IHaveConversionCallbacks>();
            this.defaultConversionCallbackProvider = A.Fake <IHaveDefaultConversionCallback>();
            this.assigner = A.Fake <IAssignExtensionProperties>();

            this.factory = A.Fake <IExtensionConfigurationSectionBehaviorFactory>();
            this.SetupAutoStubFactory();

            this.extensions = new List <IExtension> {
                A.Fake <IExtension>(),
            };

            this.testee = new ExtensionConfigurationSectionBehavior(this.factory);
        }
Esempio n. 5
0
        /// <inheritdoc />
        public void Assign(IReflectExtensionProperties reflector, IExtension extension, IConsumeConfiguration consumer, IHaveConversionCallbacks conversionCallbacksProvider, IHaveDefaultConversionCallback defaultConversionCallbackProvider)
        {
            Ensure.ArgumentNotNull(reflector, "reflector");
            Ensure.ArgumentNotNull(consumer, "consumer");
            Ensure.ArgumentNotNull(conversionCallbacksProvider, "conversionCallbacksProvider");
            Ensure.ArgumentNotNull(defaultConversionCallbackProvider, "defaultConversionCallbackProvider");

            var properties = reflector.Reflect(extension).ToList();
            IDictionary <string, IConversionCallback> conversionCallbacks = conversionCallbacksProvider.ConversionCallbacks;
            IConversionCallback defaultCallback = defaultConversionCallbackProvider.DefaultConversionCallback;

            foreach (KeyValuePair <string, string> keyValuePair in consumer.Configuration)
            {
                KeyValuePair <string, string> pair = keyValuePair;

                var matchedProperty = properties.SingleOrDefault(property => property.Name.Equals(pair.Key, StringComparison.OrdinalIgnoreCase));

                if (matchedProperty == null)
                {
                    continue;
                }

                IConversionCallback conversionCallback;
                if (!conversionCallbacks.TryGetValue(pair.Key, out conversionCallback))
                {
                    conversionCallback = defaultCallback;
                }

                matchedProperty.SetValue(extension, conversionCallback.Convert(pair.Value, matchedProperty), null);
            }
        }