public StructuralTypeConfigurationTest()
 {
     Mock<StructuralTypeConfiguration> mockConfiguration = new Mock<StructuralTypeConfiguration> { CallBase = true };
     mockConfiguration.Object.Name = "Name";
     mockConfiguration.Object.Namespace = "Namespace";
     _configuration = mockConfiguration.Object;
 }
 /// <summary>
 /// Constructs a CollectionPropertyConfiguration using the <paramref name="property">property</paramref> provided.
 /// </summary>
 public CollectionPropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType)
     : base(property, declaringType)
 {
     if (!property.PropertyType.IsCollection(out _elementType))
     {
         throw Error.Argument("property", SRResources.CollectionPropertiesMustReturnIEnumerable, property.Name, property.DeclaringType.FullName);
     }
 }
        public StructuralTypeConfigurationOfTStructuralTypeTest()
        {
            Mock<StructuralTypeConfiguration> mockConfig = new Mock<StructuralTypeConfiguration> { CallBase = true };
            mockConfig.Object.Name = "Name";
            mockConfig.Object.Namespace = "Namespace";

            Mock<StructuralTypeConfiguration<object>> mockGenericConfig =
                new Mock<StructuralTypeConfiguration<object>>(mockConfig.Object) { CallBase = true };
            _configuration = mockGenericConfig.Object;
        }
 public PropertyConfigurationTest()
 {
     Mock<PropertyInfo> mockPropertyInfo = new Mock<PropertyInfo>();
     _propertyInfo = mockPropertyInfo.Object;
     Mock<StructuralTypeConfiguration> mockTypeConfig = new Mock<StructuralTypeConfiguration>();
     _declaringType = mockTypeConfig.Object;
     Mock<PropertyConfiguration> mockConfiguration =
         new Mock<PropertyConfiguration>(_propertyInfo, _declaringType) { CallBase = true };
     mockConfiguration.Object.Name = "Name";
     _configuration = mockConfiguration.Object;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyConfiguration"/> class.
        /// </summary>
        /// <param name="property">The name of the property.</param>
        /// <param name="declaringType">The declaring EDM type of the property.</param>
        protected PropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType)
        {
            if (property == null)
            {
                throw Error.ArgumentNull("property");
            }

            if (declaringType == null)
            {
                throw Error.ArgumentNull("declaringType");
            }

            PropertyInfo = property;
            DeclaringType = declaringType;
            AddedExplicitly = true;
            _name = property.Name;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="PrimitivePropertyConfiguration"/> class.
 /// </summary>
 /// <param name="property">The name of the property.</param>
 /// <param name="declaringType">The declaring EDM type of the property.</param>
 public PrimitivePropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType)
     : base(property, declaringType)
 {
 }
        private IEdmProperty CreateStructuralTypeEnumPropertyBody(EdmStructuredType type, StructuralTypeConfiguration config, EnumPropertyConfiguration enumProperty)
        {
            Type enumPropertyType = TypeHelper.GetUnderlyingTypeOrSelf(enumProperty.RelatedClrType);
            IEdmType edmType = GetEdmType(enumPropertyType);

            if (edmType == null)
            {
                throw Error.InvalidOperation(SRResources.EnumTypeDoesNotExist, enumPropertyType.Name);
            }

            IEdmEnumType enumType = (IEdmEnumType)edmType;
            IEdmTypeReference enumTypeReference = new EdmEnumTypeReference(enumType, enumProperty.OptionalProperty);

            // Set concurrency token if is entity type, and concurrency token is true
            EdmConcurrencyMode enumConcurrencyMode = EdmConcurrencyMode.None;
            if (config.Kind == EdmTypeKind.Entity && enumProperty.ConcurrencyToken)
            {
                enumConcurrencyMode = EdmConcurrencyMode.Fixed;
            }

            return type.AddStructuralProperty(
                enumProperty.Name,
                enumTypeReference,
                defaultValue: null,
                concurrencyMode: enumConcurrencyMode);
        }
        private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config)
        {
            foreach (PropertyConfiguration property in config.Properties)
            {
                IEdmProperty edmProperty = null;

                switch (property.Kind)
                {
                    case PropertyKind.Primitive:
                        PrimitivePropertyConfiguration primitiveProperty = (PrimitivePropertyConfiguration)property;
                        EdmPrimitiveTypeKind typeKind = primitiveProperty.TargetEdmTypeKind ??
                                                        GetTypeKind(primitiveProperty.PropertyInfo.PropertyType);
                        IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive(
                            typeKind,
                            primitiveProperty.OptionalProperty);

                        // Set concurrency token if is entity type, and concurrency token is true
                        EdmConcurrencyMode concurrencyMode = EdmConcurrencyMode.None;
                        if (config.Kind == EdmTypeKind.Entity && primitiveProperty.ConcurrencyToken)
                        {
                            concurrencyMode = EdmConcurrencyMode.Fixed;
                        }
                        edmProperty = type.AddStructuralProperty(
                            primitiveProperty.Name,
                            primitiveTypeReference,
                            defaultValue: null,
                            concurrencyMode: concurrencyMode);
                        break;

                    case PropertyKind.Complex:
                        ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration;
                        IEdmComplexType complexType = GetEdmType(complexProperty.RelatedClrType) as IEdmComplexType;

                        edmProperty = type.AddStructuralProperty(
                            complexProperty.Name,
                            new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty));
                        break;

                    case PropertyKind.Collection:
                        edmProperty = CreateStructuralTypeCollectionPropertyBody(type, (CollectionPropertyConfiguration)property);
                        break;

                    case PropertyKind.Enum:
                        edmProperty = CreateStructuralTypeEnumPropertyBody(type, config, (EnumPropertyConfiguration)property);
                        break;

                    default:
                        break;
                }

                if (edmProperty != null)
                {
                    if (property.PropertyInfo != null)
                    {
                        _properties[property.PropertyInfo] = edmProperty;
                    }

                    if (property.IsRestricted)
                    {
                        _propertiesRestrictions[edmProperty] = new QueryableRestrictions(property);
                    }
                }
            }
        }
        public static bool IsAssignableFrom(this StructuralTypeConfiguration baseStructuralType,
            StructuralTypeConfiguration structuralType)
        {
            if (structuralType.Kind == EdmTypeKind.Entity && baseStructuralType.Kind == EdmTypeKind.Entity)
            {
                EntityTypeConfiguration entity = (EntityTypeConfiguration)structuralType;
                while (entity != null)
                {
                    if (baseStructuralType == entity)
                    {
                        return true;
                    }

                    entity = entity.BaseType;
                }
            }
            else if (structuralType.Kind == EdmTypeKind.Complex && baseStructuralType.Kind == EdmTypeKind.Complex)
            {
                ComplexTypeConfiguration complex = (ComplexTypeConfiguration)structuralType;
                while (complex != null)
                {
                    if (baseStructuralType == complex)
                    {
                        return true;
                    }

                    complex = complex.BaseType;
                }
            }

            return false;
        }
        // Returns all the derived types of this type.
        public static IEnumerable<StructuralTypeConfiguration> DerivedTypes(this ODataModelBuilder modelBuilder,
            StructuralTypeConfiguration structuralType)
        {
            if (modelBuilder == null)
            {
                throw Error.ArgumentNull("modelBuilder");
            }

            if (structuralType == null)
            {
                throw Error.ArgumentNull("structuralType");
            }

            if (structuralType.Kind == EdmTypeKind.Entity)
            {
                return DerivedTypes(modelBuilder, (EntityTypeConfiguration)structuralType);
            }

            if (structuralType.Kind == EdmTypeKind.Complex)
            {
                return DerivedTypes(modelBuilder, (ComplexTypeConfiguration)structuralType);
            }

            return Enumerable.Empty<StructuralTypeConfiguration>();
        }
        // Returns the base types, this type and all the derived types of this type.
        public static IEnumerable<StructuralTypeConfiguration> ThisAndBaseAndDerivedTypes(
            this ODataModelBuilder modelBuilder, StructuralTypeConfiguration structuralType)
        {
            Contract.Assert(modelBuilder != null);
            Contract.Assert(structuralType != null);

            return structuralType.BaseTypes()
                    .Concat(new[] { structuralType })
                    .Concat(modelBuilder.DerivedTypes(structuralType));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="StructuralPropertyConfiguration"/> class.
 /// </summary>
 /// <param name="property">The property of the configuration.</param>
 /// <param name="declaringType">The declaring type of the property.</param>
 protected StructuralPropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType)
     : base(property, declaringType)
 {
     OptionalProperty = EdmLibHelpers.IsNullable(property.PropertyType);
 }
        private static PropertyConfiguration MockProperty(string name, StructuralTypeConfiguration declaringType)
        {
            Mock<PropertyInfo> propertyInfo = new Mock<PropertyInfo>();
            propertyInfo.Setup(p => p.Name).Returns(name);

            Mock<PropertyConfiguration> property = new Mock<PropertyConfiguration>(propertyInfo.Object, declaringType);
            return property.Object;
        }
 /// <summary>
 /// Instantiates a new instance of the <see cref="ComplexPropertyConfiguration"/> class.
 /// </summary>
 /// <param name="property">The property of the configuration.</param>
 /// <param name="declaringType">The declaring type of the property.</param>
 public ComplexPropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType)
     : base(property, declaringType)
 {
 }
        internal virtual void DerivesFromImpl(StructuralTypeConfiguration baseType)
        {
            if (baseType == null)
            {
                throw Error.ArgumentNull("baseType");
            }

            _baseType = baseType;
            _baseTypeConfigured = true;

            if (!baseType.ClrType.IsAssignableFrom(ClrType) || baseType.ClrType == ClrType)
            {
                throw Error.Argument("baseType", SRResources.TypeDoesNotInheritFromBaseType,
                    ClrType.FullName, baseType.ClrType.FullName);
            }

            foreach (PropertyConfiguration property in Properties)
            {
                ValidatePropertyNotAlreadyDefinedInBaseTypes(property.PropertyInfo);
            }

            foreach (PropertyConfiguration property in this.DerivedProperties())
            {
                ValidatePropertyNotAlreadyDefinedInDerivedTypes(property.PropertyInfo);
            }
        }
 internal virtual void DerivesFromNothingImpl()
 {
     _baseType = null;
     _baseTypeConfigured = true;
 }