Ejemplo n.º 1
0
        public void AddToEdmType(EdmStructuredType edmType, IEnumerable <Language> languages, string schemaName, Func <EdmComplexType, EdmComplexType> typeResolver)
        {
            Guard.NotNull(edmType, nameof(edmType));
            Guard.NotNull(languages, nameof(languages));
            Guard.NotNull(typeResolver, nameof(typeResolver));

            if (!RawProperties.IsLocalizable)
            {
                languages = new[] { Language.Invariant };
            }

            var edmValueType = CreateEdmType();

            if (edmValueType == null)
            {
                return;
            }

            var languageType = typeResolver(new EdmComplexType("Squidex", $"{schemaName}{Name.ToPascalCase()}Property"));

            foreach (var language in languages)
            {
                languageType.AddStructuralProperty(language.Iso2Code, edmValueType);
            }

            edmType.AddStructuralProperty(Name, new EdmComplexTypeReference(languageType, false));
        }
Ejemplo n.º 2
0
 private void AddPropertiesToEdmType(EdmStructuredType entityType)
 {
     foreach (var property in this.properties.Values)
     {
         entityType.AddStructuralProperty(property.EdmPropertyName, property.DataType.GetEdmTypeReference(property.CanBeNull));
     }
 }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Finds property on structural type by its name.
        /// </summary>
        /// <param name="structuralType">The structural type to search for the property.</param>
        /// <param name="propertyName">The name of the property to look for.</param>
        /// <returns>The member property found, or null if no such property exists.</returns>
        /// <remarks>The method will fail if there's more than one matching property.</remarks>
        public static IEdmProperty GetProperty(this EdmStructuredType structuralType, string propertyName)
        {
            ExceptionUtilities.CheckArgumentNotNull(structuralType, "structuralType");
            ExceptionUtilities.CheckArgumentNotNull(propertyName, "propertyName");

            return(structuralType.FindProperty(propertyName));
        }
Ejemplo n.º 5
0
        private void AddComponents(EdmModel model, EdmStructuredType entity, IEnumerable <ComponentInfo> components)
        {
            foreach (ComponentInfo componentInfo in components)
            {
                var            edmSchemaElement = ((IEdmSchemaElement)entity);
                EdmComplexType complexType      = new EdmComplexType(edmSchemaElement.Namespace, componentInfo.Name);

                foreach (var databaseColumn in componentInfo.Columns)
                {
                    AddPropertyToEntity(complexType, databaseColumn);
                }

                //// add created type
                model.AddElement(complexType);

                if (componentInfo.ChildComponents != null && componentInfo.ChildComponents.Any())
                {
                    //// we are passing here acutaly created complexType because it can be nested
                    AddComponents(model, complexType, componentInfo.ChildComponents);
                }

                //// adding referenece to actual entity
                entity.AddStructuralProperty(componentInfo.Name.Remove(0, (edmSchemaElement.Name + _separator).Length), new EdmComplexTypeReference(complexType, false));
            }
        }
Ejemplo n.º 6
0
        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);

                    edmProperty = type.AddStructuralProperty(
                        primitiveProperty.Name,
                        primitiveTypeReference,
                        defaultValue: null);
                    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, (EnumPropertyConfiguration)property);
                    break;

                default:
                    break;
                }

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

                    if (property.IsRestricted)
                    {
                        _propertiesRestrictions[edmProperty] = new QueryableRestrictions(property);
                    }
                }
            }
        }
Ejemplo n.º 7
0
        private static IEdmStructuralProperty AddPropertyToEntity(EdmStructuredType entity, DatabaseColumn column)
        {
            var typeKind = EdmPrimitiveTypeKind.String;
            var typeMap  = BuildEdmTypeMap();

            if (typeMap.ContainsKey(column.DataType))
            {
                typeKind = typeMap[column.DataType];
            }

            //// because of posibility of changing column table nullability from which view is generated we always set nullable to TRUE. Then it is prepared for null value.
            return(entity.AddStructuralProperty(column.Name, typeKind, true));
        }
Ejemplo n.º 8
0
        private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config)
        {
            foreach (PropertyConfiguration property in config.Properties)
            {
                IEdmProperty edmProperty = null;

                switch (property.Kind)
                {
                case PropertyKind.Primitive:
                    PrimitivePropertyConfiguration primitiveProperty = property as PrimitivePropertyConfiguration;
                    CreatePrimitiveProperty(primitiveProperty, type, config, out edmProperty);
                    break;

                case PropertyKind.Complex:
                    ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration;
                    IEdmComplexType complexType = _types[complexProperty.RelatedClrType] as IEdmComplexType;

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

                case PropertyKind.Collection:
                    CollectionPropertyConfiguration collectionProperty = property as CollectionPropertyConfiguration;
                    IEdmTypeReference elementTypeReference             = null;
                    if (_types.ContainsKey(collectionProperty.ElementType))
                    {
                        IEdmComplexType elementType = _types[collectionProperty.ElementType] as IEdmComplexType;
                        elementTypeReference = new EdmComplexTypeReference(elementType, false);
                    }
                    else
                    {
                        elementTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType);
                    }
                    edmProperty = type.AddStructuralProperty(
                        collectionProperty.PropertyInfo.Name,
                        new EdmCollectionTypeReference(
                            new EdmCollectionType(elementTypeReference),
                            collectionProperty.OptionalProperty));
                    break;

                default:
                    break;
                }

                if (edmProperty != null && property.PropertyInfo != null)
                {
                    _properties[property.PropertyInfo] = edmProperty;
                }
            }
        }
        private void CreateNavigationProperty(StructuralTypeConfiguration config)
        {
            Contract.Assert(config != null);

            EdmStructuredType type = (EdmStructuredType)(this.GetEdmType(config.ClrType));

            foreach (NavigationPropertyConfiguration navProp in config.NavigationProperties)
            {
                EdmNavigationPropertyInfo info = new EdmNavigationPropertyInfo
                {
                    Name = navProp.Name,
                    TargetMultiplicity = navProp.Multiplicity,
                    Target             = this.GetEdmType(navProp.RelatedClrType) as IEdmEntityType,
                    ContainsTarget     = navProp.ContainsTarget,
                    OnDelete           = navProp.OnDeleteAction
                };

                // Principal properties
                if (navProp.PrincipalProperties.Any())
                {
                    info.PrincipalProperties = this.GetDeclaringPropertyInfo(navProp.PrincipalProperties);
                }

                // Dependent properties
                if (navProp.DependentProperties.Any())
                {
                    info.DependentProperties = this.GetDeclaringPropertyInfo(navProp.DependentProperties);
                }

                IEdmProperty edmProperty = type.AddUnidirectionalNavigation(info);
                if (navProp.PropertyInfo != null && edmProperty != null)
                {
                    this._properties[navProp.PropertyInfo] = edmProperty;
                }

                if (edmProperty != null)
                {
                    if (navProp.IsRestricted)
                    {
                        this._propertiesRestrictions[edmProperty] = new QueryableRestrictions(navProp);
                    }

                    if (navProp.QueryConfiguration.ModelBoundQuerySettings != null)
                    {
                        this._propertiesQuerySettings.Add(edmProperty, navProp.QueryConfiguration.ModelBoundQuerySettings);
                    }
                }
            }
        }
Ejemplo n.º 10
0
        private IEdmProperty CreateStructuralTypeCollectionPropertyBody(EdmStructuredType type, CollectionPropertyConfiguration collectionProperty)
        {
            IEdmTypeReference elementTypeReference = null;
            Type clrType = TypeHelper.GetUnderlyingTypeOrSelf(collectionProperty.ElementType);

            if (clrType.GetTypeInfo().IsEnum)
            {
                IEdmType edmType = GetEdmType(clrType);

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

                IEdmEnumType enumElementType = (IEdmEnumType)edmType;
                bool         isNullable      = collectionProperty.ElementType != clrType;
                elementTypeReference = new EdmEnumTypeReference(enumElementType, isNullable);
            }
            else
            {
                IEdmType edmType = GetEdmType(collectionProperty.ElementType);
                if (edmType != null)
                {
                    IEdmComplexType elementType = edmType as IEdmComplexType;
                    // Work around for primitive types (ex: Int32 would be typed to System.Int32 instead of EdmInt)
                    if (elementType != null)
                    {
                        elementTypeReference = new EdmComplexTypeReference(elementType, collectionProperty.OptionalProperty);
                    }
                    else
                    {
                        elementTypeReference =
                            EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType);
                    }
                }
                else
                {
                    elementTypeReference =
                        EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType);
                }

                Contract.Assert(elementTypeReference != null);
            }

            return(type.AddStructuralProperty(
                       collectionProperty.Name,
                       new EdmCollectionTypeReference(new EdmCollectionType(elementTypeReference))));
        }
Ejemplo n.º 11
0
        private void AddProperty(EdmStructuredType edmType, RdmProperty rdmProp)
        {
            var         edmTypeRef = env.ResolveTypeReference(rdmProp.Type);
            EdmProperty edmProp;

            // collection navigation property
            if (edmTypeRef is IEdmCollectionTypeReference collRef &&
                collRef.Definition is IEdmCollectionType collType &&
                collType.ElementType is IEdmEntityTypeReference elTypeRef &&
                elTypeRef.Definition is IEdmEntityType elType)
            {
                var info = new EdmNavigationPropertyInfo {
                    Name = rdmProp.Name, Target = elType, TargetMultiplicity = EdmMultiplicity.Many
                };
                edmProp = edmType.AddUnidirectionalNavigation(info);
            }
Ejemplo n.º 12
0
        private IEdmProperty CreateStructuralTypeEnumPropertyBody(EdmStructuredType type, 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.NullableProperty);

            return(type.AddStructuralProperty(
                       enumProperty.Name,
                       enumTypeReference,
                       defaultValue: enumProperty.DefaultValueString));
        }
Ejemplo n.º 13
0
        private void CreateStructuralTypeBody(EdmStructuredType type, IStructuralTypeConfiguration config)
        {
            foreach (PrimitivePropertyConfiguration prop in config.Properties.OfType <PrimitivePropertyConfiguration>())
            {
                type.AddStructuralProperty(
                    prop.PropertyInfo.Name,
                    GetTypeKind(prop.PropertyInfo.PropertyType),
                    prop.OptionalProperty);
            }
            foreach (ComplexPropertyConfiguration prop in config.Properties.OfType <ComplexPropertyConfiguration>())
            {
                IEdmComplexType complexType = _types[prop.RelatedClrType.FullName] as IEdmComplexType;

                type.AddStructuralProperty(
                    prop.PropertyInfo.Name,
                    new EdmComplexTypeReference(complexType, prop.OptionalProperty));
            }
        }
Ejemplo n.º 14
0
        private IEdmProperty CreateStructuralTypeCollectionPropertyBody(EdmStructuredType type, CollectionPropertyConfiguration collectionProperty)
        {
            IEdmTypeReference elementTypeReference;
            Type clrType = TypeHelper.GetUnderlyingTypeOrSelf(collectionProperty.ElementType);

            if (clrType == typeof(object))
            {
                elementTypeReference = EdmCoreModel.Instance.GetUntyped();
            }
            else if (TypeHelper.IsEnum(clrType))
            {
                IEdmType edmType = GetEdmType(clrType);

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

                IEdmEnumType enumElementType = (IEdmEnumType)edmType;
                bool         isNullable      = collectionProperty.ElementType != clrType;
                elementTypeReference = new EdmEnumTypeReference(enumElementType, isNullable);
            }
            else
            {
                IEdmType edmType = GetEdmType(collectionProperty.ElementType);
                if (edmType != null)
                {
                    IEdmComplexType elementType = edmType as IEdmComplexType;
                    Contract.Assert(elementType != null);
                    elementTypeReference = new EdmComplexTypeReference(elementType, collectionProperty.NullableProperty);
                }
                else
                {
                    elementTypeReference =
                        EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType);
                    Contract.Assert(elementTypeReference != null);
                }
            }

            return(type.AddStructuralProperty(
                       collectionProperty.Name,
                       new EdmCollectionTypeReference(new EdmCollectionType(elementTypeReference))));
        }
Ejemplo n.º 15
0
        public void AddToEdmType(EdmStructuredType edmType, PartitionResolver partitionResolver, string schemaName, Func <EdmComplexType, EdmComplexType> typeResolver)
        {
            Guard.NotNull(edmType, nameof(edmType));
            Guard.NotNull(typeResolver, nameof(typeResolver));
            Guard.NotNull(partitionResolver, nameof(partitionResolver));

            var edmValueType = CreateEdmType();

            if (edmValueType == null)
            {
                return;
            }

            var partitionType = typeResolver(new EdmComplexType("Squidex", $"{schemaName}{Name.ToPascalCase()}Property"));
            var partition     = partitionResolver(partitioning);

            foreach (var partitionItem in partition)
            {
                partitionType.AddStructuralProperty(partitionItem.Key, edmValueType);
            }

            edmType.AddStructuralProperty(Name.EscapeEdmField(), new EdmComplexTypeReference(partitionType, false));
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Implement the creation of an EDM Entity Type or EDM Complex Type.
 /// </summary>
 /// <param name="clrType">The CLR type of the new schema element to create.</param>
 /// <param name="context">The query context.</param>
 /// <param name="entryEdmType">The new Schema element.</param>
 private static void CreateSchemaType(Type clrType, ODataQueryContext context, EdmStructuredType entryEdmType)
 {
     foreach (var pi in clrType.GetProperties())
     {
         if (pi.Name == "ComparerInstance")
         {
             continue;
         }
         if (pi.PropertyType.IsPrimitive || pi.PropertyType.FullName == "System.String")
         {
             entryEdmType.AddStructuralProperty(pi.Name, GetPrimitiveTypeKind(pi.PropertyType), true);
         }
         else if (pi.PropertyType.IsEnum)
         {
             var enumType = GetEnumTypeKind(pi.PropertyType, context);
             if (enumType != null)
             {
                 entryEdmType.AddStructuralProperty(pi.Name, enumType);
             }
             else
             {
                 entryEdmType.AddStructuralProperty(pi.Name, EdmPrimitiveTypeKind.Int32);
             }
         }
         else
         {
             var propEdmType = context.Model.FindDeclaredType(pi.PropertyType.FullName);
             if (propEdmType != null)
             {
                 entryEdmType.AddStructuralProperty(pi.Name, propEdmType.ToEdmTypeReference(true));
             }
             else
             {
                 //var t = Type.GetType(pi.PropertyType.FullName);
                 var t       = pi.PropertyType;
                 var edmType = context.Model.GetEdmTypeReference(t);
                 if (edmType != null)
                 {
                     entryEdmType.AddStructuralProperty(pi.Name, edmType);
                 }
                 else
                 {
                     throw new InvalidOperationException(string.Format("Could not find EDM type of {0}", pi.PropertyType.FullName));
                 }
             }
         }
     }
 }
Ejemplo n.º 17
0
        private IEdmProperty CreateProperty(EdmStructuredType declaringType, ResourceProperty resourceProperty)
        {
            IEdmProperty property;
            List <KeyValuePair <string, object> > annotations = (resourceProperty.CustomAnnotations == null) ? null : resourceProperty.CustomAnnotations.ToList <KeyValuePair <string, object> >();
            ODataNullValueBehaviorKind            nullValueReadBehaviorKind = ODataNullValueBehaviorKind.Default;

            if (resourceProperty.IsOfKind(ResourcePropertyKind.Primitive) || resourceProperty.IsOfKind(ResourcePropertyKind.Stream))
            {
                IEdmPrimitiveTypeReference typeReference = MetadataProviderUtils.CreatePrimitiveTypeReference(resourceProperty.ResourceType, annotations);
                if (resourceProperty.IsOfKind(ResourcePropertyKind.Key))
                {
                    if (typeReference.IsNullable)
                    {
                        typeReference = (IEdmPrimitiveTypeReference)typeReference.Clone(false);
                    }
                    nullValueReadBehaviorKind = ODataNullValueBehaviorKind.IgnoreValue;
                }
                else if (MetadataProviderUtils.ShouldDisablePrimitivePropertyNullValidation(resourceProperty, typeReference))
                {
                    nullValueReadBehaviorKind = ODataNullValueBehaviorKind.DisableValidation;
                }
                string             andRemoveDefaultValue = MetadataProviderUtils.GetAndRemoveDefaultValue(annotations);
                EdmConcurrencyMode concurrencyMode       = resourceProperty.IsOfKind(ResourcePropertyKind.ETag) ? EdmConcurrencyMode.Fixed : EdmConcurrencyMode.None;
                property = declaringType.AddStructuralProperty(resourceProperty.Name, typeReference, andRemoveDefaultValue, concurrencyMode);
                string mimeType = resourceProperty.MimeType;
                if (!string.IsNullOrEmpty(mimeType))
                {
                    this.SetMimeType(property, mimeType);
                }
            }
            else if (resourceProperty.IsOfKind(ResourcePropertyKind.ComplexType))
            {
                IEdmTypeReference reference2   = this.EnsureTypeReference(resourceProperty.ResourceType, annotations);
                string            defaultValue = MetadataProviderUtils.GetAndRemoveDefaultValue(annotations);
                property = declaringType.AddStructuralProperty(resourceProperty.Name, reference2, defaultValue, EdmConcurrencyMode.None);
                if (this.metadataProvider.IsV1Provider && !reference2.IsNullable)
                {
                    nullValueReadBehaviorKind = ODataNullValueBehaviorKind.DisableValidation;
                }
            }
            else if (resourceProperty.IsOfKind(ResourcePropertyKind.Collection))
            {
                string            str4       = MetadataProviderUtils.GetAndRemoveDefaultValue(annotations);
                IEdmTypeReference reference3 = this.EnsureTypeReference(resourceProperty.ResourceType, annotations);
                property = declaringType.AddStructuralProperty(resourceProperty.Name, reference3, str4, EdmConcurrencyMode.None);
            }
            else
            {
                if (!resourceProperty.IsOfKind(ResourcePropertyKind.ResourceSetReference) && !resourceProperty.IsOfKind(ResourcePropertyKind.ResourceReference))
                {
                    throw new InvalidOperationException(System.Data.Services.Strings.MetadataProviderEdmModel_UnsupportedResourcePropertyKind(resourceProperty.Kind.ToString()));
                }
                EdmEntityType     type       = (EdmEntityType)declaringType;
                IEdmTypeReference reference4 = resourceProperty.IsOfKind(ResourcePropertyKind.ResourceSetReference) ? this.EnsureEntityPrimitiveOrComplexCollectionTypeReference(resourceProperty.ResourceType, annotations) : this.EnsureTypeReference(resourceProperty.ResourceType, annotations);
                property = new MetadataProviderEdmNavigationProperty(type, resourceProperty.Name, reference4);
                type.AddProperty(property);
            }
            this.SetNullValueReaderBehavior(property, nullValueReadBehaviorKind);
            MetadataProviderUtils.ConvertCustomAnnotations(this, annotations, property);
            return(property);
        }
Ejemplo n.º 18
0
        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.NullableProperty);

                    if (typeKind == EdmPrimitiveTypeKind.Decimal)
                    {
                        DecimalPropertyConfiguration decimalProperty =
                            primitiveProperty as DecimalPropertyConfiguration;
                        if (decimalProperty.Precision.HasValue || decimalProperty.Scale.HasValue)
                        {
                            primitiveTypeReference = new EdmDecimalTypeReference(
                                (IEdmPrimitiveType)primitiveTypeReference.Definition,
                                primitiveTypeReference.IsNullable,
                                decimalProperty.Precision,
                                decimalProperty.Scale.HasValue ? decimalProperty.Scale : 0);
                        }
                    }
                    else if (EdmLibHelpers.HasPrecision(typeKind))
                    {
                        PrecisionPropertyConfiguration precisionProperty =
                            primitiveProperty as PrecisionPropertyConfiguration;
                        primitiveTypeReference = AddPrecisionConfigInPrimitiveTypeReference(
                            precisionProperty,
                            primitiveTypeReference);
                    }
                    else if (EdmLibHelpers.HasLength(typeKind))
                    {
                        LengthPropertyConfiguration lengthProperty =
                            primitiveProperty as LengthPropertyConfiguration;
                        primitiveTypeReference = AddLengthConfigInPrimitiveTypeReference(
                            lengthProperty,
                            primitiveTypeReference);
                    }
                    edmProperty = type.AddStructuralProperty(
                        primitiveProperty.Name,
                        primitiveTypeReference,
                        defaultValue: primitiveProperty.DefaultValueString);
                    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.NullableProperty));
                    break;

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

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

                default:
                    break;
                }

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

                    if (property.IsRestricted)
                    {
                        _propertiesRestrictions[edmProperty] = new QueryableRestrictions(property);
                    }

                    //if (property.QueryConfiguration.ModelBoundQuerySettings != null)
                    //{
                    //    _propertiesQuerySettings.Add(edmProperty, property.QueryConfiguration.ModelBoundQuerySettings);
                    //}
                }
            }
        }
Ejemplo n.º 19
0
        private void CreateNavigationProperty(StructuralTypeConfiguration config)
        {
            Contract.Assert(config != null);

            EdmStructuredType type = (EdmStructuredType)(GetEdmType(config.ClrType));

            foreach (NavigationPropertyConfiguration navProp in config.NavigationProperties)
            {
                Func <NavigationPropertyConfiguration, EdmNavigationPropertyInfo> getInfo = nav =>
                {
                    EdmNavigationPropertyInfo info = new EdmNavigationPropertyInfo
                    {
                        Name = nav.Name,
                        TargetMultiplicity = nav.Multiplicity,
                        Target             = GetEdmType(nav.RelatedClrType) as IEdmEntityType,
                        ContainsTarget     = nav.ContainsTarget,
                        OnDelete           = nav.OnDeleteAction
                    };

                    // Principal properties
                    if (nav.PrincipalProperties.Any())
                    {
                        info.PrincipalProperties = GetDeclaringPropertyInfo(nav.PrincipalProperties);
                    }

                    // Dependent properties
                    if (nav.DependentProperties.Any())
                    {
                        info.DependentProperties = GetDeclaringPropertyInfo(nav.DependentProperties);
                    }

                    return(info);
                };

                var           navInfo    = getInfo(navProp);
                var           props      = new Dictionary <IEdmProperty, NavigationPropertyConfiguration>();
                EdmEntityType entityType = type as EdmEntityType;
                if (entityType != null && navProp.Partner != null)
                {
                    var edmProperty        = entityType.AddBidirectionalNavigation(navInfo, getInfo(navProp.Partner));
                    var partnerEdmProperty = (navInfo.Target as EdmEntityType).Properties().Single(p => p.Name == navProp.Partner.Name);
                    props.Add(edmProperty, navProp);
                    props.Add(partnerEdmProperty, navProp.Partner);
                }
                else
                {
                    // Do not add this if we have have a partner relationship configured, as this
                    // property will be added automatically through the AddBidirectionalNavigation
                    var targetConfig = config.ModelBuilder.GetTypeConfigurationOrNull(navProp.RelatedClrType) as StructuralTypeConfiguration;
                    if (!targetConfig.NavigationProperties.Any(p => p.Partner != null && p.Partner.Name == navInfo.Name))
                    {
                        var edmProperty = type.AddUnidirectionalNavigation(navInfo);
                        props.Add(edmProperty, navProp);
                    }
                }

                foreach (var item in props)
                {
                    var edmProperty = item.Key;
                    var prop        = item.Value;
                    if (prop.PropertyInfo != null)
                    {
                        _properties[prop.PropertyInfo] = edmProperty;
                    }


                    if (prop.IsRestricted)
                    {
                        _propertiesRestrictions[edmProperty] = new QueryableRestrictions(prop);
                    }

                    /*
                     *                 if (prop.QueryConfiguration.ModelBoundQuerySettings != null)
                     *                 {
                     *                     _propertiesQuerySettings.Add(edmProperty, prop.QueryConfiguration.ModelBoundQuerySettings);
                     *                 }*/
                }
            }
        }
Ejemplo n.º 20
0
        private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config)
        {
            foreach (PropertyConfiguration property in config.Properties)
            {
                IEdmProperty edmProperty = null;

                switch (property.Kind)
                {
                case PropertyKind.Primitive:
                    PrimitivePropertyConfiguration primitiveProperty = property as PrimitivePropertyConfiguration;
                    if (primitiveProperty.IsIgnored)
                    {
                        continue;
                    }
                    EdmPrimitiveTypeKind typeKind = 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);
                    }
                }
            }
        }
Ejemplo n.º 21
0
        private void CreatePrimitiveProperty(PrimitivePropertyConfiguration primitiveProperty, EdmStructuredType type, StructuralTypeConfiguration config, out IEdmProperty edmProperty)
        {
            EdmPrimitiveTypeKind typeKind = 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;
            }

            var primitiveProp = new EdmStructuralProperty(
                type,
                primitiveProperty.PropertyInfo.Name,
                primitiveTypeReference,
                defaultValueString: null,
                concurrencyMode: concurrencyMode);

            type.AddProperty(primitiveProp);
            edmProperty = primitiveProp;

            // Set Annotation StoreGeneratedPattern
            if (config.Kind == EdmTypeKind.Entity &&
                primitiveProperty.StoreGeneratedPattern != DatabaseGeneratedOption.None)
            {
                _directValueAnnotations.Add(
                    new StoreGeneratedPatternAnnotation(primitiveProp, primitiveProperty.StoreGeneratedPattern));
            }
        }
Ejemplo n.º 22
0
        private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config)
        {
            foreach (PropertyConfiguration property in config.Properties)
            {
                switch (property.Kind)
                {
                case PropertyKind.Primitive:
                    PrimitivePropertyConfiguration primitiveProperty = property as PrimitivePropertyConfiguration;

                    EdmPrimitiveTypeKind typeKind = GetTypeKind(primitiveProperty.PropertyInfo.PropertyType);
                    IEdmTypeReference    primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive(
                        typeKind,
                        primitiveProperty.OptionalProperty);

                    var primitiveProp = new EdmStructuralProperty(
                        type,
                        primitiveProperty.PropertyInfo.Name,
                        primitiveTypeReference);

                    type.AddProperty(primitiveProp);

                    // Set Annotation StoreGeneratedPattern
                    if (config.Kind == EdmTypeKind.Entity &&
                        primitiveProperty.StoreGeneratedPattern != DatabaseGeneratedOption.None)
                    {
                        _directValueAnnotations.Add(
                            new StoreGeneratedPatternAnnotation(primitiveProp, primitiveProperty.StoreGeneratedPattern));
                    }
                    break;

                case PropertyKind.Complex:
                    ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration;
                    IEdmComplexType complexType = _types[complexProperty.RelatedClrType] as IEdmComplexType;

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

                case PropertyKind.Collection:
                    CollectionPropertyConfiguration collectionProperty = property as CollectionPropertyConfiguration;
                    IEdmTypeReference elementTypeReference             = null;
                    if (_types.ContainsKey(collectionProperty.ElementType))
                    {
                        IEdmComplexType elementType = _types[collectionProperty.ElementType] as IEdmComplexType;
                        elementTypeReference = new EdmComplexTypeReference(elementType, false);
                    }
                    else
                    {
                        elementTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType);
                    }
                    type.AddStructuralProperty(
                        collectionProperty.PropertyInfo.Name,
                        new EdmCollectionTypeReference(
                            new EdmCollectionType(elementTypeReference),
                            collectionProperty.OptionalProperty));
                    break;

                default:
                    break;
                }
            }
        }