internal static ObjectTypeMapping GetObjectMapping(
            EdmType type,
            MetadataWorkspace workspace)
        {
            ItemCollection collection;

            if (workspace.TryGetItemCollection(DataSpace.CSpace, out collection))
            {
                return((ObjectTypeMapping)workspace.GetMap((GlobalItem)type, DataSpace.OCSpace));
            }
            EdmType edmType;
            EdmType cdmType;

            if (type.DataSpace == DataSpace.CSpace)
            {
                edmType = !Helper.IsPrimitiveType(type) ? workspace.GetItem <EdmType>(type.FullName, DataSpace.OSpace) : (EdmType)workspace.GetMappedPrimitiveType(((PrimitiveType)type).PrimitiveTypeKind, DataSpace.OSpace);
                cdmType = type;
            }
            else
            {
                edmType = type;
                cdmType = type;
            }
            if (!Helper.IsPrimitiveType(edmType) && !Helper.IsEntityType(edmType) && !Helper.IsComplexType(edmType))
            {
                throw new NotSupportedException(Strings.Materializer_UnsupportedType);
            }
            return(!Helper.IsPrimitiveType(edmType) ? DefaultObjectMappingItemCollection.LoadObjectMapping(cdmType, edmType, (DefaultObjectMappingItemCollection)null) : new ObjectTypeMapping(edmType, cdmType));
        }
            public void Registering_c_space_or_o_space_does_not_cause_oc_mapping_to_be_registered_if_it_is_already_registered()
            {
                var edmItemCollection       = new EdmItemCollection(new[] { XDocument.Parse(Csdl).CreateReader() });
                var objectItemCollection    = new ObjectItemCollection();
                var ocMappingItemCollection = new DefaultObjectMappingItemCollection(edmItemCollection, objectItemCollection);

                var workspace = new MetadataWorkspace();

#pragma warning disable 612,618
                workspace.RegisterItemCollection(ocMappingItemCollection);
                workspace.RegisterItemCollection(edmItemCollection);
                workspace.RegisterItemCollection(objectItemCollection);
#pragma warning restore 612,618

                Assert.Same(ocMappingItemCollection, workspace.GetItemCollection(DataSpace.OCSpace));
                Assert.Same(edmItemCollection, workspace.GetItemCollection(DataSpace.CSpace));
                Assert.Same(objectItemCollection, workspace.GetItemCollection(DataSpace.OSpace));
            }
            private static void Item_collections_can_be_registered(MetadataWorkspace workspace)
            {
                var edmItemCollection            = new EdmItemCollection(new[] { XDocument.Parse(Csdl).CreateReader() });
                var storeItemCollection          = new StoreItemCollection(new[] { XDocument.Parse(Ssdl).CreateReader() });
                var objectItemCollection         = new ObjectItemCollection();
                var storageMappingItemCollection = LoadMsl(edmItemCollection, storeItemCollection);
                var ocMappingItemCollection      = new DefaultObjectMappingItemCollection(edmItemCollection, objectItemCollection);

#pragma warning disable 612,618
                workspace.RegisterItemCollection(edmItemCollection);
                workspace.RegisterItemCollection(storeItemCollection);
                workspace.RegisterItemCollection(objectItemCollection);
                workspace.RegisterItemCollection(storageMappingItemCollection);
                workspace.RegisterItemCollection(ocMappingItemCollection);
#pragma warning restore 612,618

                Assert.Same(edmItemCollection, workspace.GetItemCollection(DataSpace.CSpace));
                Assert.Same(storeItemCollection, workspace.GetItemCollection(DataSpace.SSpace));
                Assert.Same(storageMappingItemCollection, workspace.GetItemCollection(DataSpace.CSSpace));
                Assert.Same(objectItemCollection, workspace.GetItemCollection(DataSpace.OSpace));
                Assert.Same(ocMappingItemCollection, workspace.GetItemCollection(DataSpace.OCSpace));
            }
        // <summary>
        // Retrieves a mapping to CLR type for the given EDM type. Assumes the MetadataWorkspace has no
        // </summary>
        internal static ObjectTypeMapping GetObjectMapping(EdmType type, MetadataWorkspace workspace)
        {
            // Check if the workspace has cspace item collection registered with it. If not, then its a case
            // of public materializer trying to create objects from PODR or EntityDataReader with no context.
            ItemCollection collection;

            if (workspace.TryGetItemCollection(DataSpace.CSpace, out collection))
            {
                return((ObjectTypeMapping)workspace.GetMap(type, DataSpace.OCSpace));
            }
            else
            {
                EdmType ospaceType;
                EdmType cspaceType;
                // If its a case of EntityDataReader with no context, the typeUsage which is passed in must contain
                // a cspace type. We need to look up an OSpace type in the ospace item collection and then create
                // ocMapping
                if (type.DataSpace
                    == DataSpace.CSpace)
                {
                    // if its a primitive type, then the names will be different for CSpace type and OSpace type
                    if (Helper.IsPrimitiveType(type))
                    {
                        ospaceType = workspace.GetMappedPrimitiveType(((PrimitiveType)type).PrimitiveTypeKind, DataSpace.OSpace);
                    }
                    else
                    {
                        // Metadata will throw if there is no item with this identity present.
                        // Is this exception fine or does object materializer code wants to wrap and throw a new exception
                        ospaceType = workspace.GetItem <EdmType>(type.FullName, DataSpace.OSpace);
                    }
                    cspaceType = type;
                }
                else
                {
                    // In case of PODR, there is no cspace at all. We must create a fake ocmapping, with ospace types
                    // on both the ends
                    ospaceType = type;
                    cspaceType = type;
                }

                // This condition must be hit only when someone is trying to materialize a legacy data reader and we
                // don't have the CSpace metadata.
                if (!Helper.IsPrimitiveType(ospaceType) &&
                    !Helper.IsEntityType(ospaceType) &&
                    !Helper.IsComplexType(ospaceType))
                {
                    throw new NotSupportedException(Strings.Materializer_UnsupportedType);
                }

                ObjectTypeMapping typeMapping;

                if (Helper.IsPrimitiveType(ospaceType))
                {
                    typeMapping = new ObjectTypeMapping(ospaceType, cspaceType);
                }
                else
                {
                    typeMapping = DefaultObjectMappingItemCollection.LoadObjectMapping(cspaceType, ospaceType, null);
                }

                return(typeMapping);
            }
        }