Exemple #1
0
        internal static bool TryDetermineCSpaceModelType(Type type, MetadataWorkspace workspace, out EdmType modelEdmType)
        {
            Debug.Assert(null != workspace);
            Type nonNullabelType = TypeSystem.GetNonNullableType(type);

            // make sure the workspace knows about T
            workspace.ImplicitLoadAssemblyForType(nonNullabelType, System.Reflection.Assembly.GetCallingAssembly());
            ObjectItemCollection objectItemCollection = (ObjectItemCollection)workspace.GetItemCollection(DataSpace.OSpace);
            EdmType objectEdmType;

            if (objectItemCollection.TryGetItem <EdmType>(nonNullabelType.FullName, out objectEdmType))
            {
                Map map;
                if (workspace.TryGetMap(objectEdmType, DataSpace.OCSpace, out map))
                {
                    ObjectTypeMapping objectMapping = (ObjectTypeMapping)map;
                    modelEdmType = objectMapping.EdmType;
                    return(true);
                }
            }
            modelEdmType = null;
            return(false);
        }
        // <summary>
        // Search for a Mapping metadata with the specified type key.
        // </summary>
        // <param name="identity"> identity of the type </param>
        // <param name="typeSpace"> The dataspace that the type for which map needs to be returned belongs to </param>
        // <param name="ignoreCase"> true for case-insensitive lookup </param>
        // <returns> Returns false if no match found. </returns>
        internal override bool TryGetMap(string identity, DataSpace typeSpace, bool ignoreCase, out MappingBase map)
        {
            EdmType cdmType = null;
            EdmType clrType = null;

            if (typeSpace == DataSpace.CSpace)
            {
                if (ignoreCase)
                {
                    // Get the correct casing of the identity first if we are asked to do ignore case
                    if (!_edmCollection.TryGetItem(identity, true, out cdmType))
                    {
                        map = null;
                        return(false);
                    }

                    identity = cdmType.Identity;
                }

                int index;
                if (_edmTypeIndexes.TryGetValue(identity, out index))
                {
                    map = (MappingBase)this[index];
                    return(true);
                }

                if (cdmType != null
                    ||
                    _edmCollection.TryGetItem(identity, ignoreCase, out cdmType))
                {
                    // If the mapping is not already loaded, then get the mapping ospace type
                    _objectCollection.TryGetOSpaceType(cdmType, out clrType);
                }
            }
            else if (typeSpace == DataSpace.OSpace)
            {
                if (ignoreCase)
                {
                    // Get the correct casing of the identity first if we are asked to do ignore case
                    if (!_objectCollection.TryGetItem(identity, true, out clrType))
                    {
                        map = null;
                        return(false);
                    }

                    identity = clrType.Identity;
                }

                int index;
                if (_clrTypeIndexes.TryGetValue(identity, out index))
                {
                    map = (MappingBase)this[index];
                    return(true);
                }

                if (clrType != null
                    ||
                    _objectCollection.TryGetItem(identity, ignoreCase, out clrType))
                {
                    // If the mapping is not already loaded, get the mapping cspace type
                    var cspaceTypeName = ObjectItemCollection.TryGetMappingCSpaceTypeIdentity(clrType);
                    _edmCollection.TryGetItem(cspaceTypeName, out cdmType);
                }
            }

            if ((clrType == null) ||
                (cdmType == null))
            {
                map = null;
                return(false);
            }
            else
            {
                map = GetDefaultMapping(cdmType, clrType);
                return(true);
            }
        }