public StructuralTypeConfigurationTest() { Mock<StructuralTypeConfiguration> mockConfiguration = new Mock<StructuralTypeConfiguration> { CallBase = true }; mockConfiguration.Object.Name = "Name"; mockConfiguration.Object.Namespace = "Namespace"; _configuration = mockConfiguration.Object; }
private void CreateEdmTypeHeader(StructuralTypeConfiguration config) { if (!_types.ContainsKey(config.ClrType)) { if (config.Kind == EdmTypeKind.Complex) { _types.Add(config.ClrType, new EdmComplexType(config.Namespace, config.Name)); } else { EntityTypeConfiguration entity = config as EntityTypeConfiguration; Contract.Assert(entity != null); IEdmEntityType baseType = null; if (entity.BaseType != null) { CreateEdmTypeHeader(entity.BaseType); baseType = _types[entity.BaseType.ClrType] as IEdmEntityType; Contract.Assert(baseType != null); } _types.Add(config.ClrType, new EdmEntityType(config.Namespace, config.Name, baseType, entity.IsAbstract ?? false, isOpen: false)); } } }
/// <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); } }
public StructuralTypeConfigurationOfTStructuralTypeTest() { Mock<StructuralTypeConfiguration> mockConfig = new Mock<StructuralTypeConfiguration> { CallBase = true }; mockConfig.Object.Name = "Name"; mockConfig.Object.Namespace = "Namespace"; Mock<StructuralTypeConfiguration<object>> mockGenericConfig = new Mock<StructuralTypeConfiguration<object>>(mockConfig.Object) { CallBase = true }; _configuration = mockGenericConfig.Object; }
public PropertyConfigurationTest() { Mock<PropertyInfo> mockPropertyInfo = new Mock<PropertyInfo>(); _propertyInfo = mockPropertyInfo.Object; Mock<StructuralTypeConfiguration> mockTypeConfig = new Mock<StructuralTypeConfiguration>(); _declaringType = mockTypeConfig.Object; Mock<PropertyConfiguration> mockConfiguration = new Mock<PropertyConfiguration>(_propertyInfo, _declaringType) { CallBase = true }; mockConfiguration.Object.Name = "Name"; _configuration = mockConfiguration.Object; }
protected PropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType) { if (property == null) { throw Error.ArgumentNull("property"); } if (declaringType == null) { throw Error.ArgumentNull("declaringType"); } PropertyInfo = property; DeclaringType = declaringType; }
private void CreateEdmTypeBody(StructuralTypeConfiguration config) { IEdmType edmType = _types[config.ClrType]; if (edmType.TypeKind == EdmTypeKind.Complex) { CreateComplexTypeBody(edmType as EdmComplexType, config as ComplexTypeConfiguration); } else { if (edmType.TypeKind == EdmTypeKind.Entity) { CreateEntityTypeBody(edmType as EdmEntityType, config as EntityTypeConfiguration); } } }
private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config) { foreach (PropertyConfiguration property in config.Properties) { 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); var primitiveProp = new EdmStructuralProperty( type, primitiveProperty.PropertyInfo.Name, primitiveTypeReference); type.AddProperty(primitiveProp); // Set Annotation StoreGeneratedPattern if (config.Kind == EdmTypeKind.Entity && primitiveProperty.StoreGeneratedPattern != DatabaseGeneratedOption.None) { _directValueAnnotations.Add( new StoreGeneratedPatternAnnotation(primitiveProp, primitiveProperty.StoreGeneratedPattern)); } break; case PropertyKind.Complex: ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration; IEdmComplexType complexType = _types[complexProperty.RelatedClrType] as IEdmComplexType; type.AddStructuralProperty( complexProperty.PropertyInfo.Name, new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty)); break; case PropertyKind.Collection: CollectionPropertyConfiguration collectionProperty = property as CollectionPropertyConfiguration; IEdmTypeReference elementTypeReference = null; if (_types.ContainsKey(collectionProperty.ElementType)) { IEdmComplexType elementType = _types[collectionProperty.ElementType] as IEdmComplexType; elementTypeReference = new EdmComplexTypeReference(elementType, false); } else { elementTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType); } type.AddStructuralProperty( collectionProperty.PropertyInfo.Name, new EdmCollectionTypeReference( new EdmCollectionType(elementTypeReference), collectionProperty.OptionalProperty)); break; default: break; } } }
private static 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); } }
// 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; } } StructuralTypeConfiguration propertyType = GetStructuralTypeOrNull(property.RelatedClrType); Contract.Assert(propertyType != null, "we should already have seen this type"); if (!visitedTypes.Contains(propertyType)) { reachableTypes.Enqueue(propertyType); } } // all derived types and the base type are also reachable EntityTypeConfiguration currentEntityType = currentType as EntityTypeConfiguration; if (currentEntityType != null) { 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); } } } 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); } } }
public ComplexPropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType) : base(property, declaringType) { }
/// <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); }
// figures out the type of the property (primitive, complex, navigation) and the corresponding edm type if we have seen this type // earlier or the user told us about it. private PropertyKind GetPropertyType(PropertyInfo property, out bool isCollection, out StructuralTypeConfiguration mappedType) { Contract.Assert(property != null); if (EdmLibHelpers.GetEdmPrimitiveTypeOrNull(property.PropertyType) != null) { isCollection = false; mappedType = null; return(PropertyKind.Primitive); } else { mappedType = GetStructuralTypeOrNull(property.PropertyType); if (mappedType != null) { isCollection = false; if (mappedType is ComplexTypeConfiguration) { return(PropertyKind.Complex); } else { return(PropertyKind.Navigation); } } else { Type elementType; if (property.PropertyType.IsCollection(out elementType)) { isCollection = true; // Collection properties - can be a collection of primitives, complex or entities. if (EdmLibHelpers.GetEdmPrimitiveTypeOrNull(elementType) != null) { return(PropertyKind.Primitive); } else { mappedType = GetStructuralTypeOrNull(elementType); if (mappedType != null && mappedType is ComplexTypeConfiguration) { return(PropertyKind.Complex); } else { // if we know nothing about this type we assume it to be an entity // and patch up later return(PropertyKind.Navigation); } } } else { // if we know nothing about this type we assume it to be an entity // and patch up later isCollection = false; return(PropertyKind.Navigation); } } } }
private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config) { foreach (PropertyConfiguration property in config.Properties) { switch (property.Kind) { case PropertyKind.Primitive: PrimitivePropertyConfiguration primitiveProperty = property as PrimitivePropertyConfiguration; type.AddStructuralProperty( primitiveProperty.PropertyInfo.Name, GetTypeKind(primitiveProperty.PropertyInfo.PropertyType), primitiveProperty.OptionalProperty); break; case PropertyKind.Complex: ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration; IEdmComplexType complexType = _types[complexProperty.RelatedClrType] as IEdmComplexType; type.AddStructuralProperty( complexProperty.PropertyInfo.Name, new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty)); break; case PropertyKind.Collection: CollectionPropertyConfiguration collectionProperty = property as CollectionPropertyConfiguration; IEdmTypeReference elementTypeReference = null; if (_types.ContainsKey(collectionProperty.ElementType)) { IEdmComplexType elementType = _types[collectionProperty.ElementType] as IEdmComplexType; elementTypeReference = new EdmComplexTypeReference(elementType, false); } else { elementTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType); } type.AddStructuralProperty( collectionProperty.PropertyInfo.Name, new EdmCollectionTypeReference( new EdmCollectionType(elementTypeReference), collectionProperty.OptionalProperty)); break; default: break; } } }
private void CreatePrimitiveProperty(PrimitivePropertyConfiguration primitiveProperty, EdmStructuredType type, StructuralTypeConfiguration config, out IEdmProperty edmProperty) { 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; } var primitiveProp = new EdmStructuralProperty( type, primitiveProperty.PropertyInfo.Name, primitiveTypeReference, defaultValueString: null, concurrencyMode: concurrencyMode); type.AddProperty(primitiveProp); edmProperty = primitiveProp; // Set Annotation StoreGeneratedPattern if (config.Kind == EdmTypeKind.Entity && primitiveProperty.StoreGeneratedPattern != DatabaseGeneratedOption.None) { _directValueAnnotations.Add( new StoreGeneratedPatternAnnotation(primitiveProp, primitiveProperty.StoreGeneratedPattern)); } }
/// <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) { }
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 = _types[complexProperty.RelatedClrType] as IEdmComplexType; edmProperty = type.AddStructuralProperty( complexProperty.Name, new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty)); break; case PropertyKind.Collection: CollectionPropertyConfiguration collectionProperty = property as CollectionPropertyConfiguration; IEdmTypeReference elementTypeReference = null; if (_types.ContainsKey(collectionProperty.ElementType)) { IEdmComplexType elementType = _types[collectionProperty.ElementType] as IEdmComplexType; elementTypeReference = new EdmComplexTypeReference(elementType, false); } else { elementTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType); } edmProperty = type.AddStructuralProperty( collectionProperty.Name, new EdmCollectionTypeReference( new EdmCollectionType(elementTypeReference), collectionProperty.OptionalProperty)); break; default: break; } if (property.PropertyInfo != null && edmProperty != null) { _properties[property.PropertyInfo] = edmProperty; } if (edmProperty != null && property.IsRestricted) { _propertiesRestrictions[edmProperty] = new QueryableRestrictions(property); } } }
protected StructuralPropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType) : base(property, declaringType) { _optionalProperty = EdmLibHelpers.IsNullable(property.PropertyType); }
public ComplexPropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType) : base(property, declaringType) { }
private static PropertyConfiguration MockProperty(string name, StructuralTypeConfiguration declaringType) { Mock<PropertyInfo> propertyInfo = new Mock<PropertyInfo>(); propertyInfo.Setup(p => p.Name).Returns(name); Mock<PropertyConfiguration> property = new Mock<PropertyConfiguration>(propertyInfo.Object, declaringType); return property.Object; }
private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config) { foreach (PropertyConfiguration property in config.Properties) { 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); var primitiveProp = new EdmStructuralProperty( type, primitiveProperty.PropertyInfo.Name, primitiveTypeReference); type.AddProperty(primitiveProp); // Set Annotation StoreGeneratedPattern if (config.Kind == EdmTypeKind.Entity && primitiveProperty.StoreGeneratedPattern != DatabaseGeneratedOption.None) { _directValueAnnotations.Add( new StoreGeneratedPatternAnnotation(primitiveProp, primitiveProperty.StoreGeneratedPattern)); } break; case PropertyKind.Complex: ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration; IEdmComplexType complexType = _types[complexProperty.RelatedClrType] as IEdmComplexType; type.AddStructuralProperty( complexProperty.PropertyInfo.Name, new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty)); break; case PropertyKind.Collection: CollectionPropertyConfiguration collectionProperty = property as CollectionPropertyConfiguration; IEdmTypeReference elementTypeReference = null; if (_types.ContainsKey(collectionProperty.ElementType)) { IEdmComplexType elementType = _types[collectionProperty.ElementType] as IEdmComplexType; elementTypeReference = new EdmComplexTypeReference(elementType, false); } else { elementTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType); } type.AddStructuralProperty( collectionProperty.PropertyInfo.Name, new EdmCollectionTypeReference( new EdmCollectionType(elementTypeReference), collectionProperty.OptionalProperty)); break; default: break; } } }
public PrimitivePropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType) : base(property, declaringType) { }
private void CreatePrimitiveProperty(PrimitivePropertyConfiguration primitiveProperty, EdmStructuredType type, StructuralTypeConfiguration config, out IEdmProperty edmProperty) { 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; } var primitiveProp = new EdmStructuralProperty( type, primitiveProperty.PropertyInfo.Name, primitiveTypeReference, defaultValueString: null, concurrencyMode: concurrencyMode); type.AddProperty(primitiveProp); edmProperty = primitiveProp; // Set Annotation StoreGeneratedPattern if (config.Kind == EdmTypeKind.Entity && primitiveProperty.StoreGeneratedPattern != DatabaseGeneratedOption.None) { _directValueAnnotations.Add( new StoreGeneratedPatternAnnotation(primitiveProp, primitiveProperty.StoreGeneratedPattern)); } }