protected void FillRowWithObject(DataObject dataObject, DataRow row) { bool relation = false; Type myType = dataObject.GetType(); row[dataObject.TableName + "_ID"] = dataObject.ObjectId; MemberInfo[] myMembers = myType.GetMembers(); for (int i = 0; i < myMembers.Length; i++) { object[] myAttributes = GetRelationAttributes(myMembers[i]); object val = null; if (myAttributes.Length > 0) { relation = true; } else { myAttributes = myMembers[i].GetCustomAttributes(typeof(DataElement), true); object[] keyAttrib = myMembers[i].GetCustomAttributes(typeof(PrimaryKey), true); if (myAttributes.Length > 0 || keyAttrib.Length > 0) { if (myMembers[i] is PropertyInfo) { val = ((PropertyInfo)myMembers[i]).GetValue(dataObject, null); } if (myMembers[i] is FieldInfo) { val = ((FieldInfo)myMembers[i]).GetValue(dataObject); } if (val != null) { row[myMembers[i].Name] = val; } } } //} } if (relation) { SaveObjectRelations(dataObject); } }
protected DataRow FindRowByKey(DataObject dataObject) { DataRow row; string tableName = dataObject.TableName; DataTable table = GetDataSet(tableName).Tables[tableName]; Type myType = dataObject.GetType(); string key = table.PrimaryKey[0].ColumnName; if (key.Equals(tableName + "_ID")) { row = table.Rows.Find(dataObject.ObjectId); } else { MemberInfo[] keymember = myType.GetMember(key); object val = null; if (keymember[0] is PropertyInfo) { val = ((PropertyInfo)keymember[0]).GetValue(dataObject, null); } if (keymember[0] is FieldInfo) { val = ((FieldInfo)keymember[0]).GetValue(dataObject); } if (val != null) { row = table.Rows.Find(val); } else { return(null); } } return(row); }
protected void FillLazyObjectRelations(DataObject dataObject, bool autoload) { try { var dataObjectType = dataObject.GetType(); MemberInfo[] myMembers; if (!MemberInfoCache.TryGetValue(dataObjectType, out myMembers)) { myMembers = dataObjectType.GetMembers(); MemberInfoCache[dataObjectType] = myMembers; } for (int i = 0; i < myMembers.Length; i++) { Relation[] myAttributes = GetRelationAttributes(myMembers[i]); if (myAttributes.Length > 0) { Relation rel = myAttributes[0]; if ((rel.AutoLoad == false) && autoload) { continue; } bool isArray = false; Type remoteType; DataObject[] elements; string local = rel.LocalField; string remote = rel.RemoteField; if (myMembers[i] is PropertyInfo) { remoteType = ((PropertyInfo)myMembers[i]).PropertyType; } else { remoteType = ((FieldInfo)myMembers[i]).FieldType; } if (remoteType.HasElementType) { remoteType = remoteType.GetElementType(); isArray = true; } PropertyInfo prop = dataObjectType.GetProperty(local); FieldInfo field = dataObjectType.GetField(local); object val = 0; if (prop != null) { val = prop.GetValue(dataObject, null); } if (field != null) { val = field.GetValue(dataObject); } if (val != null && val.ToString() != string.Empty) { if (DataObject.GetPreCachedFlag(remoteType)) { elements = new DataObject[1]; elements[0] = FindObjectByKeyImpl(remoteType, val); } else { elements = SelectObjectsImpl(remoteType, remote + " = '" + Escape(val.ToString()) + "'", Transaction.IsolationLevel.DEFAULT); } if ((elements != null) && (elements.Length > 0)) { if (isArray) { if (myMembers[i] is PropertyInfo) { ((PropertyInfo)myMembers[i]).SetValue(dataObject, elements, null); } if (myMembers[i] is FieldInfo) { var currentField = (FieldInfo)myMembers[i]; ConstructorInfo constructor; if (!ConstructorByFieldType.TryGetValue(currentField.FieldType, out constructor)) { constructor = currentField.FieldType.GetConstructor(new[] { typeof(int) }); ConstructorByFieldType[currentField.FieldType] = constructor; } object elementHolder = constructor.Invoke(new object[] { elements.Length }); var elementArray = (object[])elementHolder; for (int m = 0; m < elementArray.Length; m++) { elementArray[m] = elements[m]; } currentField.SetValue(dataObject, elementArray); } } else { if (myMembers[i] is PropertyInfo) { ((PropertyInfo)myMembers[i]).SetValue(dataObject, elements[0], null); } if (myMembers[i] is FieldInfo) { ((FieldInfo)myMembers[i]).SetValue(dataObject, elements[0]); } } } } } } } catch (Exception e) { throw new DatabaseException("Resolving Relations for " + dataObject.TableName + " failed!", e); } }
protected void DeleteObjectRelations(DataObject dataObject) { try { object val; Type myType = dataObject.GetType(); MemberInfo[] myMembers = myType.GetMembers(); for (int i = 0; i < myMembers.Length; i++) { Relation[] myAttributes = GetRelationAttributes(myMembers[i]); if (myAttributes.Length > 0) { //if(myAttributes[0] is Attributes.Relation) //{ if (myAttributes[0].AutoDelete == false) { continue; } bool array = false; Type type; if (myMembers[i] is PropertyInfo) { type = ((PropertyInfo)myMembers[i]).PropertyType; } else { type = ((FieldInfo)myMembers[i]).FieldType; } if (type.HasElementType) { type = type.GetElementType(); array = true; } val = null; if (array) { if (myMembers[i] is PropertyInfo) { val = ((PropertyInfo)myMembers[i]).GetValue(dataObject, null); } if (myMembers[i] is FieldInfo) { val = ((FieldInfo)myMembers[i]).GetValue(dataObject); } if (val is Array) { var a = val as Array; foreach (object o in a) { if (o is DataObject) { DeleteObject(o as DataObject); } } } else { if (val is DataObject) { DeleteObject(val as DataObject); } } } else { if (myMembers[i] is PropertyInfo) { val = ((PropertyInfo)myMembers[i]).GetValue(dataObject, null); } if (myMembers[i] is FieldInfo) { val = ((FieldInfo)myMembers[i]).GetValue(dataObject); } if (val != null && val is DataObject) { DeleteObject(val as DataObject); } } //} } } } catch (Exception e) { throw new DatabaseException("Resolving Relations failed !", e); } }