private static ObjectTypeMapping LoadObjectMapping( EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary <string, ObjectTypeMapping> typeMappings) { if (Helper.IsEnumType(edmType) ^ Helper.IsEnumType(objectType)) { throw new MappingException(Strings.Mapping_EnumTypeMappingToNonEnumType((object)edmType.FullName, (object)objectType.FullName)); } if (edmType.Abstract != objectType.Abstract) { throw new MappingException(Strings.Mapping_AbstractTypeMappingToNonAbstractType((object)edmType.FullName, (object)objectType.FullName)); } ObjectTypeMapping objectMapping = new ObjectTypeMapping(objectType, edmType); typeMappings.Add(edmType.FullName, objectMapping); if (Helper.IsEntityType(edmType) || Helper.IsComplexType(edmType)) { DefaultObjectMappingItemCollection.LoadEntityTypeOrComplexTypeMapping(objectMapping, edmType, objectType, ocItemCollection, typeMappings); } else if (Helper.IsEnumType(edmType)) { DefaultObjectMappingItemCollection.ValidateEnumTypeMapping((EnumType)edmType, (EnumType)objectType); } else { DefaultObjectMappingItemCollection.LoadAssociationTypeMapping(objectMapping, edmType, objectType, ocItemCollection, typeMappings); } return(objectMapping); }
// This method should be called inside a lock unless it is being called from the constructor. private ObjectTypeMapping AddInternalMapping( ObjectTypeMapping objectMap, Dictionary <string, int> clrTypeIndexes, Dictionary <string, int> edmTypeIndexes) { if (Source.ContainsIdentity(objectMap.Identity)) { return((ObjectTypeMapping)Source[objectMap.Identity]); } objectMap.DataSpace = DataSpace.OCSpace; var currIndex = Count; AddInternal(objectMap); var clrName = objectMap.ClrType.Identity; if (!clrTypeIndexes.ContainsKey(clrName)) { clrTypeIndexes.Add(clrName, currIndex); } var edmName = objectMap.EdmType.Identity; if (!edmTypeIndexes.ContainsKey(edmName)) { edmTypeIndexes.Add(edmName, currIndex); } return(objectMap); }
private ObjectTypeMapping AddInternalMapping( ObjectTypeMapping objectMap, Dictionary <string, int> clrTypeIndexes, Dictionary <string, int> edmTypeIndexes) { if (this.Source.ContainsIdentity(objectMap.Identity)) { return((ObjectTypeMapping)this.Source[objectMap.Identity]); } objectMap.DataSpace = DataSpace.OCSpace; int count = this.Count; this.AddInternal((GlobalItem)objectMap); string identity1 = objectMap.ClrType.Identity; if (!clrTypeIndexes.ContainsKey(identity1)) { clrTypeIndexes.Add(identity1, count); } string identity2 = objectMap.EdmType.Identity; if (!edmTypeIndexes.ContainsKey(identity2)) { edmTypeIndexes.Add(identity2, count); } return(objectMap); }
// <summary> // Load the entity type or complex type mapping // </summary> private static void LoadEntityTypeOrComplexTypeMapping( ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary <string, ObjectTypeMapping> typeMappings) { Debug.Assert( edmType.BuiltInTypeKind == BuiltInTypeKind.EntityType || edmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType, "Expected Type Encountered in LoadEntityTypeOrComplexTypeMapping"); Debug.Assert( (edmType.BuiltInTypeKind == objectType.BuiltInTypeKind), "The BuiltInTypeKind must be same in LoadEntityTypeOrComplexTypeMapping"); var cdmStructuralType = (StructuralType)edmType; var objectStructuralType = (StructuralType)objectType; ValidateAllMembersAreMapped(cdmStructuralType, objectStructuralType); //Go through the CDMMembers and find the corresponding member in Object space //and create a member map. foreach (var edmMember in cdmStructuralType.Members) { var objectMember = GetObjectMember(edmMember, objectStructuralType); ValidateMembersMatch(edmMember, objectMember); if (Helper.IsEdmProperty(edmMember)) { var edmPropertyMember = (EdmProperty)edmMember; var edmPropertyObject = (EdmProperty)objectMember; //Depending on the type of member load the member mapping i.e. For complex //members we have to go in and load the child members of the Complex type. if (Helper.IsComplexType(edmMember.TypeUsage.EdmType)) { objectMapping.AddMemberMap( LoadComplexMemberMapping(edmPropertyMember, edmPropertyObject, ocItemCollection, typeMappings)); } else { objectMapping.AddMemberMap( LoadScalarPropertyMapping(edmPropertyMember, edmPropertyObject)); } } else { Debug.Assert(edmMember.BuiltInTypeKind == BuiltInTypeKind.NavigationProperty, "Unexpected Property type encountered"); // For navigation properties, we need to make sure the relationship type on the navigation property is mapped var navigationProperty = (NavigationProperty)edmMember; var objectNavigationProperty = (NavigationProperty)objectMember; LoadTypeMapping( navigationProperty.RelationshipType, objectNavigationProperty.RelationshipType, ocItemCollection, typeMappings); objectMapping.AddMemberMap(new ObjectNavigationPropertyMapping(navigationProperty, objectNavigationProperty)); } } }
private Map GetOCMapForTransientType(EdmType edmType, DataSpace typeSpace) { Debug.Assert( typeSpace == DataSpace.CSpace || typeSpace == DataSpace.OSpace || Helper.IsRowType(edmType) || Helper.IsCollectionType(edmType)); EdmType clrType = null; EdmType cdmType = null; var index = -1; if (typeSpace != DataSpace.OSpace) { if (cdmTypeIndexes.TryGetValue(edmType.Identity, out index)) { return((Map)this[index]); } else { cdmType = edmType; clrType = ConvertCSpaceToOSpaceType(edmType); } } else if (typeSpace == DataSpace.OSpace) { if (clrTypeIndexes.TryGetValue(edmType.Identity, out index)) { return((Map)this[index]); } else { clrType = edmType; cdmType = ConvertOSpaceToCSpaceType(clrType); } } var typeMapping = new ObjectTypeMapping(clrType, cdmType); if (BuiltInTypeKind.RowType == edmType.BuiltInTypeKind) { var clrRowType = (RowType)clrType; var edmRowType = (RowType)cdmType; Debug.Assert(clrRowType.Properties.Count == edmRowType.Properties.Count, "Property count mismatch"); for (var idx = 0; idx < clrRowType.Properties.Count; idx++) { typeMapping.AddMemberMap(new ObjectPropertyMapping(edmRowType.Properties[idx], clrRowType.Properties[idx])); } } if ((!cdmTypeIndexes.ContainsKey(cdmType.Identity)) && (!clrTypeIndexes.ContainsKey(clrType.Identity))) { AddInternalMapping(typeMapping); } return(typeMapping); }
internal static ObjectTypeMapping LoadObjectMapping( EdmType cdmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection) { Dictionary <string, ObjectTypeMapping> typeMappings = new Dictionary <string, ObjectTypeMapping>((IEqualityComparer <string>)StringComparer.Ordinal); ObjectTypeMapping objectTypeMapping = DefaultObjectMappingItemCollection.LoadObjectMapping(cdmType, objectType, ocItemCollection, typeMappings); ocItemCollection?.AddInternalMappings((IEnumerable <ObjectTypeMapping>)typeMappings.Values); return(objectTypeMapping); }
private bool ContainsMap(GlobalItem cspaceItem, out ObjectTypeMapping map) { int index; if (this._edmTypeIndexes.TryGetValue(cspaceItem.Identity, out index)) { map = (ObjectTypeMapping)this[index]; return(true); } map = (ObjectTypeMapping)null; return(false); }
private bool ContainsMap(GlobalItem cspaceItem, out ObjectTypeMapping map) { Debug.Assert(cspaceItem.DataSpace == DataSpace.CSpace, "ContainsMap: It must be a CSpace item"); int index; if (_edmTypeIndexes.TryGetValue(cspaceItem.Identity, out index)) { map = (ObjectTypeMapping)this[index]; return(true); } map = null; return(false); }
private MappingBase GetOCMapForTransientType(EdmType edmType, DataSpace typeSpace) { EdmType clrType = (EdmType)null; EdmType cdmType = (EdmType)null; int index1 = -1; if (typeSpace != DataSpace.OSpace) { if (this._edmTypeIndexes.TryGetValue(edmType.Identity, out index1)) { return((MappingBase)this[index1]); } cdmType = edmType; clrType = this.ConvertCSpaceToOSpaceType(edmType); } else if (typeSpace == DataSpace.OSpace) { if (this._clrTypeIndexes.TryGetValue(edmType.Identity, out index1)) { return((MappingBase)this[index1]); } clrType = edmType; cdmType = this.ConvertOSpaceToCSpaceType(clrType); } ObjectTypeMapping objectMap = new ObjectTypeMapping(clrType, cdmType); if (BuiltInTypeKind.RowType == edmType.BuiltInTypeKind) { RowType rowType1 = (RowType)clrType; RowType rowType2 = (RowType)cdmType; for (int index2 = 0; index2 < rowType1.Properties.Count; ++index2) { objectMap.AddMemberMap((ObjectMemberMapping) new ObjectPropertyMapping(rowType2.Properties[index2], rowType1.Properties[index2])); } } if (!this._edmTypeIndexes.ContainsKey(cdmType.Identity) && !this._clrTypeIndexes.ContainsKey(clrType.Identity)) { lock (this._lock) { Dictionary <string, int> clrTypeIndexes = new Dictionary <string, int>((IDictionary <string, int>) this._clrTypeIndexes); Dictionary <string, int> edmTypeIndexes = new Dictionary <string, int>((IDictionary <string, int>) this._edmTypeIndexes); objectMap = this.AddInternalMapping(objectMap, clrTypeIndexes, edmTypeIndexes); this._clrTypeIndexes = clrTypeIndexes; this._edmTypeIndexes = edmTypeIndexes; } } return((MappingBase)objectMap); }
internal StateManagerTypeMetadata(EdmType edmType, ObjectTypeMapping mapping) { DebugCheck.NotNull(edmType); Debug.Assert( Helper.IsEntityType(edmType) || Helper.IsComplexType(edmType), "not Complex or EntityType"); Debug.Assert( ReferenceEquals(mapping, null) || ReferenceEquals(mapping.EdmType, edmType), "different EdmType instance"); _typeUsage = TypeUsage.Create(edmType); _recordInfo = new DataRecordInfo(_typeUsage); var members = TypeHelpers.GetProperties(edmType); _members = new StateManagerMemberMetadata[members.Count]; _objectNameToOrdinal = new Dictionary<string, int>(members.Count); _cLayerNameToOrdinal = new Dictionary<string, int>(members.Count); ReadOnlyMetadataCollection<EdmMember> keyMembers = null; if (Helper.IsEntityType(edmType)) { keyMembers = ((EntityType)edmType).KeyMembers; } for (var i = 0; i < _members.Length; ++i) { var member = members[i]; ObjectPropertyMapping memberMap = null; if (null != mapping) { memberMap = mapping.GetPropertyMap(member.Name); if (null != memberMap) { _objectNameToOrdinal.Add(memberMap.ClrProperty.Name, i); // olayer name } } _cLayerNameToOrdinal.Add(member.Name, i); // clayer name // Determine whether this member is part of the identity of the entity. _members[i] = new StateManagerMemberMetadata(memberMap, member, ((null != keyMembers) && keyMembers.Contains(member))); } }
// Add to the cache. If it is already present, then throw an exception private void AddInternalMapping(ObjectTypeMapping objectMap) { var clrName = objectMap.ClrType.Identity; var cdmName = objectMap.EdmType.Identity; var currIndex = Count; //Always assume that the first Map for an associated map being added is //the default map for primitive type. Similarly, row and collection types can collide //because their components are primitive types. For other types, //there should be only one map if (clrTypeIndexes.ContainsKey(clrName)) { if (BuiltInTypeKind.PrimitiveType != objectMap.ClrType.BuiltInTypeKind && BuiltInTypeKind.RowType != objectMap.ClrType.BuiltInTypeKind && BuiltInTypeKind.CollectionType != objectMap.ClrType.BuiltInTypeKind) { throw new MappingException(Strings.Mapping_Duplicate_Type(clrName)); } } else { clrTypeIndexes.Add(clrName, currIndex); } if (cdmTypeIndexes.ContainsKey(cdmName)) { if (BuiltInTypeKind.PrimitiveType != objectMap.EdmType.BuiltInTypeKind && BuiltInTypeKind.RowType != objectMap.EdmType.BuiltInTypeKind && BuiltInTypeKind.CollectionType != objectMap.EdmType.BuiltInTypeKind) { throw new MappingException(Strings.Mapping_Duplicate_Type(clrName)); } } else { cdmTypeIndexes.Add(cdmName, currIndex); } objectMap.DataSpace = DataSpace.OCSpace; base.AddInternal(objectMap); }
private static ObjectTypeMapping LoadObjectMapping( EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary <string, ObjectTypeMapping> typeMappings) { DebugCheck.NotNull(edmType); DebugCheck.NotNull(objectType); if (Helper.IsEnumType(edmType) ^ Helper.IsEnumType(objectType)) { throw new MappingException(Strings.Mapping_EnumTypeMappingToNonEnumType(edmType.FullName, objectType.FullName)); } // Check if both the types are abstract or both of them are not if (edmType.Abstract != objectType.Abstract) { throw new MappingException(Strings.Mapping_AbstractTypeMappingToNonAbstractType(edmType.FullName, objectType.FullName)); } var objectTypeMapping = new ObjectTypeMapping(objectType, edmType); typeMappings.Add(edmType.FullName, objectTypeMapping); if (Helper.IsEntityType(edmType) || Helper.IsComplexType(edmType)) { LoadEntityTypeOrComplexTypeMapping(objectTypeMapping, edmType, objectType, ocItemCollection, typeMappings); } else if (Helper.IsEnumType(edmType)) { ValidateEnumTypeMapping((EnumType)edmType, (EnumType)objectType); } else { Debug.Assert(Helper.IsAssociationType(edmType)); LoadAssociationTypeMapping(objectTypeMapping, edmType, objectType, ocItemCollection, typeMappings); } return(objectTypeMapping); }
private static void LoadAssociationTypeMapping( ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary <string, ObjectTypeMapping> typeMappings) { AssociationType associationType1 = (AssociationType)edmType; AssociationType associationType2 = (AssociationType)objectType; foreach (AssociationEndMember associationEndMember in associationType1.AssociationEndMembers) { AssociationEndMember objectMember = (AssociationEndMember)DefaultObjectMappingItemCollection.GetObjectMember((EdmMember)associationEndMember, (StructuralType)associationType2); DefaultObjectMappingItemCollection.ValidateMembersMatch((EdmMember)associationEndMember, (EdmMember)objectMember); if (associationEndMember.RelationshipMultiplicity != objectMember.RelationshipMultiplicity) { throw new MappingException(Strings.Mapping_Default_OCMapping_MultiplicityMismatch((object)associationEndMember.RelationshipMultiplicity, (object)associationEndMember.Name, (object)associationType1.FullName, (object)objectMember.RelationshipMultiplicity, (object)objectMember.Name, (object)associationType2.FullName)); } DefaultObjectMappingItemCollection.LoadTypeMapping((EdmType)((RefType)associationEndMember.TypeUsage.EdmType).ElementType, (EdmType)((RefType)objectMember.TypeUsage.EdmType).ElementType, ocItemCollection, typeMappings); objectMapping.AddMemberMap((ObjectMemberMapping) new ObjectAssociationEndMapping(associationEndMember, objectMember)); } }
// <summary> // Loads Association Type Mapping // </summary> private static void LoadAssociationTypeMapping( ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary <string, ObjectTypeMapping> typeMappings) { Debug.Assert( edmType.BuiltInTypeKind == BuiltInTypeKind.AssociationType, "Expected Type Encountered in LoadAssociationTypeMapping"); Debug.Assert( (edmType.BuiltInTypeKind == objectType.BuiltInTypeKind), "The BuiltInTypeKind must be same in LoadAssociationTypeMapping"); var association = (AssociationType)edmType; var objectAssociation = (AssociationType)objectType; foreach (var edmEnd in association.AssociationEndMembers) { var objectEnd = (AssociationEndMember)GetObjectMember(edmEnd, objectAssociation); ValidateMembersMatch(edmEnd, objectEnd); if (edmEnd.RelationshipMultiplicity != objectEnd.RelationshipMultiplicity) { throw new MappingException( Strings.Mapping_Default_OCMapping_MultiplicityMismatch( edmEnd.RelationshipMultiplicity, edmEnd.Name, association.FullName, objectEnd.RelationshipMultiplicity, objectEnd.Name, objectAssociation.FullName)); } Debug.Assert(edmEnd.TypeUsage.EdmType.BuiltInTypeKind == BuiltInTypeKind.RefType, "Ends must be of Ref type"); // GetMap for the entity types for the ends of the relationship type to make sure // the entity type mentioned are valid LoadTypeMapping( ((RefType)edmEnd.TypeUsage.EdmType).ElementType, ((RefType)objectEnd.TypeUsage.EdmType).ElementType, ocItemCollection, typeMappings); objectMapping.AddMemberMap(new ObjectAssociationEndMapping(edmEnd, objectEnd)); } }
private static void LoadEntityTypeOrComplexTypeMapping( ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary <string, ObjectTypeMapping> typeMappings) { StructuralType cdmStructuralType = (StructuralType)edmType; StructuralType structuralType = (StructuralType)objectType; DefaultObjectMappingItemCollection.ValidateAllMembersAreMapped(cdmStructuralType, structuralType); foreach (EdmMember member in cdmStructuralType.Members) { EdmMember objectMember = DefaultObjectMappingItemCollection.GetObjectMember(member, structuralType); DefaultObjectMappingItemCollection.ValidateMembersMatch(member, objectMember); if (Helper.IsEdmProperty(member)) { EdmProperty edmProperty1 = (EdmProperty)member; EdmProperty edmProperty2 = (EdmProperty)objectMember; if (Helper.IsComplexType(member.TypeUsage.EdmType)) { objectMapping.AddMemberMap((ObjectMemberMapping)DefaultObjectMappingItemCollection.LoadComplexMemberMapping(edmProperty1, edmProperty2, ocItemCollection, typeMappings)); } else { objectMapping.AddMemberMap((ObjectMemberMapping)DefaultObjectMappingItemCollection.LoadScalarPropertyMapping(edmProperty1, edmProperty2)); } } else { NavigationProperty edmNavigationProperty = (NavigationProperty)member; NavigationProperty clrNavigationProperty = (NavigationProperty)objectMember; DefaultObjectMappingItemCollection.LoadTypeMapping((EdmType)edmNavigationProperty.RelationshipType, (EdmType)clrNavigationProperty.RelationshipType, ocItemCollection, typeMappings); objectMapping.AddMemberMap((ObjectMemberMapping) new ObjectNavigationPropertyMapping(edmNavigationProperty, clrNavigationProperty)); } } }
/// <summary> /// Load the entity type or complex type mapping /// </summary> /// <param name="objectMapping"></param> /// <param name="edmType"></param> /// <param name="objectType"></param> /// <param name="ocItemCollection"> /// <param name="typeMappings"></param></param> private static void LoadEntityTypeOrComplexTypeMapping( ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary<string, ObjectTypeMapping> typeMappings) { Debug.Assert( edmType.BuiltInTypeKind == BuiltInTypeKind.EntityType || edmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType, "Expected Type Encountered in LoadEntityTypeOrComplexTypeMapping"); Debug.Assert( (edmType.BuiltInTypeKind == objectType.BuiltInTypeKind), "The BuiltInTypeKind must be same in LoadEntityTypeOrComplexTypeMapping"); var cdmStructuralType = (StructuralType)edmType; var objectStructuralType = (StructuralType)objectType; ValidateAllMembersAreMapped(cdmStructuralType, objectStructuralType); //Go through the CDMMembers and find the corresponding member in Object space //and create a member map. foreach (var edmMember in cdmStructuralType.Members) { var objectMember = GetObjectMember(edmMember, objectStructuralType); ValidateMembersMatch(edmMember, objectMember); if (Helper.IsEdmProperty(edmMember)) { var edmPropertyMember = (EdmProperty)edmMember; var edmPropertyObject = (EdmProperty)objectMember; //Depending on the type of member load the member mapping i.e. For complex //members we have to go in and load the child members of the Complex type. if (Helper.IsComplexType(edmMember.TypeUsage.EdmType)) { objectMapping.AddMemberMap( LoadComplexMemberMapping(edmPropertyMember, edmPropertyObject, ocItemCollection, typeMappings)); } else { objectMapping.AddMemberMap( LoadScalarPropertyMapping(edmPropertyMember, edmPropertyObject)); } } else { Debug.Assert(edmMember.BuiltInTypeKind == BuiltInTypeKind.NavigationProperty, "Unexpected Property type encountered"); // For navigation properties, we need to make sure the relationship type on the navigation property is mapped var navigationProperty = (NavigationProperty)edmMember; var objectNavigationProperty = (NavigationProperty)objectMember; LoadTypeMapping( navigationProperty.RelationshipType, objectNavigationProperty.RelationshipType, ocItemCollection, typeMappings); objectMapping.AddMemberMap(new ObjectNavigationPropertyMapping(navigationProperty, objectNavigationProperty)); } } }
private static ObjectTypeMapping LoadObjectMapping( EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary<string, ObjectTypeMapping> typeMappings) { Debug.Assert((edmType != null) && (objectType != null)); Debug.Assert((edmType.BuiltInTypeKind == objectType.BuiltInTypeKind), "The BuiltInTypeKind must be same in LoadObjectMapping"); if (Helper.IsEnumType(edmType) ^ Helper.IsEnumType(objectType)) { throw new MappingException(Strings.Mapping_EnumTypeMappingToNonEnumType(edmType.FullName, objectType.FullName)); } // Check if both the types are abstract or both of them are not if (edmType.Abstract != objectType.Abstract) { throw new MappingException(Strings.Mapping_AbstractTypeMappingToNonAbstractType(edmType.FullName, objectType.FullName)); } var objectTypeMapping = new ObjectTypeMapping(objectType, edmType); typeMappings.Add(edmType.FullName, objectTypeMapping); if (Helper.IsEntityType(edmType) || Helper.IsComplexType(edmType)) { LoadEntityTypeOrComplexTypeMapping(objectTypeMapping, edmType, objectType, ocItemCollection, typeMappings); } else if (Helper.IsEnumType(edmType)) { ValidateEnumTypeMapping((EnumType)edmType, (EnumType)objectType); } else { Debug.Assert(Helper.IsAssociationType(edmType)); LoadAssociationTypeMapping(objectTypeMapping, edmType, objectType, ocItemCollection, typeMappings); } return objectTypeMapping; }
private Map GetOCMapForTransientType(EdmType edmType, DataSpace typeSpace) { Debug.Assert( typeSpace == DataSpace.CSpace || typeSpace == DataSpace.OSpace || Helper.IsRowType(edmType) || Helper.IsCollectionType(edmType)); EdmType clrType = null; EdmType cdmType = null; var index = -1; if (typeSpace != DataSpace.OSpace) { if (cdmTypeIndexes.TryGetValue(edmType.Identity, out index)) { return (Map)this[index]; } else { cdmType = edmType; clrType = ConvertCSpaceToOSpaceType(edmType); } } else if (typeSpace == DataSpace.OSpace) { if (clrTypeIndexes.TryGetValue(edmType.Identity, out index)) { return (Map)this[index]; } else { clrType = edmType; cdmType = ConvertOSpaceToCSpaceType(clrType); } } var typeMapping = new ObjectTypeMapping(clrType, cdmType); if (BuiltInTypeKind.RowType == edmType.BuiltInTypeKind) { var clrRowType = (RowType)clrType; var edmRowType = (RowType)cdmType; Debug.Assert(clrRowType.Properties.Count == edmRowType.Properties.Count, "Property count mismatch"); for (var idx = 0; idx < clrRowType.Properties.Count; idx++) { typeMapping.AddMemberMap(new ObjectPropertyMapping(edmRowType.Properties[idx], clrRowType.Properties[idx])); } } if ((!cdmTypeIndexes.ContainsKey(cdmType.Identity)) && (!clrTypeIndexes.ContainsKey(clrType.Identity))) { AddInternalMapping(typeMapping); } return typeMapping; }
internal Plan(TypeUsage key, ObjectTypeMapping mapping, ReadOnlyCollection<FieldMetadata> fields) { Debug.Assert(null != mapping, "null ObjectTypeMapping"); Debug.Assert(null != fields, "null FieldMetadata"); Key = key; Debug.Assert(!Helper.IsEntityType(mapping.ClrType), "Expecting complex type"); ClrType = LightweightCodeGenerator.GetConstructorDelegateForType((ClrComplexType)mapping.ClrType); Properties = new PlanEdmProperty[fields.Count]; var lastOrdinal = -1; for (var i = 0; i < Properties.Length; ++i) { var field = fields[i]; Debug.Assert( unchecked((uint)field.Ordinal) < unchecked((uint)fields.Count), "FieldMetadata.Ordinal out of range of Fields.Count"); Debug.Assert(lastOrdinal < field.Ordinal, "FieldMetadata.Ordinal is not increasing"); lastOrdinal = field.Ordinal; Properties[i] = new PlanEdmProperty(lastOrdinal, mapping.GetPropertyMap(field.FieldType.Name).ClrProperty); } }
/// <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; } }
// This method should be called inside a lock unless it is being called from the constructor. private ObjectTypeMapping AddInternalMapping( ObjectTypeMapping objectMap, Dictionary<string, int> clrTypeIndexes, Dictionary<string, int> edmTypeIndexes) { if (Source.ContainsIdentity(objectMap.Identity)) { return (ObjectTypeMapping)Source[objectMap.Identity]; } objectMap.DataSpace = DataSpace.OCSpace; var currIndex = Count; AddInternal(objectMap); var clrName = objectMap.ClrType.Identity; if (!clrTypeIndexes.ContainsKey(clrName)) { clrTypeIndexes.Add(clrName, currIndex); } var edmName = objectMap.EdmType.Identity; if (!edmTypeIndexes.ContainsKey(edmName)) { edmTypeIndexes.Add(edmName, currIndex); } return objectMap; }
/// <summary> /// Loads Association Type Mapping /// </summary> /// <param name="objectMapping"></param> /// <param name="edmType"></param> /// <param name="objectType"></param> /// <param name="ocItemCollection"></param> /// <param name="typeMappings"></param> private static void LoadAssociationTypeMapping( ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary<string, ObjectTypeMapping> typeMappings) { Debug.Assert( edmType.BuiltInTypeKind == BuiltInTypeKind.AssociationType, "Expected Type Encountered in LoadAssociationTypeMapping"); Debug.Assert( (edmType.BuiltInTypeKind == objectType.BuiltInTypeKind), "The BuiltInTypeKind must be same in LoadAssociationTypeMapping"); var association = (AssociationType)edmType; var objectAssociation = (AssociationType)objectType; foreach (var edmEnd in association.AssociationEndMembers) { var objectEnd = (AssociationEndMember)GetObjectMember(edmEnd, objectAssociation); ValidateMembersMatch(edmEnd, objectEnd); if (edmEnd.RelationshipMultiplicity != objectEnd.RelationshipMultiplicity) { throw new MappingException( Strings.Mapping_Default_OCMapping_MultiplicityMismatch( edmEnd.RelationshipMultiplicity, edmEnd.Name, association.FullName, objectEnd.RelationshipMultiplicity, objectEnd.Name, objectAssociation.FullName)); } Debug.Assert(edmEnd.TypeUsage.EdmType.BuiltInTypeKind == BuiltInTypeKind.RefType, "Ends must be of Ref type"); // GetMap for the entity types for the ends of the relationship type to make sure // the entity type mentioned are valid LoadTypeMapping( ((RefType)edmEnd.TypeUsage.EdmType).ElementType, ((RefType)objectEnd.TypeUsage.EdmType).ElementType, ocItemCollection, typeMappings); objectMapping.AddMemberMap(new ObjectAssociationEndMapping(edmEnd, objectEnd)); } }
private bool ContainsMap(GlobalItem cspaceItem, out ObjectTypeMapping map) { Debug.Assert(cspaceItem.DataSpace == DataSpace.CSpace, "ContainsMap: It must be a CSpace item"); int index; if (cdmTypeIndexes.TryGetValue(cspaceItem.Identity, out index)) { map = (ObjectTypeMapping)this[index]; return true; } map = null; return false; }
public void Executes_in_a_transaction_using_ExecutionStrategy() { var shaperMock = MockHelper.CreateShaperMock<object>(); shaperMock.Setup(m => m.GetEnumerator()).Returns( () => new DbEnumeratorShim<object>(((IEnumerable<object>)new[] { new object() }).GetEnumerator())); var objectResultMock = new Mock<ObjectResult<object>>(shaperMock.Object, null, null) { CallBase = true }; var entityType = new EntityType( "FakeEntityType", "FakeNamespace", DataSpace.CSpace, new[] { "key" }, new EdmMember[] { new EdmProperty("key") }); var entitySet = new EntitySet("FakeSet", "FakeSchema", "FakeTable", null, entityType); var entityContainer = new EntityContainer("FakeContainer", DataSpace.CSpace); entityContainer.AddEntitySetBase(entitySet); entitySet.ChangeEntityContainerWithoutCollectionFixup(entityContainer); entitySet.SetReadOnly(); var model = new EdmModel(DataSpace.CSpace); var omicMock = new Mock<DefaultObjectMappingItemCollection>(new EdmItemCollection(model), new ObjectItemCollection()) { CallBase = true }; var objectTypeMapping = new ObjectTypeMapping(entityType, entityType); objectTypeMapping.AddMemberMap( new ObjectPropertyMapping((EdmProperty)entityType.Members.First(), (EdmProperty)entityType.Members.First())); omicMock.Setup(m => m.GetMap(It.IsAny<GlobalItem>())).Returns(objectTypeMapping); var metadataWorkspaceMock = new Mock<MetadataWorkspace> { CallBase = true }; metadataWorkspaceMock.Setup(m => m.IsItemCollectionAlreadyRegistered(DataSpace.OSpace)).Returns(true); metadataWorkspaceMock.Setup(m => m.IsItemCollectionAlreadyRegistered(DataSpace.SSpace)).Returns(true); metadataWorkspaceMock.Setup(m => m.GetEntityContainer(It.IsAny<string>(), It.IsAny<DataSpace>())) .Returns(entityContainer); metadataWorkspaceMock.Setup(m => m.TryGetEntityContainer(It.IsAny<string>(), It.IsAny<DataSpace>(), out entityContainer)) .Returns(true); var storeItemCollection = new StoreItemCollection( GenericProviderFactory<DbProviderFactory>.Instance, new SqlProviderManifest("2008"), "System.Data.FakeSqlClient", "2008"); metadataWorkspaceMock.Setup(m => m.GetItemCollection(DataSpace.SSpace)).Returns(storeItemCollection); #pragma warning disable 618 metadataWorkspaceMock.Object.RegisterItemCollection(omicMock.Object); #pragma warning restore 618 var objectStateManagerMock = new Mock<ObjectStateManager>(metadataWorkspaceMock.Object) { CallBase = true }; var entityKey = new EntityKey(entitySet, "keyValue"); var entityEntry = objectStateManagerMock.Object.AddKeyEntry(entityKey, entitySet); var objectContextMock = Mock.Get( CreateObjectContext( metadataWorkspaceMock: metadataWorkspaceMock, objectStateManagerMock: objectStateManagerMock)); var entityWrapperMock = new Mock<IEntityWrapper>(); entityWrapperMock.Setup(m => m.EntityKey).Returns(entityKey); var entityWrapperFactoryMock = new Mock<EntityWrapperFactory>(); entityWrapperFactoryMock.Setup( m => m.WrapEntityUsingStateManagerGettingEntry( It.IsAny<object>(), It.IsAny<ObjectStateManager>(), out entityEntry)) .Returns(entityWrapperMock.Object); objectContextMock.Setup(m => m.EntityWrapperFactory).Returns(entityWrapperFactoryMock.Object); var executionPlanMock = new Mock<ObjectQueryExecutionPlan>( MockBehavior.Loose, null, null, null, MergeOption.NoTracking, false, null, null); executionPlanMock.Setup( m => m.Execute<object>(It.IsAny<ObjectContext>(), It.IsAny<ObjectParameterCollection>())) .Returns(objectResultMock.Object); objectContextMock.Setup( m => m.PrepareRefreshQuery(It.IsAny<RefreshMode>(), It.IsAny<EntitySet>(), It.IsAny<List<EntityKey>>(), It.IsAny<int>())) .Returns(new Tuple<ObjectQueryExecutionPlan, int>(executionPlanMock.Object, 1)); // Verify that ExecuteInTransaction calls ObjectQueryExecutionPlan.Execute objectContextMock.Setup( m => m.ExecuteInTransaction(It.IsAny<Func<ObjectResult<object>>>(), It.IsAny<IDbExecutionStrategy>(), It.IsAny<bool>(), It.IsAny<bool>())) .Returns<Func<ObjectResult<object>>, IDbExecutionStrategy, bool, bool>( (f, t, s, r) => { executionPlanMock.Verify( m => m.Execute<object>(It.IsAny<ObjectContext>(), It.IsAny<ObjectParameterCollection>()), Times.Never()); var result = f(); executionPlanMock.Verify( m => m.Execute<object>(It.IsAny<ObjectContext>(), It.IsAny<ObjectParameterCollection>()), Times.Once()); return result; }); // Verify that ExecutionStrategy.Execute calls ExecuteInTransaction var executionStrategyMock = new Mock<IDbExecutionStrategy>(); executionStrategyMock.Setup(m => m.Execute(It.IsAny<Func<ObjectResult<object>>>())) .Returns<Func<ObjectResult<object>>>( f => { objectContextMock.Verify( m => m.ExecuteInTransaction( It.IsAny<Func<ObjectResult<object>>>(), It.IsAny<IDbExecutionStrategy>(), false, true), Times.Never()); var result = f(); objectContextMock.Verify( m => m.ExecuteInTransaction( It.IsAny<Func<ObjectResult<object>>>(), It.IsAny<IDbExecutionStrategy>(), false, true), Times.Once()); return result; }); MutableResolver.AddResolver<Func<IDbExecutionStrategy>>(key => (Func<IDbExecutionStrategy>)(() => executionStrategyMock.Object)); try { objectContextMock.Object.Refresh(RefreshMode.StoreWins, new object()); } finally { MutableResolver.ClearResolvers(); } // Finally verify that ExecutionStrategy.Execute was called executionStrategyMock.Verify(m => m.Execute(It.IsAny<Func<ObjectResult<object>>>()), Times.Once()); }