Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Mapping / StorageEntityTypeMapping.cs
1 //---------------------------------------------------------------------
2 // <copyright file="StorageEntityTypeMapping.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner       Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9
10
11 using System;
12 using System.Collections.Generic;
13 using System.Collections.ObjectModel;
14 using System.Text;
15 using System.Data.Metadata.Edm;
16
17 namespace System.Data.Mapping {
18     /// <summary>
19     /// Mapping metadata for Entity type.
20     /// If an EntitySet represents entities of more than one type, than we will have
21     /// more than one EntityTypeMapping for an EntitySet( For ex : if
22     /// PersonSet Entity extent represents entities of types Person and Customer,
23     /// than we will have two EntityType Mappings under mapping for PersonSet).
24     /// </summary>
25     /// <example>
26     /// For Example if conceptually you could represent the CS MSL file as following
27     /// --Mapping 
28     ///   --EntityContainerMapping ( CNorthwind-->SNorthwind )
29     ///     --EntitySetMapping
30     ///       --EntityTypeMapping
31     ///         --MappingFragment
32     ///           --EntityKey
33     ///             --ScalarPropertyMap
34     ///           --ScalarPropertyMap
35     ///       --EntityTypeMapping
36     ///         --MappingFragment
37     ///           --EntityKey
38     ///             --ScalarPropertyMap
39     ///           --ComplexPropertyMap
40     ///             --ScalarPropertyMap
41     ///             --ScalarProperyMap
42     ///           --ScalarPropertyMap
43     ///     --AssociationSetMapping 
44     ///       --AssociationTypeMapping
45     ///         --MappingFragment
46     ///           --EndPropertyMap
47     ///             --ScalarPropertyMap
48     ///             --ScalarProperyMap
49     ///           --EndPropertyMap
50     ///             --ScalarPropertyMap
51     /// This class represents the metadata for all entity Type map elements in the 
52     /// above example. Users can access the table mapping fragments under the 
53     /// entity type mapping through this class.
54     /// </example>
55
56     internal class StorageEntityTypeMapping : StorageTypeMapping {
57         #region Constructors
58         /// <summary>
59         /// Construct the new EntityTypeMapping object.
60         /// </summary>
61         /// <param name="setMapping">Set Mapping that contains this Type mapping </param>
62         internal StorageEntityTypeMapping(StorageSetMapping setMapping)
63             : base(setMapping) {
64         }
65         #endregion
66
67         #region Fields
68         /// <summary>
69         /// Types for which the mapping holds true for.
70         /// </summary>
71         private Dictionary<string, EdmType> m_entityTypes = new Dictionary<string, EdmType>(StringComparer.Ordinal);
72         /// <summary>
73         /// Types for which the mapping holds true for not only the type specified but the sub-types of that type as well.
74         /// </summary>
75         private Dictionary<string, EdmType> m_isOfEntityTypes = new Dictionary<string, EdmType>(StringComparer.Ordinal);
76         #endregion
77
78         #region Properties
79         /// <summary>
80         /// a list of TypeMetadata that this mapping holds true for.
81         /// </summary>
82         internal override ReadOnlyCollection<EdmType> Types {
83             get {
84                 return new List<EdmType>(m_entityTypes.Values).AsReadOnly();
85             }
86         }
87
88         /// <summary>
89         /// a list of TypeMetadatas for which the mapping holds true for
90         /// not only the type specified but the sub-types of that type as well.        
91         /// </summary>
92         internal override ReadOnlyCollection<EdmType> IsOfTypes {
93             get {
94                 return new List<EdmType>(m_isOfEntityTypes.Values).AsReadOnly();
95             }
96         }
97         #endregion
98
99         #region Methods
100         /// <summary>
101         /// Add a Type to the list of types that this mapping is valid for
102         /// </summary>
103         internal void AddType(EdmType type) {
104             this.m_entityTypes.Add(type.FullName, type);
105         }
106
107         /// <summary>
108         /// Add a Type to the list of Is-Of types that this mapping is valid for
109         /// </summary>
110         internal void AddIsOfType(EdmType type) {
111             this.m_isOfEntityTypes.Add(type.FullName, type);
112         }
113
114         internal EntityType GetContainerType(string memberName) {
115             foreach (EntityType type in m_entityTypes.Values) {
116                 if (type.Properties.Contains(memberName))
117                 {
118                     return type;
119                 }
120             }
121
122             foreach (EntityType type in m_isOfEntityTypes.Values)
123             {
124                 if (type.Properties.Contains(memberName))
125                 {
126                     return type;
127                 }
128             }
129             return null;
130         }
131
132
133         /// <summary>
134         /// This method is primarily for debugging purposes.
135         /// Will be removed shortly.
136         /// </summary>
137         /// <param name="index"></param>
138         internal override void Print(int index) {
139             StorageEntityContainerMapping.GetPrettyPrintString(ref index);
140             StringBuilder sb = new StringBuilder();
141             sb.Append("EntityTypeMapping");
142             sb.Append("   ");
143             foreach (EdmType type in m_entityTypes.Values) {
144                 sb.Append("Types:");
145                 sb.Append(type.FullName);
146                 sb.Append("   ");
147             }
148             foreach (EdmType type in m_isOfEntityTypes.Values) {
149                 sb.Append("Is-Of Types:");
150                 sb.Append(type.FullName);
151                 sb.Append("   ");
152             }
153
154             Console.WriteLine(sb.ToString());
155             foreach (StorageMappingFragment fragment in MappingFragments) {
156                 fragment.Print(index + 5);
157             }
158         }
159         #endregion
160     }
161 }