public BindingPathConfiguration <TTargetType> HasManyPath <TTargetType>(
            Expression <Func <TStructuralType, IEnumerable <TTargetType> > > pathExpression,
            bool contained)
            where TTargetType : class
        {
            if (pathExpression == null)
            {
                throw Error.ArgumentNull("pathExpression");
            }

            PropertyInfo pathProperty = PropertySelectorVisitor.GetSelectedProperty(pathExpression);

            IList <MemberInfo> bindingPath = new List <MemberInfo>(_bindingPath);

            bindingPath.Add(pathProperty);

            StructuralTypeConfiguration <TTargetType> target;

            if (contained)
            {
                target = _modelBuilder.EntityType <TTargetType>();
                _structuralType.ContainsMany(pathExpression); // add a containment navigation property
            }
            else
            {
                target = _modelBuilder.ComplexType <TTargetType>();
                _structuralType.CollectionProperty(pathExpression); // add a collection complex property
            }

            return(new BindingPathConfiguration <TTargetType>(_modelBuilder, target, _navigationSource, bindingPath));
        }
예제 #2
0
        public void CanGetSingleSelectedProperty()
        {
            Expression <Func <AddressEntity, int> > expr = a => a.ID;
            var properties = PropertySelectorVisitor.GetSelectedProperties(expr).ToArray();

            Assert.Equal(1, properties.Length);
            Assert.Equal("ID", properties[0].Name);
        }
예제 #3
0
        public void CanGetMultipleSelectedProperties()
        {
            var expr       = Expr((AddressEntity a) => new { a.ID, a.ZipCode });
            var properties = PropertySelectorVisitor.GetSelectedProperties(expr).ToArray();

            Assert.Equal(2, properties.Length);
            Assert.Equal("ID", properties[0].Name);
            Assert.Equal("ZipCode", properties[1].Name);
        }
        public EntityTypeConfiguration <TEntityType> HasKey <TKey>(Expression <Func <TEntityType, TKey> > keyDefinitionExpression)
        {
            ICollection <PropertyInfo> properties = PropertySelectorVisitor.GetSelectedProperties(keyDefinitionExpression);

            foreach (PropertyInfo property in properties)
            {
                _configuration.HasKey(property);
            }
            return(this);
        }
예제 #5
0
        public void FailOnUnsupportedExpressionNodeType()
        {
            Expression <Func <AddressEntity, AddressEntity> > expr = (a) => CreateAddress(a.ID);
            var exception = Assert.Throws <NotSupportedException>(() =>
            {
                var properties = PropertySelectorVisitor.GetSelectedProperties(expr);
            });

            Assert.Equal("Unsupported Expression NodeType.", exception.Message);
        }
예제 #6
0
        public void FailWhenMemberExpressionNotBoundToParameter()
        {
            Expression <Func <AddressEntity, int> > expr = (a) => new AddressEntity().ID;
            var exception = Assert.Throws <InvalidOperationException>(() =>
            {
                var properties = PropertySelectorVisitor.GetSelectedProperties(expr);
            });

            Assert.Equal("MemberExpressions must be bound to the LambdaExpression parameter.", exception.Message);
        }
예제 #7
0
        public void FailWhenLambdaExpressionHasMoreThanOneParameter()
        {
            Expression <Func <AddressEntity, AddressEntity, int> > expr = (a1, a2) => a1.ID;
            var exception = Assert.Throws <InvalidOperationException>(() =>
            {
                var properties = PropertySelectorVisitor.GetSelectedProperties(expr);
            });

            Assert.Equal("The LambdaExpression must have exactly one parameter.", exception.Message);
        }
예제 #8
0
        public void FailWhenLambdaExpressionAccessesFields()
        {
            Expression <Func <WorkItem, int> > expr = w => w.Field;
            var exception = Assert.Throws <InvalidOperationException>(() =>
            {
                var properties = PropertySelectorVisitor.GetSelectedProperties(expr);
            });

            Assert.Equal(string.Format("Member '{0}.Field' is not a property.", typeof(WorkItem).FullName), exception.Message);
        }
        public BindingPathConfiguration <TTargetType> HasSinglePath <TTargetType, TDerivedType>(
            Expression <Func <TDerivedType, TTargetType> > pathExpression,
            bool required,
            bool contained)
            where TTargetType : class
            where TDerivedType : class, TStructuralType
        {
            if (pathExpression == null)
            {
                throw Error.ArgumentNull("pathExpression");
            }

            PropertyInfo pathProperty = PropertySelectorVisitor.GetSelectedProperty(pathExpression);

            IList <MemberInfo> bindingPath = new List <MemberInfo>(_bindingPath);

            bindingPath.Add(typeof(TDerivedType));
            bindingPath.Add(pathProperty);

            // make sure the derived type has the same type kind with the resource type.
            StructuralTypeConfiguration <TDerivedType> derivedConfiguration;

            if (_structuralType.Configuration.Kind == EdmTypeKind.Entity)
            {
                derivedConfiguration = _modelBuilder.EntityType <TDerivedType>().DerivesFrom <TStructuralType>();
            }
            else
            {
                derivedConfiguration = _modelBuilder.ComplexType <TDerivedType>().DerivesFrom <TStructuralType>();
            }

            StructuralTypeConfiguration <TTargetType> target;

            if (contained)
            {
                target = _modelBuilder.EntityType <TTargetType>();

                if (required)
                {
                    derivedConfiguration.ContainsRequired(pathExpression);
                }
                else
                {
                    derivedConfiguration.ContainsOptional(pathExpression);
                }
            }
            else
            {
                target = _modelBuilder.ComplexType <TTargetType>();
                derivedConfiguration.ComplexProperty(pathExpression).OptionalProperty = !required;
            }

            return(new BindingPathConfiguration <TTargetType>(_modelBuilder, target, _navigationSource, bindingPath));
        }
예제 #10
0
        private PrimitivePropertyConfiguration GetPrimitivePropertyConfiguration(Expression propertyExpression, bool optional)
        {
            PropertyInfo propertyInfo = PropertySelectorVisitor.GetSelectedProperty(propertyExpression);
            PrimitivePropertyConfiguration property = _configuration.AddProperty(propertyInfo);

            if (optional)
            {
                property.IsOptional();
            }

            return(property);
        }
예제 #11
0
        public void PassNotEnumPropertyoStructuralTypeConfigurationAddEnumPropertyShouldThrowException()
        {
            // Arrange
            var builder = new ODataModelBuilder();

            builder.ComplexType <EntityTypeWithEnumTypePropertyTestModel>();
            StructuralTypeConfiguration structuralTypeConfiguration = builder.StructuralTypes.Single();
            Expression <Func <EntityTypeWithEnumTypePropertyTestModel, int> > propertyExpression = e => e.ID;
            PropertyInfo propertyInfo = PropertySelectorVisitor.GetSelectedProperty(propertyExpression);

            // Act & Assert
            Assert.ThrowsArgument(
                () => structuralTypeConfiguration.AddEnumProperty(propertyInfo),
                "propertyInfo",
                "The property 'ID' on type 'System.Web.OData.Builder.EntityTypeWithEnumTypePropertyTestModel' must be an Enum property.");
        }
예제 #12
0
        public void PassNotExistingPropertyoStructuralTypeConfigurationAddEnumPropertyShouldThrowException()
        {
            // Arrange
            var builder = new ODataModelBuilder();

            builder.ComplexType <ComplexTypeWithEnumTypePropertyTestModel>();
            StructuralTypeConfiguration structuralTypeConfiguration = builder.StructuralTypes.Single();
            Expression <Func <EntityTypeWithEnumTypePropertyTestModel, Color> > propertyExpression = e => e.RequiredColor;
            PropertyInfo propertyInfo = PropertySelectorVisitor.GetSelectedProperty(propertyExpression);

            // Act & Assert
            Assert.ThrowsArgument(
                () => structuralTypeConfiguration.AddEnumProperty(propertyInfo),
                "propertyInfo",
                "The property 'RequiredColor' does not belong to the type 'System.Web.OData.Builder.ComplexTypeWithEnumTypePropertyTestModel'.");
        }
예제 #13
0
        private ComplexPropertyConfiguration GetComplexPropertyConfiguration(Expression propertyExpression, bool optional = false)
        {
            PropertyInfo propertyInfo             = PropertySelectorVisitor.GetSelectedProperty(propertyExpression);
            ComplexPropertyConfiguration property = _configuration.AddComplexProperty(propertyInfo);

            if (optional)
            {
                property.IsOptional();
            }
            else
            {
                property.IsRequired();
            }

            return(property);
        }
        public BindingPathConfiguration <TTargetType> HasSinglePath <TTargetType>(
            Expression <Func <TStructuralType, TTargetType> > pathExpression,
            bool required,
            bool contained)
            where TTargetType : class
        {
            if (pathExpression == null)
            {
                throw Error.ArgumentNull("pathExpression");
            }

            PropertyInfo pathProperty = PropertySelectorVisitor.GetSelectedProperty(pathExpression);

            IList <MemberInfo> bindingPath = new List <MemberInfo>(_bindingPath);

            bindingPath.Add(pathProperty);

            StructuralTypeConfiguration <TTargetType> target;

            if (contained)
            {
                target = _modelBuilder.EntityType <TTargetType>();

                if (required)
                {
                    _structuralType.ContainsRequired(pathExpression);
                }
                else
                {
                    _structuralType.ContainsOptional(pathExpression);
                }
            }
            else
            {
                target = _modelBuilder.ComplexType <TTargetType>();
                _structuralType.ComplexProperty(pathExpression).OptionalProperty = !required;
            }

            return(new BindingPathConfiguration <TTargetType>(_modelBuilder, target, _navigationSource, bindingPath));
        }
        internal NavigationPropertyConfiguration GetOrCreateContainedNavigationProperty(Expression navigationPropertyExpression, EdmMultiplicity multiplicity)
        {
            PropertyInfo navigationProperty = PropertySelectorVisitor.GetSelectedProperty(navigationPropertyExpression);

            return(_configuration.AddContainedNavigationProperty(navigationProperty, multiplicity));
        }
예제 #16
0
        public virtual void Ignore <TProperty>(Expression <Func <TStructuralType, TProperty> > propertyExpression)
        {
            PropertyInfo ignoredProperty = PropertySelectorVisitor.GetSelectedProperty(propertyExpression);

            _configuration.RemoveProperty(ignoredProperty);
        }
예제 #17
0
        public void HasDynamicProperties(Expression <Func <TStructuralType, IDictionary <string, object> > > propertyExpression)
        {
            PropertyInfo propertyInfo = PropertySelectorVisitor.GetSelectedProperty(propertyExpression);

            _configuration.AddDynamicPropertyDictionary(propertyInfo);
        }