private void GetRelatedObjects <WPOType>(WPOType obj, Dictionary <string, string> dataRow, List <PropertyInfo> properties, int depth) where WPOType : WPOBaseObject { foreach (PropertyInfo property in properties.Where(prop => prop.GetAttribute <WPORelationAttribute>() != null)) { string columnName = obj.GetColumnName(property); string value = dataRow[columnName]; if (!string.IsNullOrEmpty(value)) { WPORelationAttribute relation = property.GetAttribute <WPORelationAttribute>(); var relatedObject = session.objectsAll.FirstOrDefault(o => o.GetType() == property.PropertyType && o.GetPropertyValueByColumnName(relation.ForeignKey) != null && o.GetPropertyValueByColumnName(relation.ForeignKey).ToString() == value); if (relatedObject == null && depth > 0) { MethodInfo method = GetType().GetMethod("GetObjectWithDependencies", BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(property.PropertyType); relatedObject = (WPOBaseObject)method.Invoke(this, new object[] { relation.ForeignKey, value, depth }); } // Add itself to releted object collection if (relatedObject != null) { if (relation.RelationType == RelationType.OneToMany) { relatedObject.AddToCollection(obj); } else if (relation.RelationType == RelationType.OneToOne) { relatedObject.AddRelation(obj, relation.ForeignKey); } } property.SetValue(obj, relatedObject); } } }
public void LoadDependencies() { if (Status != ObjectStatus.New) { //Get WPOCollections var collectionProperties = GetType().GetProperties().Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(WPOCollection <>)); foreach (var collectionProperty in collectionProperties) { Type childType = collectionProperty.PropertyType.GetGenericArguments()[0]; //Gets the query object query = typeof(WPOManager).GetMethod("GetQuery", BindingFlags.Public | BindingFlags.Instance) .MakeGenericMethod(childType) .Invoke(WPOManager.GetInstance(), new object[] { Session }); //Finds the connected field name string relatedObjColumn = GetColumnName(childType.GetProperties().FirstOrDefault(prop => prop.PropertyType == GetType())); //Set filter query.GetType().GetMethod("Where", BindingFlags.Public | BindingFlags.Instance).Invoke(query, new object[] { relatedObjColumn + " = " + TableObject.PrimaryKey.Value, false }); //Get objects and automatically set references query.GetType().GetMethod("GetObjects", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(query, null); } //Get WPOObjects var relatedObjProperties = GetType().GetProperties().Where(p => p.GetAttribute <WPORelationAttribute>() != null); foreach (var relatedObjProperty in relatedObjProperties) { //Finds the connected field name WPORelationAttribute relation = relatedObjProperty.GetAttribute <WPORelationAttribute>(); if (DataRow.ContainsKey(relation.ColumnName) && !string.IsNullOrEmpty(DataRow[relation.ColumnName])) { //Gets the query object query = typeof(WPOManager).GetMethod("GetQuery", BindingFlags.Public | BindingFlags.Instance) .MakeGenericMethod(relatedObjProperty.PropertyType) .Invoke(WPOManager.GetInstance(), new object[] { Session }); //Set filter query.GetType().GetMethod("Where", BindingFlags.Public | BindingFlags.Instance).Invoke(query, new object[] { relation.ForeignKey + " = " + DataRow[relation.ColumnName], false }); //Get object query.GetType().GetMethod("GetObjects", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(query, null); var queryList = query.GetType().GetProperty("InternalList", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(query); var queryObj = queryList.GetType().GetMethod("get_Item").Invoke(queryList, new object[] { 0 }); relatedObjProperty.SetValue(this, queryObj); queryObj.GetType().GetMethod("AddToCollection", BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(GetType()).Invoke(queryObj, new object[] { this }); } } } }