コード例 #1
0
 /// <summary>
 /// Constructs a CollectionPropertyConfiguration using the <paramref name="property">property</paramref> provided.
 /// </summary>
 public CollectionPropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType)
     : base(property, declaringType)
 {
     if (!property.PropertyType.IsCollection(out _elementType))
     {
         throw Error.Argument("property", SRResources.CollectionPropertiesMustReturnIEnumerable, property.Name, property.DeclaringType.FullName);
     }
 }
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyConfiguration"/> class.
        /// </summary>
        /// <param name="property">The name of the property.</param>
        /// <param name="declaringType">The declaring EDM type of the property.</param>
        protected PropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType)
        {
            if (property == null)
            {
                throw Error.ArgumentNull("property");
            }

            if (declaringType == null)
            {
                throw Error.ArgumentNull("declaringType");
            }

            PropertyInfo = property;
            DeclaringType = declaringType;
            AddedExplicitly = true;
            _name = property.Name;
        }
コード例 #3
0
        private void MapStructuralProperty(StructuralTypeConfiguration type, PropertyInfo property, PropertyKind propertyKind, bool isCollection)
        {
            Contract.Assert(type != null);
            Contract.Assert(property != null);
            Contract.Assert(propertyKind == PropertyKind.Complex || propertyKind == PropertyKind.Primitive || propertyKind == PropertyKind.Enum);

            bool addedExplicitly = type.Properties.Any(p => p.PropertyInfo.Name == property.Name);

            PropertyConfiguration addedEdmProperty;

            if (!isCollection)
            {
                if (propertyKind == PropertyKind.Primitive)
                {
                    addedEdmProperty = type.AddProperty(property);
                }
                else if (propertyKind == PropertyKind.Enum)
                {
                    AddEnumType(TypeHelper.GetUnderlyingTypeOrSelf(property.PropertyType));
                    addedEdmProperty = type.AddEnumProperty(property);
                }
                else
                {
                    addedEdmProperty = type.AddComplexProperty(property);
                }
            }
            else
            {
                if (_isQueryCompositionMode)
                {
                    Contract.Assert(propertyKind != PropertyKind.Complex, "we don't create complex types in query composition mode.");
                }

                if (TypeHelper.IsGenericType(property.PropertyType))
                {
                    Type elementType = property.PropertyType.GetGenericArguments().First();
                    Type elementUnderlyingTypeOrSelf = TypeHelper.GetUnderlyingTypeOrSelf(elementType);

                    if (TypeHelper.IsEnum(elementUnderlyingTypeOrSelf))
                    {
                        AddEnumType(elementUnderlyingTypeOrSelf);
                    }
                }
                else
                {
                    Type elementType;
                    if (TypeHelper.IsCollection(property.PropertyType, out elementType))
                    {
                        Type elementUnderlyingTypeOrSelf = TypeHelper.GetUnderlyingTypeOrSelf(elementType);
                        if (TypeHelper.IsEnum(elementUnderlyingTypeOrSelf))
                        {
                            AddEnumType(elementUnderlyingTypeOrSelf);
                        }
                    }
                }

                addedEdmProperty = type.AddCollectionProperty(property);
            }

            addedEdmProperty.AddedExplicitly = addedExplicitly;
        }
コード例 #4
0
        private void ApplyPropertyConvention(IEdmPropertyConvention propertyConvention, StructuralTypeConfiguration edmTypeConfiguration)
        {
            Contract.Assert(propertyConvention != null);
            Contract.Assert(edmTypeConfiguration != null);

            foreach (PropertyConfiguration property in edmTypeConfiguration.Properties.ToArray())
            {
                propertyConvention.Apply(property, edmTypeConfiguration, this);
            }
        }
コード例 #5
0
        private void CreateNavigationProperty(StructuralTypeConfiguration config)
        {
            Contract.Assert(config != null);

            EdmStructuredType type = (EdmStructuredType)(GetEdmType(config.ClrType));

            foreach (NavigationPropertyConfiguration navProp in config.NavigationProperties)
            {
                Func <NavigationPropertyConfiguration, EdmNavigationPropertyInfo> getInfo = nav =>
                {
                    EdmNavigationPropertyInfo info = new EdmNavigationPropertyInfo
                    {
                        Name = nav.Name,
                        TargetMultiplicity = nav.Multiplicity,
                        Target             = GetEdmType(nav.RelatedClrType) as IEdmEntityType,
                        ContainsTarget     = nav.ContainsTarget,
                        OnDelete           = nav.OnDeleteAction
                    };

                    // Principal properties
                    if (nav.PrincipalProperties.Any())
                    {
                        info.PrincipalProperties = GetDeclaringPropertyInfo(nav.PrincipalProperties);
                    }

                    // Dependent properties
                    if (nav.DependentProperties.Any())
                    {
                        info.DependentProperties = GetDeclaringPropertyInfo(nav.DependentProperties);
                    }

                    return(info);
                };

                var           navInfo    = getInfo(navProp);
                var           props      = new Dictionary <IEdmProperty, NavigationPropertyConfiguration>();
                EdmEntityType entityType = type as EdmEntityType;
                if (entityType != null && navProp.Partner != null)
                {
                    var edmProperty        = entityType.AddBidirectionalNavigation(navInfo, getInfo(navProp.Partner));
                    var partnerEdmProperty = (navInfo.Target as EdmEntityType).Properties().Single(p => p.Name == navProp.Partner.Name);
                    props.Add(edmProperty, navProp);
                    props.Add(partnerEdmProperty, navProp.Partner);
                }
                else
                {
                    // Do not add this if we have have a partner relationship configured, as this
                    // property will be added automatically through the AddBidirectionalNavigation
                    var targetConfig = config.ModelBuilder.GetTypeConfigurationOrNull(navProp.RelatedClrType) as StructuralTypeConfiguration;
                    if (!targetConfig.NavigationProperties.Any(p => p.Partner != null && p.Partner.Name == navInfo.Name))
                    {
                        var edmProperty = type.AddUnidirectionalNavigation(navInfo);
                        props.Add(edmProperty, navProp);
                    }
                }

                foreach (var item in props)
                {
                    var edmProperty = item.Key;
                    var prop        = item.Value;
                    if (prop.PropertyInfo != null)
                    {
                        _properties[prop.PropertyInfo] = edmProperty;
                    }

                    if (prop.IsRestricted)
                    {
                        _propertiesRestrictions[edmProperty] = new QueryableRestrictions(prop);
                    }

                    if (prop.QueryConfiguration.ModelBoundQuerySettings != null)
                    {
                        _propertiesQuerySettings.Add(edmProperty, prop.QueryConfiguration.ModelBoundQuerySettings);
                    }

                    _propertyConfigurations[edmProperty] = prop;
                }
            }
        }
コード例 #6
0
        public static bool IsAssignableFrom(this StructuralTypeConfiguration baseStructuralType,
            StructuralTypeConfiguration structuralType)
        {
            if (structuralType.Kind == EdmTypeKind.Entity && baseStructuralType.Kind == EdmTypeKind.Entity)
            {
                EntityTypeConfiguration entity = (EntityTypeConfiguration)structuralType;
                while (entity != null)
                {
                    if (baseStructuralType == entity)
                    {
                        return true;
                    }

                    entity = entity.BaseType;
                }
            }
            else if (structuralType.Kind == EdmTypeKind.Complex && baseStructuralType.Kind == EdmTypeKind.Complex)
            {
                ComplexTypeConfiguration complex = (ComplexTypeConfiguration)structuralType;
                while (complex != null)
                {
                    if (baseStructuralType == complex)
                    {
                        return true;
                    }

                    complex = complex.BaseType;
                }
            }

            return false;
        }
コード例 #7
0
 /// <summary>
 /// Instantiates a new instance of the <see cref="ComplexPropertyConfiguration"/> class.
 /// </summary>
 /// <param name="property">The property of the configuration.</param>
 /// <param name="declaringType">The declaring type of the property.</param>
 public ComplexPropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType)
     : base(property, declaringType)
 {
 }
コード例 #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PrecisionPropertyConfiguration"/> class.
 /// </summary>
 /// <param name="property">The name of the property.</param>
 /// <param name="declaringType">The declaring EDM type of the property.</param>
 public PrecisionPropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType)
     : base(property, declaringType)
 {
 }
コード例 #9
0
 internal virtual void DerivesFromNothingImpl()
 {
     _baseType = null;
     _baseTypeConfigured = true;
 }
コード例 #10
0
 /// <summary>
 /// Instantiates a new instance of the <see cref="ComplexPropertyConfiguration"/> class.
 /// </summary>
 /// <param name="property">The property of the configuration.</param>
 /// <param name="declaringType">The declaring type of the property.</param>
 public ComplexPropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType)
     : base(property, declaringType)
 {
 }
コード例 #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StructuralPropertyConfiguration"/> class.
 /// </summary>
 /// <param name="property">The property of the configuration.</param>
 /// <param name="declaringType">The declaring type of the property.</param>
 protected StructuralPropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType)
     : base(property, declaringType)
 {
     OptionalProperty = EdmLibHelpers.IsNullable(property.PropertyType);
 }
コード例 #12
0
 // Returns the base types, this type.
 public static IEnumerable <StructuralTypeConfiguration> ThisAndBaseTypes(
     this StructuralTypeConfiguration structuralType)
 {
     Contract.Assert(structuralType != null);
     return(structuralType.BaseTypes().Concat(new[] { structuralType }));
 }
コード例 #13
0
ファイル: EdmTypeBuilder.cs プロジェクト: akrisiun/WebApi
        private IEdmProperty CreateStructuralTypeEnumPropertyBody(EdmStructuredType type, StructuralTypeConfiguration config, EnumPropertyConfiguration enumProperty)
        {
            Type enumPropertyType = TypeHelper.GetUnderlyingTypeOrSelf(enumProperty.RelatedClrType);
            IEdmType edmType = GetEdmType(enumPropertyType);

            if (edmType == null)
            {
                throw Error.InvalidOperation(SRResources.EnumTypeDoesNotExist, enumPropertyType.Name);
            }

            IEdmEnumType enumType = (IEdmEnumType)edmType;
            IEdmTypeReference enumTypeReference = new EdmEnumTypeReference(enumType, enumProperty.OptionalProperty);

            // Set concurrency token if is entity type, and concurrency token is true
            EdmConcurrencyMode enumConcurrencyMode = EdmConcurrencyMode.None;
            if (config.Kind == EdmTypeKind.Entity && enumProperty.ConcurrencyToken)
            {
                enumConcurrencyMode = EdmConcurrencyMode.Fixed;
            }

            return type.AddStructuralProperty(
                enumProperty.Name,
                enumTypeReference,
                defaultValue: null,
                concurrencyMode: enumConcurrencyMode);
        }
コード例 #14
0
ファイル: EdmTypeBuilder.cs プロジェクト: akrisiun/WebApi
        private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config)
        {
            foreach (PropertyConfiguration property in config.Properties)
            {
                IEdmProperty edmProperty = null;

                switch (property.Kind)
                {
                    case PropertyKind.Primitive:
                        PrimitivePropertyConfiguration primitiveProperty = property as PrimitivePropertyConfiguration;
                        EdmPrimitiveTypeKind typeKind = GetTypeKind(primitiveProperty.PropertyInfo.PropertyType);
                        IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive(
                            typeKind,
                            primitiveProperty.OptionalProperty);

                        // Set concurrency token if is entity type, and concurrency token is true
                        EdmConcurrencyMode concurrencyMode = EdmConcurrencyMode.None;
                        if (config.Kind == EdmTypeKind.Entity && primitiveProperty.ConcurrencyToken)
                        {
                            concurrencyMode = EdmConcurrencyMode.Fixed;
                        }
                        edmProperty = type.AddStructuralProperty(
                            primitiveProperty.Name,
                            primitiveTypeReference,
                            defaultValue: null,
                            concurrencyMode: concurrencyMode);
                        break;

                    case PropertyKind.Complex:
                        ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration;
                        IEdmComplexType complexType = GetEdmType(complexProperty.RelatedClrType) as IEdmComplexType;

                        edmProperty = type.AddStructuralProperty(
                            complexProperty.Name,
                            new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty));
                        break;

                    case PropertyKind.Collection:
                        edmProperty = CreateStructuralTypeCollectionPropertyBody(type, (CollectionPropertyConfiguration)property);
                        break;

                    case PropertyKind.Enum:
                        edmProperty = CreateStructuralTypeEnumPropertyBody(type, config, (EnumPropertyConfiguration)property);
                        break;

                    default:
                        break;
                }

                if (edmProperty != null)
                {
                    if (property.PropertyInfo != null)
                    {
                        _properties[property.PropertyInfo] = edmProperty;
                    }

                    if (property.IsRestricted)
                    {
                        _propertiesRestrictions[edmProperty] = new QueryableRestrictions(property);
                    }
                }
            }
        }
コード例 #15
0
        private void CreateEdmTypeHeader(IEdmTypeConfiguration config)
        {
            IEdmType edmType = GetEdmType(config.ClrType);

            if (edmType == null)
            {
                if (config.Kind == EdmTypeKind.Complex)
                {
                    ComplexTypeConfiguration complex  = (ComplexTypeConfiguration)config;
                    IEdmComplexType          baseType = null;
                    if (complex.BaseType != null)
                    {
                        CreateEdmTypeHeader(complex.BaseType);
                        baseType = GetEdmType(complex.BaseType.ClrType) as IEdmComplexType;

                        Contract.Assert(baseType != null);
                    }

                    EdmComplexType complexType = new EdmComplexType(config.Namespace, config.Name,
                                                                    baseType, complex.IsAbstract ?? false, complex.IsOpen);

                    _types.Add(config.ClrType, complexType);

                    if (complex.IsOpen)
                    {
                        // add a mapping between the open complex type and its dynamic property dictionary.
                        _openTypes.Add(complexType, complex.DynamicPropertyDictionary);
                    }

                    if (complex.SupportsInstanceAnnotations)
                    {
                        // add a mapping between the complex type and its instance annotation dictionary.
                        _instanceAnnotableTypes.Add(complexType, complex.InstanceAnnotationsContainer);
                    }

                    edmType = complexType;
                }
                else if (config.Kind == EdmTypeKind.Entity)
                {
                    EntityTypeConfiguration entity = config as EntityTypeConfiguration;
                    Contract.Assert(entity != null);

                    IEdmEntityType baseType = null;
                    if (entity.BaseType != null)
                    {
                        CreateEdmTypeHeader(entity.BaseType);
                        baseType = GetEdmType(entity.BaseType.ClrType) as IEdmEntityType;

                        Contract.Assert(baseType != null);
                    }

                    EdmEntityType entityType = new EdmEntityType(config.Namespace, config.Name, baseType,
                                                                 entity.IsAbstract ?? false, entity.IsOpen, entity.HasStream);
                    _types.Add(config.ClrType, entityType);

                    if (entity.IsOpen)
                    {
                        // add a mapping between the open entity type and its dynamic property dictionary.
                        _openTypes.Add(entityType, entity.DynamicPropertyDictionary);
                    }

                    if (entity.SupportsInstanceAnnotations)
                    {
                        // add a mapping between the entity type and its instance annotation dictionary.
                        _instanceAnnotableTypes.Add(entityType, entity.InstanceAnnotationsContainer);
                    }

                    edmType = entityType;
                }
                else
                {
                    EnumTypeConfiguration enumTypeConfiguration = config as EnumTypeConfiguration;

                    // The config has to be enum.
                    Contract.Assert(enumTypeConfiguration != null);

                    _types.Add(enumTypeConfiguration.ClrType,
                               new EdmEnumType(enumTypeConfiguration.Namespace, enumTypeConfiguration.Name,
                                               GetTypeKind(enumTypeConfiguration.UnderlyingType), enumTypeConfiguration.IsFlags));
                }
            }

            IEdmStructuredType          structuredType = edmType as IEdmStructuredType;
            StructuralTypeConfiguration structuralTypeConfiguration = config as StructuralTypeConfiguration;

            if (structuredType != null && structuralTypeConfiguration != null &&
                !_structuredTypeQuerySettings.ContainsKey(structuredType))
            {
                ModelBoundQuerySettings querySettings =
                    structuralTypeConfiguration.QueryConfiguration.ModelBoundQuerySettings;
                if (querySettings != null)
                {
                    _structuredTypeQuerySettings.Add(structuredType,
                                                     structuralTypeConfiguration.QueryConfiguration.ModelBoundQuerySettings);
                }
            }
        }
コード例 #16
0
        internal bool InferEdmTypeFromDerivedTypes(Type propertyType, ref PropertyKind propertyKind)
        {
            HashSet <Type> visitedTypes = new HashSet <Type>();

            Queue <Type> typeToBeVisited = new Queue <Type>();

            typeToBeVisited.Enqueue(propertyType);

            IList <StructuralTypeConfiguration> foundMappedTypes = new List <StructuralTypeConfiguration>();

            while (typeToBeVisited.Count != 0)
            {
                Type currentType = typeToBeVisited.Dequeue();
                visitedTypes.Add(currentType);

                List <Type> derivedTypes;
                if (_allTypesWithDerivedTypeMapping.Value.TryGetValue(currentType, out derivedTypes))
                {
                    foreach (Type derivedType in derivedTypes)
                    {
                        if (!visitedTypes.Contains(derivedType))
                        {
                            StructuralTypeConfiguration structuralType =
                                _explicitlyAddedTypes.FirstOrDefault(c => c.ClrType == derivedType);

                            if (structuralType != null)
                            {
                                foundMappedTypes.Add(structuralType);
                            }

                            typeToBeVisited.Enqueue(derivedType);
                        }
                    }
                }
            }

            if (!foundMappedTypes.Any())
            {
                return(false);
            }

            IEnumerable <EntityTypeConfiguration> foundMappedEntityType =
                foundMappedTypes.OfType <EntityTypeConfiguration>().ToList();
            IEnumerable <ComplexTypeConfiguration> foundMappedComplexType =
                foundMappedTypes.OfType <ComplexTypeConfiguration>().ToList();

            if (!foundMappedEntityType.Any())
            {
                propertyKind = PropertyKind.Complex;
                return(true);
            }
            else if (!foundMappedComplexType.Any())
            {
                propertyKind = PropertyKind.Navigation;
                return(true);
            }
            else
            {
                throw Error.InvalidOperation(SRResources.CannotInferEdmType,
                                             propertyType.FullName,
                                             String.Join(",", foundMappedEntityType.Select(e => e.ClrType.FullName)),
                                             String.Join(",", foundMappedComplexType.Select(e => e.ClrType.FullName)));
            }
        }
コード例 #17
0
        // the convention model builder MapTypes() method might have went through deep object graphs and added a bunch of types
        // only to realise after applying the conventions that the user has ignored some of the properties. So, prune the unreachable stuff.
        private void PruneUnreachableTypes()
        {
            Contract.Assert(_explicitlyAddedTypes != null);

            // Do a BFS starting with the types the user has explicitly added to find out the unreachable nodes.
            Queue <StructuralTypeConfiguration>   reachableTypes = new Queue <StructuralTypeConfiguration>(_explicitlyAddedTypes);
            HashSet <StructuralTypeConfiguration> visitedTypes   = new HashSet <StructuralTypeConfiguration>();

            while (reachableTypes.Count != 0)
            {
                StructuralTypeConfiguration currentType = reachableTypes.Dequeue();

                // go visit other end of each of this node's edges.
                foreach (PropertyConfiguration property in currentType.Properties.Where(property => property.Kind != PropertyKind.Primitive))
                {
                    if (property.Kind == PropertyKind.Collection)
                    {
                        // if the elementType is primitive we don't need to do anything.
                        CollectionPropertyConfiguration colProperty = property as CollectionPropertyConfiguration;
                        if (EdmLibHelpers.GetEdmPrimitiveTypeOrNull(colProperty.ElementType) != null)
                        {
                            continue;
                        }
                    }

                    IEdmTypeConfiguration propertyType = GetStructuralTypeOrNull(property.RelatedClrType);
                    Contract.Assert(propertyType != null, "we should already have seen this type");

                    var structuralTypeConfiguration = propertyType as StructuralTypeConfiguration;
                    if (structuralTypeConfiguration != null && !visitedTypes.Contains(propertyType))
                    {
                        reachableTypes.Enqueue(structuralTypeConfiguration);
                    }
                }

                // all derived types and the base type are also reachable
                if (currentType.Kind == EdmTypeKind.Entity)
                {
                    EntityTypeConfiguration currentEntityType = (EntityTypeConfiguration)currentType;
                    if (currentEntityType.BaseType != null && !visitedTypes.Contains(currentEntityType.BaseType))
                    {
                        reachableTypes.Enqueue(currentEntityType.BaseType);
                    }

                    foreach (EntityTypeConfiguration derivedType in this.DerivedTypes(currentEntityType))
                    {
                        if (!visitedTypes.Contains(derivedType))
                        {
                            reachableTypes.Enqueue(derivedType);
                        }
                    }
                }
                else if (currentType.Kind == EdmTypeKind.Complex)
                {
                    ComplexTypeConfiguration currentComplexType = (ComplexTypeConfiguration)currentType;
                    if (currentComplexType.BaseType != null && !visitedTypes.Contains(currentComplexType.BaseType))
                    {
                        reachableTypes.Enqueue(currentComplexType.BaseType);
                    }

                    foreach (ComplexTypeConfiguration derivedType in this.DerivedTypes(currentComplexType))
                    {
                        if (!visitedTypes.Contains(derivedType))
                        {
                            reachableTypes.Enqueue(derivedType);
                        }
                    }
                }

                visitedTypes.Add(currentType);
            }

            StructuralTypeConfiguration[] allConfiguredTypes = StructuralTypes.ToArray();
            foreach (StructuralTypeConfiguration type in allConfiguredTypes)
            {
                if (!visitedTypes.Contains(type))
                {
                    // we don't have to fix up any properties because this type is unreachable and cannot be a property of any reachable type.
                    RemoveStructuralType(type.ClrType);
                }
            }
        }
コード例 #18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NavigationPropertyConfiguration"/> class.
        /// </summary>
        /// <param name="property">The backing CLR property.</param>
        /// <param name="multiplicity">The <see cref="EdmMultiplicity"/>.</param>
        /// <param name="declaringType">The declaring structural type.</param>
        public NavigationPropertyConfiguration(PropertyInfo property, EdmMultiplicity multiplicity, StructuralTypeConfiguration declaringType)
            : base(property, declaringType)
        {
            if (property == null)
            {
                throw Error.ArgumentNull("property");
            }

            Multiplicity = multiplicity;

            _relatedType = property.PropertyType;
            if (multiplicity == EdmMultiplicity.Many)
            {
                Type elementType;
                if (!TypeHelper.IsCollection(_relatedType, out elementType))
                {
                    throw Error.Argument("property", SRResources.ManyToManyNavigationPropertyMustReturnCollection, property.Name, TypeHelper.GetReflectedType(property).Name);
                }

                _relatedType = elementType;
            }

            OnDeleteAction = EdmOnDeleteAction.None;
        }
コード例 #19
0
        internal virtual void DerivesFromImpl(StructuralTypeConfiguration baseType)
        {
            if (baseType == null)
            {
                throw Error.ArgumentNull("baseType");
            }

            _baseType = baseType;
            _baseTypeConfigured = true;

            if (!baseType.ClrType.IsAssignableFrom(ClrType) || baseType.ClrType == ClrType)
            {
                throw Error.Argument("baseType", SRResources.TypeDoesNotInheritFromBaseType,
                    ClrType.FullName, baseType.ClrType.FullName);
            }

            foreach (PropertyConfiguration property in Properties)
            {
                ValidatePropertyNotAlreadyDefinedInBaseTypes(property.PropertyInfo);
            }

            foreach (PropertyConfiguration property in this.DerivedProperties())
            {
                ValidatePropertyNotAlreadyDefinedInDerivedTypes(property.PropertyInfo);
            }
        }
コード例 #20
0
        private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config)
        {
            foreach (PropertyConfiguration property in config.Properties)
            {
                IEdmProperty edmProperty = null;

                switch (property.Kind)
                {
                case PropertyKind.Primitive:
                    PrimitivePropertyConfiguration primitiveProperty = property as PrimitivePropertyConfiguration;
                    EdmPrimitiveTypeKind           typeKind          = GetTypeKind(primitiveProperty.PropertyInfo.PropertyType);
                    IEdmTypeReference primitiveTypeReference         = EdmCoreModel.Instance.GetPrimitive(
                        typeKind,
                        primitiveProperty.OptionalProperty);

                    // Set concurrency token if is entity type, and concurrency token is true
                    EdmConcurrencyMode concurrencyMode = EdmConcurrencyMode.None;
                    if (config.Kind == EdmTypeKind.Entity && primitiveProperty.ConcurrencyToken)
                    {
                        concurrencyMode = EdmConcurrencyMode.Fixed;
                    }
                    edmProperty = type.AddStructuralProperty(
                        primitiveProperty.Name,
                        primitiveTypeReference,
                        defaultValue: null,
                        concurrencyMode: concurrencyMode);
                    break;

                case PropertyKind.Complex:
                    ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration;
                    IEdmComplexType complexType = GetEdmType(complexProperty.RelatedClrType) as IEdmComplexType;

                    edmProperty = type.AddStructuralProperty(
                        complexProperty.Name,
                        new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty));
                    break;

                case PropertyKind.Collection:
                    edmProperty = CreateStructuralTypeCollectionPropertyBody(type, (CollectionPropertyConfiguration)property);
                    break;

                case PropertyKind.Enum:
                    edmProperty = CreateStructuralTypeEnumPropertyBody(type, config, (EnumPropertyConfiguration)property);
                    break;

                default:
                    break;
                }

                if (edmProperty != null)
                {
                    if (property.PropertyInfo != null)
                    {
                        _properties[property.PropertyInfo] = edmProperty;
                    }

                    if (property.IsRestricted)
                    {
                        _propertiesRestrictions[edmProperty] = new QueryableRestrictions(property);
                    }
                }
            }
        }
コード例 #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PrimitivePropertyConfiguration"/> class.
 /// </summary>
 /// <param name="property">The name of the property.</param>
 /// <param name="declaringType">The declaring EDM type of the property.</param>
 public PrimitivePropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType)
     : base(property, declaringType)
 {
 }
コード例 #22
0
        private IEdmProperty CreateStructuralTypeEnumPropertyBody(EdmStructuredType type, StructuralTypeConfiguration config, EnumPropertyConfiguration enumProperty)
        {
            Type     enumPropertyType = TypeHelper.GetUnderlyingTypeOrSelf(enumProperty.RelatedClrType);
            IEdmType edmType          = GetEdmType(enumPropertyType);

            if (edmType == null)
            {
                throw Error.InvalidOperation(SRResources.EnumTypeDoesNotExist, enumPropertyType.Name);
            }

            IEdmEnumType      enumType          = (IEdmEnumType)edmType;
            IEdmTypeReference enumTypeReference = new EdmEnumTypeReference(enumType, enumProperty.OptionalProperty);

            // Set concurrency token if is entity type, and concurrency token is true
            EdmConcurrencyMode enumConcurrencyMode = EdmConcurrencyMode.None;

            if (config.Kind == EdmTypeKind.Entity && enumProperty.ConcurrencyToken)
            {
                enumConcurrencyMode = EdmConcurrencyMode.Fixed;
            }

            return(type.AddStructuralProperty(
                       enumProperty.Name,
                       enumTypeReference,
                       defaultValue: null,
                       concurrencyMode: enumConcurrencyMode));
        }
コード例 #23
0
        // Returns all the derived types of this type.
        public static IEnumerable<StructuralTypeConfiguration> DerivedTypes(this ODataModelBuilder modelBuilder,
            StructuralTypeConfiguration structuralType)
        {
            if (modelBuilder == null)
            {
                throw Error.ArgumentNull("modelBuilder");
            }

            if (structuralType == null)
            {
                throw Error.ArgumentNull("structuralType");
            }

            if (structuralType.Kind == EdmTypeKind.Entity)
            {
                return DerivedTypes(modelBuilder, (EntityTypeConfiguration)structuralType);
            }

            if (structuralType.Kind == EdmTypeKind.Complex)
            {
                return DerivedTypes(modelBuilder, (ComplexTypeConfiguration)structuralType);
            }

            return Enumerable.Empty<StructuralTypeConfiguration>();
        }
コード例 #24
0
 internal virtual void DerivesFromNothingImpl()
 {
     _baseType           = null;
     _baseTypeConfigured = true;
 }
コード例 #25
0
        // Returns the base types, this type and all the derived types of this type.
        public static IEnumerable<StructuralTypeConfiguration> ThisAndBaseAndDerivedTypes(
            this ODataModelBuilder modelBuilder, StructuralTypeConfiguration structuralType)
        {
            Contract.Assert(modelBuilder != null);
            Contract.Assert(structuralType != null);

            return structuralType.BaseTypes()
                    .Concat(new[] { structuralType })
                    .Concat(modelBuilder.DerivedTypes(structuralType));
        }
コード例 #26
0
        private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config)
        {
            foreach (PropertyConfiguration property in config.Properties)
            {
                IEdmProperty edmProperty = null;

                switch (property.Kind)
                {
                case PropertyKind.Primitive:
                    PrimitivePropertyConfiguration primitiveProperty = (PrimitivePropertyConfiguration)property;
                    EdmPrimitiveTypeKind           typeKind          = primitiveProperty.TargetEdmTypeKind ??
                                                                       GetTypeKind(primitiveProperty.PropertyInfo.PropertyType);
                    IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive(
                        typeKind,
                        primitiveProperty.OptionalProperty);

                    if (typeKind == EdmPrimitiveTypeKind.Decimal)
                    {
                        DecimalPropertyConfiguration decimalProperty =
                            primitiveProperty as DecimalPropertyConfiguration;
                        if (decimalProperty.Precision.HasValue || decimalProperty.Scale.HasValue)
                        {
                            primitiveTypeReference = new EdmDecimalTypeReference(
                                (IEdmPrimitiveType)primitiveTypeReference.Definition,
                                primitiveTypeReference.IsNullable,
                                decimalProperty.Precision,
                                decimalProperty.Scale.HasValue ? decimalProperty.Scale : 0);
                        }
                    }
                    else if (EdmLibHelpers.HasPrecision(typeKind))
                    {
                        PrecisionPropertyConfiguration precisionProperty =
                            primitiveProperty as PrecisionPropertyConfiguration;
                        primitiveTypeReference = AddPrecisionConfigInPrimitiveTypeReference(
                            precisionProperty,
                            primitiveTypeReference);
                    }
                    else if (EdmLibHelpers.HasLength(typeKind))
                    {
                        LengthPropertyConfiguration lengthProperty =
                            primitiveProperty as LengthPropertyConfiguration;
                        primitiveTypeReference = AddLengthConfigInPrimitiveTypeReference(
                            lengthProperty,
                            primitiveTypeReference);
                    }
                    edmProperty = type.AddStructuralProperty(
                        primitiveProperty.Name,
                        primitiveTypeReference,
                        defaultValue: primitiveProperty.DefaultValueString);
                    break;

                case PropertyKind.Complex:
                    ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration;
                    IEdmComplexType complexType = GetEdmType(complexProperty.RelatedClrType) as IEdmComplexType;

                    edmProperty = type.AddStructuralProperty(
                        complexProperty.Name,
                        new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty));
                    break;

                case PropertyKind.Collection:
                    edmProperty = CreateStructuralTypeCollectionPropertyBody(type, (CollectionPropertyConfiguration)property);
                    break;

                case PropertyKind.Enum:
                    edmProperty = CreateStructuralTypeEnumPropertyBody(type, (EnumPropertyConfiguration)property);
                    break;

                default:
                    break;
                }

                if (edmProperty != null)
                {
                    if (property.PropertyInfo != null)
                    {
                        _properties[property.PropertyInfo] = edmProperty;
                    }

                    if (property.IsRestricted)
                    {
                        _propertiesRestrictions[edmProperty] = new QueryableRestrictions(property);
                    }

                    if (property.QueryConfiguration.ModelBoundQuerySettings != null)
                    {
                        _propertiesQuerySettings.Add(edmProperty, property.QueryConfiguration.ModelBoundQuerySettings);
                    }

                    _propertyConfigurations[edmProperty] = property;
                }
            }
        }