public static List <T> Include <T, T1>(this List <T> parentObjectList) where T : new() where T1 : new() { string parentEntityName = typeof(T).Name; string relatedManyEntityName = typeof(T1).Name; PropertyInfo parentEntityIdentityColumn = typeof(T).GetProperties() .Where(x => x.Name.ToLower() == parentEntityName.ToLower() + "id").FirstOrDefault(); if (parentEntityIdentityColumn == null) { throw new Exception(ErrorMessages.IDENTITY_COLUMN_IS_MISSING(parentEntityName)); } foreach (var parentObject in parentObjectList) { IComparable val = (IComparable)parentEntityIdentityColumn.GetValue(parentObject); string query = $"select * from {relatedManyEntityName} where {parentEntityName}id = {val}"; List <T1> includedObject = Executor.GetInstance().Read <T1>(query); Dictionary <string, PropertyInfo> relatedEntityMapping = HotSauceHelpers.GetRelatedEntityNames(typeof(T)); PropertyInfo pi = relatedEntityMapping[relatedManyEntityName]; pi.SetValue(parentObject, includedObject); } return(parentObjectList); }
public void InsertChildren(object parentObject, IComparable parentId) { string parentName = parentObject.GetType().Name.ToLower(); Dictionary <string, PropertyInfo> relatedEntities = HotSauceHelpers.GetRelatedEntityNames(parentObject.GetType()); foreach (KeyValuePair <string, PropertyInfo> relatedEntity in relatedEntities) { IEnumerable childObjects = (IEnumerable)relatedEntity.Value.GetValue(parentObject); if (childObjects == null) { continue; } foreach (object childObject in childObjects) { string parentPropertyIdName = parentName + "id"; BindingFlags bindingFlags = BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance; PropertyInfo parentIdProperty = childObject.GetType().GetProperty(parentPropertyIdName, bindingFlags); if (parentIdProperty == null) { throw new Exception(ErrorMessages.PARENT_ID_COLUMN_MISSING(parentPropertyIdName, childObject.GetType().Name)); } parentIdProperty.SetValue(childObject, parentId); IComparable identity = InsertObject(childObject); InsertChildren(childObject, identity); } } }