예제 #1
0
        /// <summary>Gets the metadata document for this provider.</summary>
        /// <param name="metadataEdmSchemaVersion">EDM schema version.</param>
        /// <param name="service">Data service instance.</param>
        internal void GenerateMetadata(MetadataEdmSchemaVersion metadataEdmSchemaVersion, IDataService service)
        {
            Debug.Assert(this.Writer != null, "this.Writer != null");
            Debug.Assert(this.Provider != null, "this.Provider != null");

            MetadataManager metadataManager = new MetadataManager(this.Provider, service);
            string dataServiceVersion = MetadataSerializer.GetVersionsForMetadata(metadataManager.ResourceTypes, ref metadataEdmSchemaVersion);
            MetadataSerializer.WriteTopLevelSchemaElements(this.Writer, dataServiceVersion);
            HashSet<ResourceType> typesInEntityContainerNamespace = null;

            // Write the schema Element for every namespace
            foreach (KeyValuePair<string, HashSet<ResourceType>> namespaceAlongWithTypes in metadataManager.NamespaceAlongWithTypes)
            {
                Debug.Assert(!string.IsNullOrEmpty(namespaceAlongWithTypes.Key), "!string.IsNullOrEmpty(namespaceAlongWithTypes.Key)");

                // If the types live in the same namespace as that of entity container and types that don't live in any namespace,
                // should be written out in the service namespace. If the service type also doesn't have a namespace, we will use
                // the service name as the namespace. See code below for that.
                if (namespaceAlongWithTypes.Key == metadataManager.GetContainerNamespace())
                {
                    typesInEntityContainerNamespace = namespaceAlongWithTypes.Value;
                }
                else
                {
                    Dictionary<string, ResourceAssociationType> associationsInThisNamespace = metadataManager.GetAssociationTypesForNamespace(namespaceAlongWithTypes.Key);
                    MetadataSerializer.WriteSchemaElement(this.Writer, namespaceAlongWithTypes.Key, metadataEdmSchemaVersion);
                    WriteTypes(this.Writer, namespaceAlongWithTypes.Value, associationsInThisNamespace, metadataManager);
                    WriteAssociationTypes(this.Writer, new HashSet<ResourceAssociationType>(associationsInThisNamespace.Values, EqualityComparer<ResourceAssociationType>.Default));
                    this.Writer.WriteEndElement();
                }
            }

            // Write the entity container definition. Also if there are types in the same namespace,
            // we need to write them out too.
            string typeNamespace = metadataManager.GetContainerNamespace();
            MetadataSerializer.WriteSchemaElement(this.Writer, typeNamespace, metadataEdmSchemaVersion);
            if (typesInEntityContainerNamespace != null)
            {
                Dictionary<string, ResourceAssociationType> associationsInThisNamespace = metadataManager.GetAssociationTypesForNamespace(typeNamespace);
                WriteTypes(this.Writer, typesInEntityContainerNamespace, associationsInThisNamespace, metadataManager);
                WriteAssociationTypes(this.Writer, new HashSet<ResourceAssociationType>(associationsInThisNamespace.Values, EqualityComparer<ResourceAssociationType>.Default));
            }

            this.WriteEntityContainer(this.Writer, XmlConvert.EncodeName(this.Provider.ContainerName), metadataManager.ResourceSets, metadataManager.ResourceAssociationSets);
            this.Writer.WriteEndElement();

            // These end elements balance the elements written out in WriteTopLevelSchemaElements
            this.Writer.WriteEndElement();
            this.Writer.WriteEndElement();
            this.Writer.Flush();
        }