Example #1
0
        public TypeUsage GetModelTypeUsage()
        {
            if (_modelTypeUsage == null)
            {
                var edmType = EdmType;

                // If the edm type is already a cspace type, return the same type
                if (edmType.DataSpace == DataSpace.CSpace ||
                    edmType.DataSpace == DataSpace.OSpace)
                {
                    return(this);
                }

                TypeUsage result;
                if (Helper.IsRowType(edmType))
                {
                    var sspaceRowType = (RowType)edmType;
                    var properties    = new EdmProperty[sspaceRowType.Properties.Count];
                    for (var i = 0; i < properties.Length; i++)
                    {
                        var sspaceProperty = sspaceRowType.Properties[i];
                        var newTypeUsage   = sspaceProperty.TypeUsage.GetModelTypeUsage();
                        properties[i] = new EdmProperty(sspaceProperty.Name, newTypeUsage);
                    }
                    var edmRowType = new RowType(properties, sspaceRowType.InitializerMetadata);
                    result = Create(edmRowType, Facets);
                }
                else if (Helper.IsCollectionType(edmType))
                {
                    var sspaceCollectionType = ((CollectionType)edmType);
                    var newTypeUsage         = sspaceCollectionType.TypeUsage.GetModelTypeUsage();
                    result = Create(new CollectionType(newTypeUsage), Facets);
                }
                else if (Helper.IsPrimitiveType(edmType))
                {
                    result = ((PrimitiveType)edmType).ProviderManifest.GetEdmType(this);

                    if (result == null)
                    {
                        throw new ProviderIncompatibleException(Strings.Mapping_ProviderReturnsNullType(ToString()));
                    }

                    if (!TypeSemantics.IsNullable(this))
                    {
                        result = Create(
                            result.EdmType,
                            OverrideFacetValues(
                                result.Facets,
                                new FacetValues
                        {
                            Nullable = false
                        }));
                    }
                }
                else if (Helper.IsEntityTypeBase(edmType) ||
                         Helper.IsComplexType(edmType))
                {
                    result = this;
                }
                else
                {
                    Debug.Assert(false, "Unexpected type found in entity data reader");
                    return(null);
                }
                Interlocked.CompareExchange(ref _modelTypeUsage, result, null);
            }
            return(_modelTypeUsage);
        }
Example #2
0
        private static bool TryGetCommonType(EdmType edmType1, EdmType edmType2, out EdmType commonEdmType)
        {
            DebugCheck.NotNull(edmType1);
            DebugCheck.NotNull(edmType2);

            if (edmType2 == edmType1)
            {
                commonEdmType = edmType1;
                return(true);
            }

            if (Helper.IsPrimitiveType(edmType1) &&
                Helper.IsPrimitiveType(edmType2))
            {
                return(TryGetCommonType(
                           (PrimitiveType)edmType1,
                           (PrimitiveType)edmType2,
                           out commonEdmType));
            }

            else if (Helper.IsCollectionType(edmType1) &&
                     Helper.IsCollectionType(edmType2))
            {
                return(TryGetCommonType(
                           (CollectionType)edmType1,
                           (CollectionType)edmType2,
                           out commonEdmType));
            }

            else if (Helper.IsEntityTypeBase(edmType1) &&
                     Helper.IsEntityTypeBase(edmType2))
            {
                return(TryGetCommonBaseType(
                           edmType1,
                           edmType2,
                           out commonEdmType));
            }

            else if (Helper.IsRefType(edmType1) &&
                     Helper.IsRefType(edmType2))
            {
                return(TryGetCommonType(
                           (RefType)edmType1,
                           (RefType)edmType2,
                           out commonEdmType));
            }

            else if (Helper.IsRowType(edmType1) &&
                     Helper.IsRowType(edmType2))
            {
                return(TryGetCommonType(
                           (RowType)edmType1,
                           (RowType)edmType2,
                           out commonEdmType));
            }
            else
            {
                commonEdmType = null;
                return(false);
            }
        }