Пример #1
0
        private static IEnumerable <KeyValueElementFactory> CreateElementsFactories(Type keyType, Type valueType, IEnumerable <object> values)
        {
            if (values.Any(value => !(value is XamlElement)))
            {
                throw new Granular.Exception("Can't add a value of type \"{0}\" to a dictionary, as it cannot have a key", values.First(value => !(value is XamlElement)).GetType().Name);
            }

            IEnumerable <XamlElement> valuesElements = values.Cast <XamlElement>();

            List <KeyValueElementFactory> list = new List <KeyValueElementFactory>();

            foreach (XamlElement contentChild in valuesElements)
            {
                bool isShared = contentChild.Directives.All(directive => directive.Name != XamlLanguage.SharedDirective || (bool)TypeConverter.ConvertValue(directive.GetSingleValue(), typeof(bool), XamlNamespaces.Empty));

                IElementFactory contentChildFactory = ElementFactory.FromXamlElement(contentChild, valueType);

                if (!isShared)
                {
                    contentChildFactory = new ValueProviderFactory(contentChildFactory);
                }

                list.Add(new KeyValueElementFactory(keyType, contentChildFactory, contentChild));
            }

            return(list);
        }
        // Get a single ValueProvider from a collection of factories. 
        // This will never return null.
        internal static IValueProvider GetValueProvider(HttpActionContext actionContext, ValueProviderFactory[] factories)
        {
            // Fast-path the case of just one factory
            if (factories.Length == 1)
            {
                IValueProvider provider = factories[0].GetValueProvider(actionContext);
                if (provider != null)
                {
                    return provider;
                }
            }

            List<IValueProvider> providers = new List<IValueProvider>();
            foreach (ValueProviderFactory factory in factories)
            {
                IValueProvider vp = factory.GetValueProvider(actionContext);
                if (vp != null)
                {
                    providers.Add(vp);
                }
            }

            // Fast-path the case of just one provider
            if (providers.Count == 1)
            {
                return providers[0];
            }
            
            return new CompositeValueProvider(providers);
        }
        public void Check_CustomModelBinder_On_Parameter()
        {
            HttpConfiguration config = new HttpConfiguration();

            config.Services.ReplaceRange(
                typeof(ValueProviderFactory),
                new ValueProviderFactory[] { new CustomValueProviderFactory(), }
                );

            DefaultActionValueBinder binder = new DefaultActionValueBinder();

            var binding = binder.GetBinding(
                GetAction("Action_CustomModelBinder_On_Parameter_WithProvider", config)
                );

            Assert.Single(binding.ParameterBindings);
            AssertIsModelBound(binding, 0);

            ModelBinderParameterBinding p = (ModelBinderParameterBinding)binding.ParameterBindings[
                0
                                            ];

            Assert.IsType <CustomModelBinder>(p.Binder);

            // Since the ModelBinderAttribute didn't specify the valueproviders, we should pull those from config.
            ValueProviderFactory valueProviderFactory = Assert.Single(p.ValueProviderFactories);

            Assert.IsType <CustomValueProviderFactory>(valueProviderFactory);
        }
Пример #4
0
            public override IEnumerable <ValueProviderFactory> GetValueProviderFactories(
                HttpConfiguration configuration
                )
            {
                var factories = new ValueProviderFactory[] { new HeaderValueProvider() };

                return(factories);
            }
Пример #5
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            var valueFactories = new ValueProviderFactory[ValueProviderFactories.Factories.Count];

            ValueProviderFactories.Factories.CopyTo(valueFactories, 0);
            ValueProviderFactories.Factories.Clear();

            ValueProviderFactories.Factories.Add(new DataContractValueProviderFactory());
        }
        public void SetItem()
        {
            // Arrange
            ValueProviderFactoryCollection collection = new ValueProviderFactoryCollection();

            collection.Add(new Mock <ValueProviderFactory>().Object);

            ValueProviderFactory newFactory = new Mock <ValueProviderFactory>().Object;

            // Act
            collection[0] = newFactory;

            // Assert
            ValueProviderFactory factory = Assert.Single(collection);

            Assert.Equal(newFactory, factory);
        }
        public void ValueProviderFactoryCollectionCombinedItemsCaches()
        {
            // Arrange
            var factories = new ValueProviderFactory[]
            {
                new Mock <ValueProviderFactory>(MockBehavior.Strict).Object,
                new Mock <ValueProviderFactory>(MockBehavior.Strict).Object
            };
            var collection = new ValueProviderFactoryCollection(factories);

            // Act
            var combined1 = collection.CombinedItems;
            var combined2 = collection.CombinedItems;

            // Assert
            Assert.Equal(factories, combined1);
            Assert.Same(combined1, combined2);
        }
        public void ValueProviderFactoryCollectionCombinedItemsCaches()
        {
            // Arrange
            var factories = new ValueProviderFactory[] 
            {
                new Mock<ValueProviderFactory>(MockBehavior.Strict).Object, 
                new Mock<ValueProviderFactory>(MockBehavior.Strict).Object
            };
            var collection = new ValueProviderFactoryCollection(factories);

            // Act
            var combined1 = collection.CombinedItems;
            var combined2 = collection.CombinedItems;

            // Assert
            Assert.Equal(factories, combined1);
            Assert.Same(combined1, combined2);
        }
        public void ValueProviderFactoryCollectionCombinedItemsDelegatesToResolver()
        {
            // Arrange
            var firstFactory = new Mock<ValueProviderFactory>();
            var secondFactory = new Mock<ValueProviderFactory>();
            var thirdFactory = new Mock<ValueProviderFactory>();
            var dependencyFactories = new ValueProviderFactory[] { firstFactory.Object, secondFactory.Object };
            var collectionFactories = new ValueProviderFactory[] { thirdFactory.Object };
            var expectedFactories = new ValueProviderFactory[] { firstFactory.Object, secondFactory.Object, thirdFactory.Object };

            var resolver = new Mock<IDependencyResolver>();
            resolver.Setup(r => r.GetServices(typeof(ValueProviderFactory))).Returns(dependencyFactories);

            var factories = new ValueProviderFactoryCollection(collectionFactories, resolver.Object);

            // Act
            ValueProviderFactory[] combined = factories.CombinedItems;

            // Assert
            Assert.Equal(expectedFactories, combined);
        }
        public void ValueProviderFactoryCollectionCombinedItemsDelegatesToResolver()
        {
            // Arrange
            var firstFactory        = new Mock <ValueProviderFactory>();
            var secondFactory       = new Mock <ValueProviderFactory>();
            var thirdFactory        = new Mock <ValueProviderFactory>();
            var dependencyFactories = new ValueProviderFactory[] { firstFactory.Object, secondFactory.Object };
            var collectionFactories = new ValueProviderFactory[] { thirdFactory.Object };
            var expectedFactories   = new ValueProviderFactory[] { firstFactory.Object, secondFactory.Object, thirdFactory.Object };

            var resolver = new Mock <IDependencyResolver>();

            resolver.Setup(r => r.GetServices(typeof(ValueProviderFactory))).Returns(dependencyFactories);

            var factories = new ValueProviderFactoryCollection(collectionFactories, resolver.Object);

            // Act
            ValueProviderFactory[] combined = factories.CombinedItems;

            // Assert
            Assert.Equal(expectedFactories, combined);
        }
Пример #11
0
        private static IEnumerable<KeyValueElementFactory> CreateElementsFactories(Type keyType, Type valueType, IEnumerable<object> values)
        {
            if (values.Any(value => !(value is XamlElement)))
            {
                throw new Granular.Exception("Can't add a value of type \"{0}\" to a dictionary, as it cannot have a key", values.First(value => !(value is XamlElement)).GetType().Name);
            }

            IEnumerable<XamlElement> valuesElements = values.Cast<XamlElement>();

            List<KeyValueElementFactory> list = new List<KeyValueElementFactory>();

            foreach (XamlElement contentChild in valuesElements)
            {
                bool isShared = contentChild.Directives.All(directive => directive.Name != XamlLanguage.SharedDirective || (bool)TypeConverter.ConvertValue(directive.GetSingleValue(), typeof(bool), XamlNamespaces.Empty));

                IElementFactory contentChildFactory = ElementFactory.FromXamlElement(contentChild, valueType);

                if (!isShared)
                {
                    contentChildFactory = new ValueProviderFactory(contentChildFactory);
                }

                list.Add(new KeyValueElementFactory(keyType, contentChildFactory, contentChild));
            }

            return list;
        }
Пример #12
0
 public GlimpseValueProviderFactory(ValueProviderFactory factory)
 {
     Factory = factory;
 }
        public void UpdateValueProviderFactories(ModelBinderInspector sut, IInspectorContext context, ValueProviderFactory proxy)
        {
            ValueProviderFactories.Factories.Add(new DummyValueProviderFactory());
            context.ProxyFactory.Setup(pf => pf.IsWrapClassEligible(It.IsAny <Type>())).Returns(true);
            context.ProxyFactory.Setup(pf => pf.WrapClass(It.IsAny <ValueProviderFactory>(), It.IsAny <IEnumerable <IAlternateMethod> >(), Enumerable.Empty <object>(), null)).Returns(proxy);

            sut.Setup(context);

            Assert.Contains(proxy, ValueProviderFactories.Factories);
            context.Logger.Verify(l => l.Info(It.IsAny <string>(), It.IsAny <object[]>()));
        }
        public void IgnoreEmptyValueProviderFactoriesCollection(ModelBinderInspector sut, IInspectorContext context, ValueProviderFactory proxy)
        {
            ValueProviderFactories.Factories.Clear();
            context.ProxyFactory.Setup(pf => pf.IsWrapClassEligible(It.IsAny <Type>())).Returns(true);
            context.ProxyFactory.Setup(pf => pf.WrapClass(It.IsAny <ValueProviderFactory>(), It.IsAny <IEnumerable <IAlternateMethod> >(), Enumerable.Empty <object>(), null)).Returns(proxy);

            sut.Setup(context);

            Assert.Empty(ValueProviderFactories.Factories);
        }
Пример #15
0
        private static IEnumerable<KeyElementFactory> CreateElementsFactories(XamlElement memberElement)
        {
            List<KeyElementFactory> list = new List<KeyElementFactory>();

            foreach (XamlElement contentChild in memberElement.GetContentChildren())
            {
                bool isShared = contentChild.Attributes.All(attribute => attribute.Name != XamlLanguage.SharedDirective || (bool)TypeConverter.ConvertValue(attribute.Value, typeof(bool), XamlNamespaces.Empty));

                IElementFactory contentChildFactory = ElementFactory.FromXamlElement(contentChild, null);

                if (!isShared)
                {
                    contentChildFactory = new ValueProviderFactory(contentChildFactory);
                }

                list.Add(new KeyElementFactory(contentChildFactory, contentChild));
            }

            return list;
        }