Ejemplo n.º 1
0
        // Expression:
        // new NamedProperty<T> { Name = property.Name, Value = property.Value, Next = next }.
        private static Expression CreateNamedPropertyCreationExpression(NamedPropertyExpression property, Expression next)
        {
            Contract.Assert(property != null);
            Contract.Assert(property.Value != null);

            Type namedPropertyType = GetNamedPropertyType(property, next);
            List <MemberBinding> memberBindings = new List <MemberBinding>();

            memberBindings.Add(Expression.Bind(namedPropertyType.GetProperty("Name"), property.Name));

            if (property.PageSize == null)
            {
                memberBindings.Add(Expression.Bind(namedPropertyType.GetProperty("Value"), property.Value));
            }
            else
            {
                memberBindings.Add(Expression.Bind(namedPropertyType.GetProperty("Collection"), property.Value));
                memberBindings.Add(Expression.Bind(namedPropertyType.GetProperty("PageSize"), Expression.Constant(property.PageSize)));
            }

            if (next != null)
            {
                memberBindings.Add(Expression.Bind(namedPropertyType.GetProperty("Next"), next));
            }
            if (property.NullCheck != null)
            {
                memberBindings.Add(Expression.Bind(namedPropertyType.GetProperty("IsNull"), property.NullCheck));
            }

            return(Expression.MemberInit(Expression.New(namedPropertyType), memberBindings));
        }
Ejemplo n.º 2
0
        private Expression BuildPropertyContainer(IEdmEntityType elementType, Expression source,
                                                  Dictionary <IEdmNavigationProperty, SelectExpandClause> propertiesToExpand,
                                                  ISet <IEdmStructuralProperty> propertiesToInclude, ISet <IEdmStructuralProperty> autoSelectedProperties)
        {
            IList <NamedPropertyExpression> includedProperties = new List <NamedPropertyExpression>();

            foreach (KeyValuePair <IEdmNavigationProperty, SelectExpandClause> kvp in propertiesToExpand)
            {
                IEdmNavigationProperty propertyToExpand = kvp.Key;
                SelectExpandClause     projection       = kvp.Value;

                Expression propertyName  = CreatePropertyNameExpression(elementType, propertyToExpand, source);
                Expression propertyValue = CreatePropertyValueExpression(elementType, propertyToExpand, source);
                Expression nullCheck     = Expression.Equal(propertyValue, Expression.Constant(null));

                // projection can be null if the expanded navigation property is not further projected or expanded.
                if (projection != null)
                {
                    propertyValue = ProjectAsWrapper(propertyValue, projection, propertyToExpand.ToEntityType());
                }

                NamedPropertyExpression propertyExpression = new NamedPropertyExpression(propertyName, propertyValue);
                if (projection != null)
                {
                    if (!propertyToExpand.Type.IsCollection())
                    {
                        propertyExpression.NullCheck = nullCheck;
                    }
                    else if (_settings.PageSize != null)
                    {
                        propertyExpression.PageSize = _settings.PageSize.Value;
                    }
                }

                //includedProperties.Add(propertyExpression);
            }

            foreach (IEdmStructuralProperty propertyToInclude in propertiesToInclude)
            {
                Expression propertyName  = CreatePropertyNameExpression(elementType, propertyToInclude, source);
                Expression propertyValue = CreatePropertyValueExpression(elementType, propertyToInclude, source);
                includedProperties.Add(new NamedPropertyExpression(propertyName, propertyValue));
            }

            foreach (IEdmStructuralProperty propertyToInclude in autoSelectedProperties)
            {
                Expression propertyName  = CreatePropertyNameExpression(elementType, propertyToInclude, source);
                Expression propertyValue = CreatePropertyValueExpression(elementType, propertyToInclude, source);
                includedProperties.Add(new NamedPropertyExpression(propertyName, propertyValue)
                {
                    AutoSelected = true
                });
            }

            // create a property container that holds all these property names and values.
            return(PropertyContainer.CreatePropertyContainer(includedProperties));
        }
        public void CreatePropertyContainer_WithNullPropertyName_DoesntIncludeTheProperty()
        {
            // Arrange
            Expression propertyName = Expression.Constant(null, typeof(string));
            Expression propertyValue = Expression.Constant(new TestEntity());
            NamedPropertyExpression property = new NamedPropertyExpression(propertyName, propertyValue);
            var properties = new[] { property, property };

            // Act
            Expression containerExpression = PropertyContainer.CreatePropertyContainer(properties);

            // Assert
            PropertyContainer container = ToContainer(containerExpression);
            Assert.Empty(container.ToDictionary(includeAutoSelected: true));
        }
Ejemplo n.º 4
0
        public void CreatePropertyContainer_WithNullPropertyName_DoesntIncludeTheProperty()
        {
            // Arrange
            Expression propertyName          = Expression.Constant(null, typeof(string));
            Expression propertyValue         = Expression.Constant(new TestEntity());
            NamedPropertyExpression property = new NamedPropertyExpression(propertyName, propertyValue);
            var properties = new[] { property, property };

            // Act
            Expression containerExpression = PropertyContainer.CreatePropertyContainer(properties);

            // Assert
            PropertyContainer container = ToContainer(containerExpression);

            Assert.Empty(container.ToDictionary(new IdentityPropertyMapper(), includeAutoSelected: true));
        }
Ejemplo n.º 5
0
        private static Type GetNamedPropertyType(NamedPropertyExpression property, Expression next)
        {
            Type namedPropertyGenericType;

            if (next == null)
            {
                if (property.NullCheck != null)
                {
                    namedPropertyGenericType = typeof(SingleExpandedProperty <>);
                }
                else if (property.PageSize != null)
                {
                    namedPropertyGenericType = typeof(CollectionExpandedProperty <>);
                }
                else if (property.AutoSelected)
                {
                    namedPropertyGenericType = typeof(AutoSelectedNamedProperty <>);
                }
                else
                {
                    namedPropertyGenericType = typeof(NamedProperty <>);
                }
            }
            else
            {
                if (property.NullCheck != null)
                {
                    namedPropertyGenericType = typeof(SingleExpandedPropertyWithNext <>);
                }
                else if (property.PageSize != null)
                {
                    namedPropertyGenericType = typeof(CollectionExpandedPropertyWithNext <>);
                }
                else if (property.AutoSelected)
                {
                    namedPropertyGenericType = typeof(AutoSelectedNamedPropertyWithNext <>);
                }
                else
                {
                    namedPropertyGenericType = typeof(NamedPropertyWithNext <>);
                }
            }

            Type elementType = property.PageSize == null ? property.Value.Type : property.Value.Type.GetInnerElementType();

            return(namedPropertyGenericType.MakeGenericType(elementType));
        }
Ejemplo n.º 6
0
        public void ToDictionary_Throws_IfMappingFunctionReturns_NullOrEmpty(string mappedName)
        {
            // Arrange
            IList <NamedPropertyExpression> properties = new NamedPropertyExpression[]
            {
                new NamedPropertyExpression(name: Expression.Constant("PropA"), value: Expression.Constant(3))
            };
            Expression        containerExpression = PropertyContainer.CreatePropertyContainer(properties);
            PropertyContainer container           = ToContainer(containerExpression);

            Mock <IPropertyMapper> mapperMock = new Mock <IPropertyMapper>();

            mapperMock.Setup(m => m.MapProperty("PropA")).Returns(mappedName);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() =>
                                                      container.ToDictionary(mapperMock.Object), "The key mapping for the property 'PropA' can't be null or empty.");
        }
Ejemplo n.º 7
0
        public void ToDictionary_AppliesMappingToAllProperties()
        {
            // Arrange
            IList <NamedPropertyExpression> properties = new NamedPropertyExpression[]
            {
                new NamedPropertyExpression(name: Expression.Constant("PropA"), value: Expression.Constant(3)),
                new NamedPropertyExpression(name: Expression.Constant("PropB"), value: Expression.Constant(6))
            };
            Expression        containerExpression = PropertyContainer.CreatePropertyContainer(properties);
            PropertyContainer container           = ToContainer(containerExpression);

            Mock <IPropertyMapper> mapperMock = new Mock <IPropertyMapper>();

            mapperMock.Setup(m => m.MapProperty("PropA")).Returns("PropertyA");
            mapperMock.Setup(m => m.MapProperty("PropB")).Returns("PropB");

            //Act
            IDictionary <string, object> result = container.ToDictionary(mapperMock.Object);

            //Assert
            Assert.NotNull(result);
            Assert.True(result.ContainsKey("PropertyA"));
            Assert.True(result.ContainsKey("PropB"));
        }
Ejemplo n.º 8
0
        public void ToDictionary_Throws_IfMappingFunctionReturns_NullOrEmpty(string mappedName)
        {
            // Arrange
            IList<NamedPropertyExpression> properties = new NamedPropertyExpression[]
            {
                new NamedPropertyExpression(name: Expression.Constant("PropA"), value: Expression.Constant(3))
            };
            Expression containerExpression = PropertyContainer.CreatePropertyContainer(properties);
            PropertyContainer container = ToContainer(containerExpression);

            Mock<IPropertyMapper> mapperMock = new Mock<IPropertyMapper>();
            mapperMock.Setup(m => m.MapProperty("PropA")).Returns(mappedName);

            // Act & Assert
            Assert.Throws<InvalidOperationException>(() =>
                container.ToDictionary(mapperMock.Object), "The key mapping for the property 'PropA' can't be null or empty.");
        }
Ejemplo n.º 9
0
        public void ToDictionary_AppliesMappingToAllProperties()
        {
            // Arrange
            IList<NamedPropertyExpression> properties = new NamedPropertyExpression[]
            {
                new NamedPropertyExpression(name: Expression.Constant("PropA"), value: Expression.Constant(3)),
                new NamedPropertyExpression(name: Expression.Constant("PropB"), value: Expression.Constant(6))
            };
            Expression containerExpression = PropertyContainer.CreatePropertyContainer(properties);
            PropertyContainer container = ToContainer(containerExpression);

            Mock<IPropertyMapper> mapperMock = new Mock<IPropertyMapper>();
            mapperMock.Setup(m => m.MapProperty("PropA")).Returns("PropertyA");
            mapperMock.Setup(m => m.MapProperty("PropB")).Returns("PropB");

            //Act
            IDictionary<string, object> result = container.ToDictionary(mapperMock.Object);

            //Assert
            Assert.NotNull(result);
            Assert.True(result.ContainsKey("PropertyA"));
            Assert.True(result.ContainsKey("PropB"));
        }
Ejemplo n.º 10
0
        private Expression BuildPropertyContainer(IEdmEntityType elementType, Expression source,
            Dictionary<IEdmNavigationProperty, SelectExpandClause> propertiesToExpand,
            ISet<IEdmStructuralProperty> propertiesToInclude, ISet<IEdmStructuralProperty> autoSelectedProperties)
        {
            IList<NamedPropertyExpression> includedProperties = new List<NamedPropertyExpression>();

            foreach (KeyValuePair<IEdmNavigationProperty, SelectExpandClause> kvp in propertiesToExpand)
            {
                IEdmNavigationProperty propertyToExpand = kvp.Key;
                SelectExpandClause projection = kvp.Value;

                Expression propertyName = CreatePropertyNameExpression(elementType, propertyToExpand, source);
                Expression propertyValue = CreatePropertyValueExpression(elementType, propertyToExpand, source);
                Expression nullCheck = Expression.Equal(propertyValue, Expression.Constant(null));

                // projection can be null if the expanded navigation property is not further projected or expanded.
                if (projection != null)
                {
                    propertyValue = ProjectAsWrapper(propertyValue, projection, propertyToExpand.ToEntityType());
                }

                NamedPropertyExpression propertyExpression = new NamedPropertyExpression(propertyName, propertyValue);
                if (projection != null)
                {
                    if (!propertyToExpand.Type.IsCollection())
                    {
                        propertyExpression.NullCheck = nullCheck;
                    }
                    else if (_settings.PageSize != null)
                    {
                        propertyExpression.PageSize = _settings.PageSize.Value;
                    }
                }

                includedProperties.Add(propertyExpression);
            }

            foreach (IEdmStructuralProperty propertyToInclude in propertiesToInclude)
            {
                Expression propertyName = CreatePropertyNameExpression(elementType, propertyToInclude, source);
                Expression propertyValue = CreatePropertyValueExpression(elementType, propertyToInclude, source);
                includedProperties.Add(new NamedPropertyExpression(propertyName, propertyValue));
            }

            foreach (IEdmStructuralProperty propertyToInclude in autoSelectedProperties)
            {
                Expression propertyName = CreatePropertyNameExpression(elementType, propertyToInclude, source);
                Expression propertyValue = CreatePropertyValueExpression(elementType, propertyToInclude, source);
                includedProperties.Add(new NamedPropertyExpression(propertyName, propertyValue) { AutoSelected = true });
            }

            // create a property container that holds all these property names and values.
            return PropertyContainer.CreatePropertyContainer(includedProperties);
        }