Datei: NDO.Mapping/NDO.Mapping/FieldMap.cs
Last Commit (6f29331)
| 1 | // |
| 2 | // Copyright (c) 2002-2024 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | |
| 23 | using System; |
| 24 | using System.Reflection; |
| 25 | using System.Linq; |
| 26 | using System.Collections.Generic; |
| 27 | |
| 28 | namespace NDO.Mapping |
| 29 | { |
| 30 | ····/// <summary> |
| 31 | ····/// Collects field information for persistent types. This class is used by the NDO Enhancer. |
| 32 | ····/// </summary> |
| 33 | ····public class FieldMap |
| 34 | ····{ |
| 35 | ········Class cl; |
| 36 | ········Type type; |
| 37 | ········List<string>····myFields; |
| 38 | ········List<string>····myEmbeddedTypes; |
| 39 | ········List<FieldInfo> myRelations; |
| 40 | |
| 41 | ········bool checkIfMappingsExist = true; |
| 42 | |
| 43 | ········// used by NDOPersistenceHandler |
| 44 | ········Dictionary<string, MemberInfo> myPersistentFields; |
| 45 | |
| 46 | ········/// <summary> |
| 47 | ········/// Gets all persistent fields. |
| 48 | ········/// </summary> |
| 49 | ········public Dictionary<string,MemberInfo> PersistentFields |
| 50 | ········{ |
| 51 | ············get |
| 52 | ············{ |
| 53 | ················GenerateFields(); |
| 54 | ················return myPersistentFields; |
| 55 | ············} |
| 56 | ········} |
| 57 | |
| 58 | ········/// <summary> |
| 59 | ········/// Gets all Embedded Types. |
| 60 | ········/// </summary> |
| 61 | ········public IEnumerable<string> EmbeddedTypes |
| 62 | ········{ |
| 63 | ············get { return myEmbeddedTypes; } |
| 64 | ········} |
| 65 | |
| 66 | |
| 67 | ········/// <summary> |
| 68 | ········/// Constructor used by NDO and the Enhancer. No sanity check. |
| 69 | ········/// </summary> |
| 70 | ········/// <param name="cl"></param> |
| 71 | ········public FieldMap(Class cl) |
| 72 | ········{ |
| 73 | ············this.cl = cl; |
| 74 | ············this.type = cl.SystemType; |
| 75 | ········} |
| 76 | |
| 77 | ········/// <summary> |
| 78 | ········/// Constructor used by NDO. |
| 79 | ········/// </summary> |
| 80 | ········/// <param name="cl"></param> |
| 81 | ········/// <param name="checkIfMappingsExist"></param> |
| 82 | ········public FieldMap(Class cl, bool checkIfMappingsExist) : this(cl) |
| 83 | ········{ |
| 84 | ············this.checkIfMappingsExist = checkIfMappingsExist; |
| 85 | ········} |
| 86 | |
| 87 | ········/// <summary> |
| 88 | ········/// Constructor used by NDO. |
| 89 | ········/// </summary> |
| 90 | ········/// <param name="t"></param> |
| 91 | ········public FieldMap(Type t) |
| 92 | ········{ |
| 93 | ············this.type = t; |
| 94 | ············this.cl = null; |
| 95 | ············this.checkIfMappingsExist = false; |
| 96 | ········} |
| 97 | |
| 98 | ········private void AddValueType(FieldInfo parent) |
| 99 | ········{ |
| 100 | ············Type t = parent.FieldType; |
| 101 | |
| 102 | ············List<FieldInfo> publicFields = new List<FieldInfo>(); |
| 103 | ············List<PropertyInfo> publicProps = new List<PropertyInfo>(); |
| 104 | |
| 105 | ············PropertyInfo[] pis = t.GetProperties(BindingFlags.Public | BindingFlags.Instance); |
| 106 | ············foreach(PropertyInfo pi in pis) |
| 107 | ············{ |
| 108 | ················if (pi.CanRead && pi.CanWrite && StorableTypes.Contains(pi.PropertyType)) |
| 109 | ················{ |
| 110 | ····················publicProps.Add(pi); |
| 111 | ················} |
| 112 | ············} |
| 113 | ············FieldInfo[] fis = t.GetFields(BindingFlags.Public | BindingFlags.Instance); |
| 114 | ············foreach(FieldInfo fi in fis) |
| 115 | ············{ |
| 116 | ················if (StorableTypes.Contains(fi.FieldType)) |
| 117 | ················{ |
| 118 | ····················publicFields.Add(fi); |
| 119 | ················} |
| 120 | ············} |
| 121 | ············if (publicProps.Count > 0 || publicFields.Count > 0) |
| 122 | ············{ |
| 123 | ················foreach (PropertyInfo pi in publicProps) |
| 124 | ················{ |
| 125 | ····················myFields.Add (parent.Name + "." + pi.Name); |
| 126 | ····················if (!myPersistentFields.ContainsKey(parent.Name + "." + pi.Name)) |
| 127 | ························myPersistentFields.Add(parent.Name + "." + pi.Name, pi); |
| 128 | ················} |
| 129 | ················foreach (FieldInfo fi in publicFields) |
| 130 | ················{ |
| 131 | ····················myFields.Add (parent.Name + "." + fi.Name); |
| 132 | ····················if (!myPersistentFields.ContainsKey(parent.Name + "." + fi.Name)) |
| 133 | ························myPersistentFields.Add(parent.Name + "." + fi.Name, fi); |
| 134 | ················} |
| 135 | ············} |
| 136 | ········} |
| 137 | |
| 138 | |
| 139 | ········private void AddEmbeddedType(FieldInfo parent) |
| 140 | ········{ |
| 141 | ············FieldInfo[] fis = parent.FieldType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); |
| 142 | ············foreach(FieldInfo fi in fis) |
| 143 | ············{ |
| 144 | ················if (fi.Name.StartsWith("_ndo")) |
| 145 | ····················continue; |
| 146 | ················Type t = fi.FieldType; |
| 147 | ················object[] attributes = fi.GetCustomAttributes(typeof(NDOTransientAttribute), false); |
| 148 | ················if (attributes.Length > 0) |
| 149 | ····················continue; |
| 150 | ················if (StorableTypes.Contains(t)) |
| 151 | ················{ |
| 152 | ····················string name = parent.Name + "." + fi.Name; |
| 153 | ····················myEmbeddedTypes.Add(name); |
| 154 | ····················if (!myPersistentFields.ContainsKey(name)) |
| 155 | ························myPersistentFields.Add(name, fi); |
| 156 | ················} |
| 157 | ············} |
| 158 | ········} |
| 159 | |
| 160 | ········private bool IsPersistentType(Type t) |
| 161 | ········{ |
| 162 | ············var attrs = t.GetCustomAttributes(); |
| 163 | ············return attrs.Any( a => a.GetType().Name == "NDOPersistentAttribute" ); |
| 164 | ········} |
| 165 | |
| 166 | ········private void AddFields(Type t) |
| 167 | ········{ |
| 168 | ············int startind = myFields.Count; |
| 169 | ············if (!IsPersistentType(t)) |
| 170 | ················return; |
| 171 | ············FieldInfo[] finfos = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); |
| 172 | ············foreach(FieldInfo fi in finfos) |
| 173 | ············{ |
| 174 | ················string fname; |
| 175 | ················if ((fname = fi.Name).StartsWith("_ndo")) |
| 176 | ····················continue; |
| 177 | |
| 178 | ················if (!fi.IsPrivate) |
| 179 | ····················continue; |
| 180 | |
| 181 | ················if (fi.IsInitOnly) |
| 182 | ····················continue; |
| 183 | |
| 184 | ················object[] attributes = fi.GetCustomAttributes(false); |
| 185 | ················bool cont = false; |
| 186 | ················foreach (System.Attribute attr in attributes) |
| 187 | ················{ |
| 188 | ····················string name; |
| 189 | ····················if ((name = attr.GetType().Name) == "NDOTransientAttribute") |
| 190 | ························cont = true; |
| 191 | ····················if (name == "NDORelationAttribute") |
| 192 | ························cont = true; |
| 193 | ················} |
| 194 | ················if (cont) |
| 195 | ····················continue; |
| 196 | |
| 197 | ················Type ft = fi.FieldType; |
| 198 | |
| 199 | ················// Typen, die nicht speicherbar sind. |
| 200 | ················if (ft.IsInterface) |
| 201 | ····················continue; |
| 202 | |
| 203 | ················if (StorableTypes.Contains(ft)) |
| 204 | ················{ |
| 205 | ····················this.myFields.Add(fname); |
| 206 | ····················if (!myPersistentFields.ContainsKey(fname)) |
| 207 | ························myPersistentFields.Add(fname, fi); |
| 208 | ················} |
| 209 | ················else if (ft.IsValueType) |
| 210 | ····················AddValueType(fi); |
| 211 | ················else if (ft.IsClass) |
| 212 | ················{ |
| 213 | ····················AddEmbeddedType(fi); |
| 214 | ················} |
| 215 | ················// Alle anderen Fälle werden ignoriert |
| 216 | ············} |
| 217 | ············// Pro Klasse werden die Strings sortiert |
| 218 | ············// Sortiert wird ohne Länderberücksichtigung, sonst sortiert die |
| 219 | ············// Anwendung auf norwegischen Systemen anders als auf deutschen. |
| 220 | ············if (myFields.Count - startind > 0) |
| 221 | ············{ |
| 222 | ················FieldSorter fs = new FieldSorter(); |
| 223 | ················myFields.Sort(startind, myFields.Count - startind, fs); |
| 224 | ············} |
| 225 | ········} |
| 226 | |
| 227 | ········private class FieldSorter : IComparer<string> |
| 228 | ········{ |
| 229 | ············public int Compare(string x, string y) |
| 230 | ············{ |
| 231 | ················return String.CompareOrdinal((string) x, (string) y); |
| 232 | ············} |
| 233 | ········}········ |
| 234 | |
| 235 | ········private void GenerateFields() |
| 236 | ········{ |
| 237 | ············myPersistentFields = new Dictionary<string,MemberInfo>(); |
| 238 | ············myFields = new List<string>(); |
| 239 | ············this.myEmbeddedTypes = new List<string>(); |
| 240 | ············AddFields(this.type); |
| 241 | ············Type t = this.type.BaseType; |
| 242 | int persCount = t. GetCustomAttributes( typeof( NDOPersistentAttribute) , false) . Length; |
| 243 | while ( persCount > 0) |
| 244 | ············{ |
| 245 | ················AddFields(t);················ |
| 246 | ················t = t.BaseType; |
| 247 | ················persCount = t.GetCustomAttributes(typeof(NDOPersistentAttribute), false).Length; |
| 248 | ············} |
| 249 | // Jetzt stehen alle Felder in myFields |
| 250 | |
| 251 | ············if (checkIfMappingsExist && !this.type.IsAbstract) |
| 252 | ············{ |
| 253 | ················for (int i = 0; i < myFields.Count; i++) |
| 254 | ················{ |
| 255 | ····················string fieldName = myFields[i]; |
| 256 | ····················NDO.Mapping.Field field; |
| 257 | ····················if ((field = cl.FindField(fieldName)) != null) |
| 258 | ························myFields[i] = field.Column.Name; |
| 259 | ····················else |
| 260 | ························throw new NDOException(7, "Can't find mapping information for field " + cl.FullName + "." + myFields[i]); |
| 261 | ················} |
| 262 | ············} |
| 263 | ········} |
| 264 | |
| 265 | ········/// <summary> |
| 266 | ········/// Gets all field names. |
| 267 | ········/// </summary> |
| 268 | ········public string[] Fields |
| 269 | ········{ |
| 270 | ············get |
| 271 | ············{ |
| 272 | ················// Wir suchen alle Fields, für die ein Mapping existieren muss. |
| 273 | ················// wir sortieren sie nach Field-Namen. |
| 274 | ················// Dann ermitteln wir die Mapping-Namen, die dann ins Array an die gleiche |
| 275 | ················// Stelle eingetragen werden. |
| 276 | ················GenerateFields(); |
| 277 | ················string[] newArr = new string[myFields.Count]; |
| 278 | ················int j = 0; |
| 279 | ················foreach(string s in myFields) |
| 280 | ····················newArr[j++] = s; |
| 281 | ················return newArr; |
| 282 | ············} // get |
| 283 | ········} // Fields |
| 284 | |
| 285 | ········private void AddRelations(Type t) |
| 286 | ········{ |
| 287 | ············var fieldInfos = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic).Where(fi => !fi.IsInitOnly); |
| 288 | ············foreach(FieldInfo fi in fieldInfos) |
| 289 | ············{ |
| 290 | ················if (fi.GetCustomAttributes(typeof(NDORelationAttribute), false).Length > 0) |
| 291 | ····················myRelations.Add(fi); |
| 292 | ············} |
| 293 | ········} |
| 294 | |
| 295 | ········private void GenerateRelations() |
| 296 | ········{ |
| 297 | ············myRelations = new List<FieldInfo>(); |
| 298 | ············AddRelations(this.type); |
| 299 | ············Type t = this.type.BaseType; |
| 300 | int persCount = t. GetCustomAttributes( typeof( NDOPersistentAttribute) , false) . Length; |
| 301 | ············while (persCount > 0) |
| 302 | ············{ |
| 303 | ················if (t.GetCustomAttributes(typeof(NDOPersistentAttribute), false).Length > 0) |
| 304 | ····················AddRelations(t);················ |
| 305 | ················t = t.BaseType; |
| 306 | ················persCount = t.GetCustomAttributes(typeof(NDOPersistentAttribute), false).Length; |
| 307 | ············} |
| 308 | // Jetzt stehen alle Relations in myRelations |
| 309 | |
| 310 | ············if (checkIfMappingsExist && !this.type.IsAbstract) |
| 311 | ············{ |
| 312 | ················for (int i = 0; i < myRelations.Count; i++) |
| 313 | ················{ |
| 314 | ····················if (cl.FindRelation(((FieldInfo)myRelations[i]).Name) == null) |
| 315 | ························throw new NDOException(8, "Can't find mapping information for relation " + cl.FullName + "." + myRelations[i] + "."); |
| 316 | ················} |
| 317 | ············} |
| 318 | ········} |
| 319 | |
| 320 | |
| 321 | ········/// <summary> |
| 322 | ········/// Gets all Relations of a class and its persistent subclasses. |
| 323 | ········/// </summary> |
| 324 | ········public IEnumerable<FieldInfo> Relations |
| 325 | ········{ |
| 326 | ············get |
| 327 | ············{ |
| 328 | ················if (myRelations == null) |
| 329 | ····················GenerateRelations(); |
| 330 | ················return myRelations; |
| 331 | ············} |
| 332 | ········} |
| 333 | |
| 334 | |
| 335 | ····}··// class FieldMap |
| 336 | }··// Namespace |
| 337 |
New Commit (4666cb7)
| 1 | // |
| 2 | // Copyright (c) 2002-2024 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | |
| 23 | using System; |
| 24 | using System.Reflection; |
| 25 | using System.Linq; |
| 26 | using System.Collections.Generic; |
| 27 | |
| 28 | namespace NDO.Mapping |
| 29 | { |
| 30 | ····/// <summary> |
| 31 | ····/// Collects field information for persistent types. This class is used by the NDO Enhancer. |
| 32 | ····/// </summary> |
| 33 | ····public class FieldMap |
| 34 | ····{ |
| 35 | ········Class cl; |
| 36 | ········Type type; |
| 37 | ········List<string>····myFields; |
| 38 | ········List<string>····myEmbeddedTypes; |
| 39 | ········List<FieldInfo> myRelations; |
| 40 | |
| 41 | ········bool checkIfMappingsExist = true; |
| 42 | |
| 43 | ········// used by NDOPersistenceHandler |
| 44 | ········Dictionary<string, MemberInfo> myPersistentFields; |
| 45 | |
| 46 | ········/// <summary> |
| 47 | ········/// Gets all persistent fields. |
| 48 | ········/// </summary> |
| 49 | ········public Dictionary<string,MemberInfo> PersistentFields |
| 50 | ········{ |
| 51 | ············get |
| 52 | ············{ |
| 53 | ················GenerateFields(); |
| 54 | ················return myPersistentFields; |
| 55 | ············} |
| 56 | ········} |
| 57 | |
| 58 | ········/// <summary> |
| 59 | ········/// Gets all Embedded Types. |
| 60 | ········/// </summary> |
| 61 | ········public IEnumerable<string> EmbeddedTypes |
| 62 | ········{ |
| 63 | ············get { return myEmbeddedTypes; } |
| 64 | ········} |
| 65 | |
| 66 | |
| 67 | ········/// <summary> |
| 68 | ········/// Constructor used by NDO and the Enhancer. No sanity check. |
| 69 | ········/// </summary> |
| 70 | ········/// <param name="cl"></param> |
| 71 | ········public FieldMap(Class cl) |
| 72 | ········{ |
| 73 | ············this.cl = cl; |
| 74 | ············this.type = cl.SystemType; |
| 75 | ········} |
| 76 | |
| 77 | ········/// <summary> |
| 78 | ········/// Constructor used by NDO. |
| 79 | ········/// </summary> |
| 80 | ········/// <param name="cl"></param> |
| 81 | ········/// <param name="checkIfMappingsExist"></param> |
| 82 | ········public FieldMap(Class cl, bool checkIfMappingsExist) : this(cl) |
| 83 | ········{ |
| 84 | ············this.checkIfMappingsExist = checkIfMappingsExist; |
| 85 | ········} |
| 86 | |
| 87 | ········/// <summary> |
| 88 | ········/// Constructor used by NDO. |
| 89 | ········/// </summary> |
| 90 | ········/// <param name="t"></param> |
| 91 | ········public FieldMap(Type t) |
| 92 | ········{ |
| 93 | ············this.type = t; |
| 94 | ············this.cl = null; |
| 95 | ············this.checkIfMappingsExist = false; |
| 96 | ········} |
| 97 | |
| 98 | ········private void AddValueType(FieldInfo parent) |
| 99 | ········{ |
| 100 | ············Type t = parent.FieldType; |
| 101 | |
| 102 | ············List<FieldInfo> publicFields = new List<FieldInfo>(); |
| 103 | ············List<PropertyInfo> publicProps = new List<PropertyInfo>(); |
| 104 | |
| 105 | ············PropertyInfo[] pis = t.GetProperties(BindingFlags.Public | BindingFlags.Instance); |
| 106 | ············foreach(PropertyInfo pi in pis) |
| 107 | ············{ |
| 108 | ················if (pi.CanRead && pi.CanWrite && StorableTypes.Contains(pi.PropertyType)) |
| 109 | ················{ |
| 110 | ····················publicProps.Add(pi); |
| 111 | ················} |
| 112 | ············} |
| 113 | ············FieldInfo[] fis = t.GetFields(BindingFlags.Public | BindingFlags.Instance); |
| 114 | ············foreach(FieldInfo fi in fis) |
| 115 | ············{ |
| 116 | ················if (StorableTypes.Contains(fi.FieldType)) |
| 117 | ················{ |
| 118 | ····················publicFields.Add(fi); |
| 119 | ················} |
| 120 | ············} |
| 121 | ············if (publicProps.Count > 0 || publicFields.Count > 0) |
| 122 | ············{ |
| 123 | ················foreach (PropertyInfo pi in publicProps) |
| 124 | ················{ |
| 125 | ····················myFields.Add (parent.Name + "." + pi.Name); |
| 126 | ····················if (!myPersistentFields.ContainsKey(parent.Name + "." + pi.Name)) |
| 127 | ························myPersistentFields.Add(parent.Name + "." + pi.Name, pi); |
| 128 | ················} |
| 129 | ················foreach (FieldInfo fi in publicFields) |
| 130 | ················{ |
| 131 | ····················myFields.Add (parent.Name + "." + fi.Name); |
| 132 | ····················if (!myPersistentFields.ContainsKey(parent.Name + "." + fi.Name)) |
| 133 | ························myPersistentFields.Add(parent.Name + "." + fi.Name, fi); |
| 134 | ················} |
| 135 | ············} |
| 136 | ········} |
| 137 | |
| 138 | |
| 139 | ········private void AddEmbeddedType(FieldInfo parent) |
| 140 | ········{ |
| 141 | ············FieldInfo[] fis = parent.FieldType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); |
| 142 | ············foreach(FieldInfo fi in fis) |
| 143 | ············{ |
| 144 | ················if (fi.Name.StartsWith("_ndo")) |
| 145 | ····················continue; |
| 146 | ················Type t = fi.FieldType; |
| 147 | ················object[] attributes = fi.GetCustomAttributes(typeof(NDOTransientAttribute), false); |
| 148 | ················if (attributes.Length > 0) |
| 149 | ····················continue; |
| 150 | ················if (StorableTypes.Contains(t)) |
| 151 | ················{ |
| 152 | ····················string name = parent.Name + "." + fi.Name; |
| 153 | ····················myEmbeddedTypes.Add(name); |
| 154 | ····················if (!myPersistentFields.ContainsKey(name)) |
| 155 | ························myPersistentFields.Add(name, fi); |
| 156 | ················} |
| 157 | ············} |
| 158 | ········} |
| 159 | |
| 160 | ········private bool IsPersistentType(Type t) |
| 161 | ········{ |
| 162 | ············var attrs = t.GetCustomAttributes(); |
| 163 | ············return attrs.Any( a => a.GetType().Name == "NDOPersistentAttribute" ); |
| 164 | ········} |
| 165 | |
| 166 | ········private void AddFields(Type t) |
| 167 | ········{ |
| 168 | ············int startind = myFields.Count; |
| 169 | ············if (!IsPersistentType(t)) |
| 170 | ················return; |
| 171 | ············FieldInfo[] finfos = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); |
| 172 | ············foreach(FieldInfo fi in finfos) |
| 173 | ············{ |
| 174 | ················string fname; |
| 175 | ················if ((fname = fi.Name).StartsWith("_ndo")) |
| 176 | ····················continue; |
| 177 | |
| 178 | ················if (!fi.IsPrivate) |
| 179 | ····················continue; |
| 180 | |
| 181 | ················if (fi.IsInitOnly) |
| 182 | ····················continue; |
| 183 | |
| 184 | ················object[] attributes = fi.GetCustomAttributes(false); |
| 185 | ················bool cont = false; |
| 186 | ················foreach (System.Attribute attr in attributes) |
| 187 | ················{ |
| 188 | ····················string name; |
| 189 | ····················if ((name = attr.GetType().Name) == "NDOTransientAttribute") |
| 190 | ························cont = true; |
| 191 | ····················if (name == "NDORelationAttribute") |
| 192 | ························cont = true; |
| 193 | ················} |
| 194 | ················if (cont) |
| 195 | ····················continue; |
| 196 | |
| 197 | ················Type ft = fi.FieldType; |
| 198 | |
| 199 | ················// Typen, die nicht speicherbar sind. |
| 200 | ················if (ft.IsInterface) |
| 201 | ····················continue; |
| 202 | |
| 203 | ················if (StorableTypes.Contains(ft)) |
| 204 | ················{ |
| 205 | ····················this.myFields.Add(fname); |
| 206 | ····················if (!myPersistentFields.ContainsKey(fname)) |
| 207 | ························myPersistentFields.Add(fname, fi); |
| 208 | ················} |
| 209 | ················else if (ft.IsValueType) |
| 210 | ····················AddValueType(fi); |
| 211 | ················else if (ft.IsClass) |
| 212 | ················{ |
| 213 | ····················AddEmbeddedType(fi); |
| 214 | ················} |
| 215 | ················// Alle anderen Fälle werden ignoriert |
| 216 | ············} |
| 217 | ············// Pro Klasse werden die Strings sortiert |
| 218 | ············// Sortiert wird ohne Länderberücksichtigung, sonst sortiert die |
| 219 | ············// Anwendung auf norwegischen Systemen anders als auf deutschen. |
| 220 | ············if (myFields.Count - startind > 0) |
| 221 | ············{ |
| 222 | ················FieldSorter fs = new FieldSorter(); |
| 223 | ················myFields.Sort(startind, myFields.Count - startind, fs); |
| 224 | ············} |
| 225 | ········} |
| 226 | |
| 227 | ········private class FieldSorter : IComparer<string> |
| 228 | ········{ |
| 229 | ············public int Compare(string x, string y) |
| 230 | ············{ |
| 231 | ················return String.CompareOrdinal((string) x, (string) y); |
| 232 | ············} |
| 233 | ········}········ |
| 234 | |
| 235 | ········private void GenerateFields() |
| 236 | ········{ |
| 237 | ············myPersistentFields = new Dictionary<string,MemberInfo>(); |
| 238 | ············myFields = new List<string>(); |
| 239 | ············this.myEmbeddedTypes = new List<string>(); |
| 240 | ············AddFields(this.type); |
| 241 | ············Type t = this.type.BaseType; |
| 242 | |
| 243 | while ( IsPersistentType( t ) ) |
| 244 | ············{ |
| 245 | ················AddFields(t);················ |
| 246 | ················t = t.BaseType;················ |
| 247 | ············} |
| 248 | // Now all fields are in myFields |
| 249 | |
| 250 | ············if (checkIfMappingsExist && !this.type.IsAbstract) |
| 251 | ············{ |
| 252 | ················for (int i = 0; i < myFields.Count; i++) |
| 253 | ················{ |
| 254 | ····················string fieldName = myFields[i]; |
| 255 | ····················NDO.Mapping.Field field; |
| 256 | ····················if ((field = cl.FindField(fieldName)) != null) |
| 257 | ························myFields[i] = field.Column.Name; |
| 258 | ····················else |
| 259 | ························throw new NDOException(7, "Can't find mapping information for field " + cl.FullName + "." + myFields[i]); |
| 260 | ················} |
| 261 | ············} |
| 262 | ········} |
| 263 | |
| 264 | ········/// <summary> |
| 265 | ········/// Gets all field names. |
| 266 | ········/// </summary> |
| 267 | ········public string[] Fields |
| 268 | ········{ |
| 269 | ············get |
| 270 | ············{ |
| 271 | ················// Wir suchen alle Fields, für die ein Mapping existieren muss. |
| 272 | ················// wir sortieren sie nach Field-Namen. |
| 273 | ················// Dann ermitteln wir die Mapping-Namen, die dann ins Array an die gleiche |
| 274 | ················// Stelle eingetragen werden. |
| 275 | ················GenerateFields(); |
| 276 | ················string[] newArr = new string[myFields.Count]; |
| 277 | ················int j = 0; |
| 278 | ················foreach(string s in myFields) |
| 279 | ····················newArr[j++] = s; |
| 280 | ················return newArr; |
| 281 | ············} // get |
| 282 | ········} // Fields |
| 283 | |
| 284 | ········private void AddRelations(Type t) |
| 285 | ········{ |
| 286 | ············var fieldInfos = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic).Where(fi => !fi.IsInitOnly); |
| 287 | ············foreach(FieldInfo fi in fieldInfos) |
| 288 | ············{ |
| 289 | ················if (fi.GetCustomAttributes(typeof(NDORelationAttribute), false).Length > 0) |
| 290 | ····················myRelations.Add(fi); |
| 291 | ············} |
| 292 | ········} |
| 293 | |
| 294 | ········private void GenerateRelations() |
| 295 | ········{ |
| 296 | ············myRelations = new List<FieldInfo>(); |
| 297 | ············AddRelations(this.type); |
| 298 | ············Type t = this.type.BaseType; |
| 299 | while ( IsPersistentType( t ) ) |
| 300 | ············{ |
| 301 | ················AddRelations(t);················ |
| 302 | ················t = t.BaseType; |
| 303 | ············} |
| 304 | // Now all Relations are in myRelations |
| 305 | |
| 306 | ············if (checkIfMappingsExist && !this.type.IsAbstract) |
| 307 | ············{ |
| 308 | ················for (int i = 0; i < myRelations.Count; i++) |
| 309 | ················{ |
| 310 | ····················if (cl.FindRelation(((FieldInfo)myRelations[i]).Name) == null) |
| 311 | ························throw new NDOException(8, "Can't find mapping information for relation " + cl.FullName + "." + myRelations[i] + "."); |
| 312 | ················} |
| 313 | ············} |
| 314 | ········} |
| 315 | |
| 316 | |
| 317 | ········/// <summary> |
| 318 | ········/// Gets all Relations of a class and its persistent subclasses. |
| 319 | ········/// </summary> |
| 320 | ········public IEnumerable<FieldInfo> Relations |
| 321 | ········{ |
| 322 | ············get |
| 323 | ············{ |
| 324 | ················if (myRelations == null) |
| 325 | ····················GenerateRelations(); |
| 326 | ················return myRelations; |
| 327 | ············} |
| 328 | ········} |
| 329 | |
| 330 | |
| 331 | ····}··// class FieldMap |
| 332 | }··// Namespace |
| 333 |