Ejemplo n.º 1
0
        internal IEdmSchemaType EnsureSchemaType(ResourceType resourceType)
        {
            IEdmSchemaType   type;
            ResourceTypeKind resourceTypeKind = resourceType.ResourceTypeKind;

            if (resourceTypeKind == ResourceTypeKind.Primitive)
            {
                return(MetadataProviderUtils.CreatePrimitiveTypeReference(resourceType).PrimitiveDefinition());
            }
            string typeNamespace = this.GetTypeNamespace(resourceType);
            string key           = ComputeSchemaTypeCacheKey(typeNamespace, resourceType);

            if (this.schemaTypeCache.TryGetValue(key, out type))
            {
                return(type);
            }
            switch (resourceTypeKind)
            {
            case ResourceTypeKind.EntityType:
                return(this.AddEntityType(resourceType, typeNamespace));

            case ResourceTypeKind.ComplexType:
                return(this.AddComplexType(resourceType, typeNamespace));
            }
            throw new InvalidOperationException(System.Data.Services.Strings.MetadataProviderEdmModel_UnsupportedSchemaTypeKind(resourceTypeKind.ToString()));
        }
Ejemplo n.º 2
0
        private IEdmEntityType AddEntityType(ResourceType resourceType, string resourceTypeNamespace)
        {
            Action <MetadataProviderEdmEntityType> propertyLoadAction = delegate(MetadataProviderEdmEntityType type) {
                IEnumerable <ResourceProperty> allVisiblePropertiesDeclaredInThisType = this.GetAllVisiblePropertiesDeclaredInThisType(resourceType);
                if (allVisiblePropertiesDeclaredInThisType != null)
                {
                    foreach (ResourceProperty property in allVisiblePropertiesDeclaredInThisType)
                    {
                        IEdmProperty property2 = this.CreateProperty(type, property);
                        if (property.IsOfKind(ResourcePropertyKind.Key))
                        {
                            type.AddKeys(new IEdmStructuralProperty[] { (IEdmStructuralProperty)property2 });
                        }
                    }
                }
            };
            MetadataProviderEdmEntityType schemaType = new MetadataProviderEdmEntityType(resourceTypeNamespace, resourceType.Name, (resourceType.BaseType != null) ? ((IEdmEntityType)this.EnsureSchemaType(resourceType.BaseType)) : null, resourceType.IsAbstract, resourceType.IsOpenType, propertyLoadAction);

            this.CacheSchemaType(schemaType);
            if (resourceType.IsMediaLinkEntry && ((resourceType.BaseType == null) || !resourceType.BaseType.IsMediaLinkEntry))
            {
                this.SetHasDefaultStream(schemaType, true);
            }
            if (resourceType.HasEntityPropertyMappings)
            {
                MetadataProviderUtils.ConvertEntityPropertyMappings(this, resourceType, schemaType);
            }
            MetadataProviderUtils.ConvertCustomAnnotations(this, resourceType.CustomAnnotations, schemaType);
            return(schemaType);
        }
Ejemplo n.º 3
0
        internal IEdmTypeReference EnsureTypeReference(ResourceType resourceType, List <KeyValuePair <string, object> > customAnnotations)
        {
            switch (resourceType.ResourceTypeKind)
            {
            case ResourceTypeKind.EntityType:
            {
                bool?andRemoveNullableFacet = MetadataProviderUtils.GetAndRemoveNullableFacet(customAnnotations);
                bool nullable = andRemoveNullableFacet.HasValue ? andRemoveNullableFacet.Value : true;
                return(this.EnsureSchemaType(resourceType).ToTypeReference(nullable));
            }

            case ResourceTypeKind.ComplexType:
            {
                bool?   nullable2  = MetadataProviderUtils.GetAndRemoveNullableFacet(customAnnotations);
                bool    flag2      = this.GetDataServiceVersion() >= RequestDescription.Version3Dot0;
                Version edmVersion = this.GetEdmVersion();
                if ((edmVersion != null) && (edmVersion < MetadataEdmSchemaVersion.Version3Dot0.ToVersion()))
                {
                    flag2 = false;
                }
                bool flag3 = nullable2.HasValue ? nullable2.Value : flag2;
                return(this.EnsureSchemaType(resourceType).ToTypeReference(flag3));
            }

            case ResourceTypeKind.Primitive:
                return(MetadataProviderUtils.CreatePrimitiveTypeReference(resourceType, customAnnotations));

            case ResourceTypeKind.Collection:
                return(this.EnsurePrimitiveOrComplexCollectionType(resourceType, customAnnotations).ToTypeReference(true));

            case ResourceTypeKind.EntityCollection:
                return(this.EnsureEntityCollectionType(resourceType, customAnnotations).ToTypeReference(true));
            }
            throw new InvalidOperationException(System.Data.Services.Strings.MetadataProviderEdmModel_UnsupportedResourceTypeKind(resourceType.ResourceTypeKind.ToString()));
        }
Ejemplo n.º 4
0
        private IEdmCollectionType EnsureCollectionItemTypeIsPrimitiveOrComplex(ResourceType itemResourceType, List <KeyValuePair <string, object> > customAnnotations)
        {
            IEdmCollectionType type;

            if (!this.primitiveOrComplexCollectionTypeCache.TryGetValue(itemResourceType, out type))
            {
                IEdmTypeReference reference;
                switch (itemResourceType.ResourceTypeKind)
                {
                case ResourceTypeKind.ComplexType:
                    reference = this.EnsureTypeReference(itemResourceType, customAnnotations);
                    reference = reference.IsNullable ? reference.Clone(false) : reference;
                    break;

                case ResourceTypeKind.Primitive:
                {
                    MetadataProviderUtils.GetAndRemoveNullableFacet(customAnnotations);
                    IEdmPrimitiveTypeReference typeReference = MetadataProviderUtils.CreatePrimitiveTypeReference(itemResourceType, customAnnotations);
                    reference = typeReference.IsNullable ? typeReference.Clone(false) : typeReference;
                    break;
                }

                default:
                    throw new InvalidOperationException(System.Data.Services.Strings.MetadataProviderEdmModel_UnsupportedCollectionItemType_PrimitiveOrComplex(itemResourceType.ResourceTypeKind.ToString()));
                }
                type = new EdmCollectionType(reference);
                this.primitiveOrComplexCollectionTypeCache.Add(itemResourceType, type);
            }
            return(type);
        }
        internal void AddEntitySet(string entitySetName, ResourceSetWrapper resourceSet)
        {
            IEdmEntityType elementType = (IEdmEntityType)this.model.EnsureSchemaType(resourceSet.ResourceType);
            IEdmEntitySet  target      = new EdmEntitySet(this, entitySetName, elementType);

            MetadataProviderUtils.ConvertCustomAnnotations(this.model, resourceSet.CustomAnnotations, target);
            this.entitySetCache.Add(entitySetName, target);
        }
Ejemplo n.º 6
0
        internal MetadataProviderEdmEntityContainer EnsureDefaultEntityContainer()
        {
            MetadataProviderEdmEntityContainer container;
            string containerName = this.metadataProvider.ContainerName;

            if (!this.entityContainerCache.TryGetValue(containerName, out container))
            {
                container = new MetadataProviderEdmEntityContainer(this, containerName, this.GetContainerNamespace());
                this.SetIsDefaultEntityContainer(container, true);
                MetadataProviderUtils.ConvertCustomAnnotations(this, this.metadataProvider.GetEntityContainerAnnotations(containerName), container);
                this.entityContainerCache.Add(containerName, container);
                this.entityContainerCache.Add(container.FullName(), container);
            }
            return(container);
        }
Ejemplo n.º 7
0
        private IEdmComplexType AddComplexType(ResourceType resourceType, string resourceTypeNamespace)
        {
            Action <MetadataProviderEdmComplexType> propertyLoadAction = delegate(MetadataProviderEdmComplexType type) {
                IEnumerable <ResourceProperty> allVisiblePropertiesDeclaredInThisType = this.GetAllVisiblePropertiesDeclaredInThisType(resourceType);
                if (allVisiblePropertiesDeclaredInThisType != null)
                {
                    foreach (ResourceProperty property in allVisiblePropertiesDeclaredInThisType)
                    {
                        this.CreateProperty(type, property);
                    }
                }
            };
            MetadataProviderEdmComplexType schemaType = new MetadataProviderEdmComplexType(resourceTypeNamespace, resourceType.Name, (resourceType.BaseType != null) ? ((IEdmComplexType)this.EnsureSchemaType(resourceType.BaseType)) : null, false, propertyLoadAction);

            this.CacheSchemaType(schemaType);
            MetadataProviderUtils.ConvertCustomAnnotations(this, resourceType.CustomAnnotations, schemaType);
            return(schemaType);
        }
Ejemplo n.º 8
0
 private void EnsureEntityContainers()
 {
     if (!this.HasCacheState(MetadataProviderState.EntityContainers))
     {
         string containerName      = this.metadataProvider.ContainerName;
         string containerNamespace = this.GetContainerNamespace();
         MetadataProviderEdmEntityContainer container    = this.EnsureDefaultEntityContainer();
         IEnumerable <ResourceSetWrapper>   resourceSets = this.metadataProvider.GetResourceSets();
         if (resourceSets != null)
         {
             foreach (ResourceSetWrapper wrapper in resourceSets)
             {
                 MetadataProviderEdmEntityContainer container2;
                 string key = wrapper.EntityContainerName ?? containerName;
                 if (!this.entityContainerCache.TryGetValue(key, out container2))
                 {
                     container2 = new MetadataProviderEdmEntityContainer(this, key, containerNamespace);
                     MetadataProviderUtils.ConvertCustomAnnotations(this, this.metadataProvider.GetEntityContainerAnnotations(key), container2);
                     this.entityContainerCache.Add(containerNamespace + '.' + key, container2);
                     this.entityContainerCache.Add(key, container2);
                 }
                 string entitySetName = MetadataProviderUtils.GetEntitySetName(wrapper.ResourceSet);
                 container2.AddEntitySet(entitySetName, wrapper);
             }
         }
         IEnumerable <OperationWrapper> visibleOperations = this.metadataProvider.GetVisibleOperations(this.operationContext);
         if (visibleOperations != null)
         {
             foreach (OperationWrapper wrapper2 in visibleOperations)
             {
                 container.EnsureFunctionImport(wrapper2);
             }
         }
         this.SetCacheState(MetadataProviderState.EntityContainers);
     }
 }
Ejemplo n.º 9
0
        private void PairUpNavigationPropertyWithResourceAssociationSet(MetadataProviderEdmEntityContainer entityContainer, ResourceAssociationSet resourceAssociationSet, ResourceAssociationType resourceAssociationType, ResourceType resourceType, ResourceProperty navigationProperty)
        {
            string            associationEndName;
            EdmMultiplicity   multiplicity;
            EdmOnDeleteAction deleteBehavior;
            string            str3;
            EdmMultiplicity   multiplicity2;
            EdmOnDeleteAction none;
            string            typeNamespace;
            bool   flag          = (resourceAssociationSet.End1.ResourceProperty != null) && (resourceAssociationSet.End2.ResourceProperty != null);
            string entitySetName = MetadataProviderUtils.GetEntitySetName(resourceAssociationSet.End1.ResourceSet);
            string name          = MetadataProviderUtils.GetEntitySetName(resourceAssociationSet.End2.ResourceSet);
            bool   isPrinciple   = false;
            List <IEdmStructuralProperty> dependentProperties = null;

            if (resourceAssociationType != null)
            {
                associationEndName = resourceAssociationType.End1.Name;
                deleteBehavior     = resourceAssociationType.End1.DeleteBehavior;
                multiplicity2      = MetadataProviderUtils.ConvertMultiplicity(resourceAssociationType.End1.Multiplicity);
                str3         = resourceAssociationType.End2.Name;
                none         = resourceAssociationType.End2.DeleteBehavior;
                multiplicity = MetadataProviderUtils.ConvertMultiplicity(resourceAssociationType.End2.Multiplicity);
                ResourceReferentialConstraint referentialConstraint = resourceAssociationType.ReferentialConstraint;
                if (referentialConstraint != null)
                {
                    isPrinciple = object.ReferenceEquals(resourceAssociationType.End1, referentialConstraint.PrincipalEnd);
                    IEdmEntityType type = isPrinciple ? ((IEdmEntityType)this.EnsureSchemaType(resourceAssociationSet.End2.ResourceType)) : ((IEdmEntityType)this.EnsureSchemaType(resourceAssociationSet.End1.ResourceType));
                    dependentProperties = new List <IEdmStructuralProperty>();
                    foreach (ResourceProperty property in referentialConstraint.DependentProperties)
                    {
                        IEdmProperty property2 = type.FindProperty(property.Name);
                        dependentProperties.Add((IEdmStructuralProperty)property2);
                    }
                }
            }
            else
            {
                if (!flag)
                {
                    if (resourceAssociationSet.End1.ResourceProperty != null)
                    {
                        associationEndName = resourceType.Name;
                        str3 = navigationProperty.Name;
                    }
                    else
                    {
                        associationEndName = navigationProperty.Name;
                        str3 = resourceType.Name;
                    }
                }
                else
                {
                    associationEndName = MetadataProviderUtils.GetAssociationEndName(resourceAssociationSet.End1.ResourceType, resourceAssociationSet.End1.ResourceProperty);
                    str3 = MetadataProviderUtils.GetAssociationEndName(resourceAssociationSet.End2.ResourceType, resourceAssociationSet.End2.ResourceProperty);
                }
                multiplicity   = MetadataProviderUtils.GetMultiplicity(resourceAssociationSet.End1.ResourceProperty);
                deleteBehavior = EdmOnDeleteAction.None;
                multiplicity2  = MetadataProviderUtils.GetMultiplicity(resourceAssociationSet.End2.ResourceProperty);
                none           = EdmOnDeleteAction.None;
            }
            string associationName = (resourceAssociationType == null) ? MetadataProviderUtils.GetAssociationName(resourceAssociationSet) : resourceAssociationType.Name;

            if ((resourceAssociationType == null) || (resourceAssociationType.NamespaceName == null))
            {
                ResourceAssociationSetEnd end = (resourceAssociationSet.End1.ResourceProperty != null) ? resourceAssociationSet.End1 : resourceAssociationSet.End2;
                typeNamespace = this.GetTypeNamespace(end.ResourceType);
            }
            else
            {
                typeNamespace = resourceAssociationType.NamespaceName;
            }
            ResourceProperty resourceProperty = resourceAssociationSet.End1.ResourceProperty;
            ResourceProperty property4        = resourceAssociationSet.End2.ResourceProperty;
            MetadataProviderEdmNavigationProperty partnerProperty = null;
            MetadataProviderEdmNavigationProperty property6       = null;

            if (resourceProperty != null)
            {
                IEdmEntityType type2 = (IEdmEntityType)this.EnsureSchemaType(resourceAssociationSet.End1.ResourceType);
                partnerProperty = (MetadataProviderEdmNavigationProperty)type2.FindProperty(resourceProperty.Name);
            }
            if (property4 != null)
            {
                IEdmEntityType type3 = (IEdmEntityType)this.EnsureSchemaType(resourceAssociationSet.End2.ResourceType);
                property6 = (MetadataProviderEdmNavigationProperty)type3.FindProperty(property4.Name);
            }
            IEdmNavigationProperty property7 = (partnerProperty != null) ? ((IEdmNavigationProperty)partnerProperty) : ((IEdmNavigationProperty) new MetadataProviderEdmSilentNavigationProperty(property6, deleteBehavior, multiplicity, associationEndName));
            IEdmNavigationProperty partner   = (property6 != null) ? ((IEdmNavigationProperty)property6) : ((IEdmNavigationProperty) new MetadataProviderEdmSilentNavigationProperty(partnerProperty, none, multiplicity2, str3));

            MetadataProviderUtils.FixUpNavigationPropertyWithAssociationSetData(property7, partner, isPrinciple, dependentProperties, deleteBehavior, multiplicity);
            MetadataProviderUtils.FixUpNavigationPropertyWithAssociationSetData(partner, property7, !isPrinciple, dependentProperties, none, multiplicity2);
            EdmEntitySet entitySet = (EdmEntitySet)entityContainer.FindEntitySet(entitySetName);
            EdmEntitySet target    = (EdmEntitySet)entityContainer.FindEntitySet(name);

            if (partnerProperty != null)
            {
                entitySet.AddNavigationTarget(partnerProperty, target);
                this.SetAssociationSetName(entitySet, partnerProperty, resourceAssociationSet.Name);
                this.SetAssociationSetAnnotations(entitySet, partnerProperty, MetadataProviderUtils.ConvertCustomAnnotations(this, resourceAssociationSet.CustomAnnotations), MetadataProviderUtils.ConvertCustomAnnotations(this, resourceAssociationSet.End1.CustomAnnotations), MetadataProviderUtils.ConvertCustomAnnotations(this, resourceAssociationSet.End2.CustomAnnotations));
            }
            if (property6 != null)
            {
                target.AddNavigationTarget(property6, entitySet);
                this.SetAssociationSetName(target, property6, resourceAssociationSet.Name);
                if (partnerProperty == null)
                {
                    this.SetAssociationSetAnnotations(entitySet, property6, MetadataProviderUtils.ConvertCustomAnnotations(this, resourceAssociationSet.CustomAnnotations), MetadataProviderUtils.ConvertCustomAnnotations(this, resourceAssociationSet.End1.CustomAnnotations), MetadataProviderUtils.ConvertCustomAnnotations(this, resourceAssociationSet.End2.CustomAnnotations));
                }
            }
            this.SetAssociationNamespace(property7, typeNamespace);
            this.SetAssociationName(property7, associationName);
            this.SetAssociationEndName(property7, associationEndName);
            this.SetAssociationNamespace(partner, typeNamespace);
            this.SetAssociationName(partner, associationName);
            this.SetAssociationEndName(partner, str3);
            if (resourceAssociationType != null)
            {
                this.SetAssociationAnnotations(property7, MetadataProviderUtils.ConvertCustomAnnotations(this, resourceAssociationType.CustomAnnotations), MetadataProviderUtils.ConvertCustomAnnotations(this, (property7.GetPrimary() == property7) ? resourceAssociationType.End1.CustomAnnotations : resourceAssociationType.End2.CustomAnnotations), MetadataProviderUtils.ConvertCustomAnnotations(this, (property7.GetPrimary() == property7) ? resourceAssociationType.End2.CustomAnnotations : resourceAssociationType.End1.CustomAnnotations), MetadataProviderUtils.ConvertCustomAnnotations(this, (resourceAssociationType.ReferentialConstraint != null) ? resourceAssociationType.ReferentialConstraint.CustomAnnotations : null));
            }
        }
Ejemplo n.º 10
0
        internal IEdmEntitySet FindEntitySet(ResourceSet resourceSet)
        {
            string name = resourceSet.EntityContainerName ?? this.metadataProvider.ContainerName;

            return(this.FindExistingEntityContainer(name).FindEntitySet(MetadataProviderUtils.GetEntitySetName(resourceSet)));
        }
Ejemplo n.º 11
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);
        }