private async Task DeleteAllRecursiveAsync(Type type, Database.IAsyncConnection connection) { await DeleteAllFromTableAsync(type, connection); var childTypes = type.GetChildRelationTypes(); foreach (var childType in childTypes) { await this.DeleteAllRecursiveAsync(childType, connection); } }
/// <summary> /// Overload of PopulateChildrenRecursive that deals with /// a collection of parents /// </summary> /// <param name="parents"></param> protected async Task PopulateChildrenRecursiveAsync(IEnumerable parents, Database.IAsyncConnection connection) { try { foreach (var parent in parents) { await this.PopulateChildrenRecursiveAsync(parent as IBlueSphereEntity, connection); } } catch (ArgumentOutOfRangeException e) { } }
private async Task DeleteAllFromTableAsync(Type type, Database.IAsyncConnection connection) { string command = string.Format("delete from {0}", type.GetTableName()); await connection.ExecuteAsync(command); }
private async Task <IList> GetChildrenAsync(IBlueSphereEntity parent, Type childType, string childIdentifyingPropertyName, object childIdentifyingPropertyValue, Database.IAsyncConnection connection) { var tableMapping = await connection.GetMappingAsync(childType); string query = string.Format("select * from {0} where {1} = ?", childType.GetTableName(), childType.GetForeignKeyName(parent.GetType())); if (!string.IsNullOrEmpty(childIdentifyingPropertyName)) { query = query + string.Format(" AND {0} = ?", childIdentifyingPropertyName); } IList <object> queryResults; if (!string.IsNullOrEmpty(childIdentifyingPropertyName)) { queryResults = await connection.QueryAsync(CancellationToken.None, tableMapping, query, parent.ID, childIdentifyingPropertyValue); } else { queryResults = await connection.QueryAsync(CancellationToken.None, tableMapping, query, parent.ID); } // Create a typed generic list we can assign back to parent element IList genericList = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(childType)); foreach (object item in queryResults) { genericList.Add(item); } return(genericList); }
/// <summary> /// For the parent entity pulled from a table, populates any children /// as labelled with the ChildRelationship. /// </summary> /// <param name="parent"></param> protected async Task PopulateChildrenRecursiveAsync(IBlueSphereEntity parent, Database.IAsyncConnection connection) { var relationshipProperties = parent.GetType().GetChildRelationProperties(); foreach (var relationshipProperty in relationshipProperties) { Type childType = relationshipProperty.GetTypeOfChildRelation(); string childIdentifyingPropertyName = relationshipProperty.GetIdentifyingPropertyNameOfChildRelation(); object childIdentifyingPropertyValue = relationshipProperty.GetIdentifyingPropertyValueOfChildRelation(); IList children = await this.GetChildrenAsync(parent, childType, childIdentifyingPropertyName, childIdentifyingPropertyValue, connection); if (relationshipProperty.GetCardinalityOfChildRelation() == RelationshipCardinality.OneToOne) { Debug.Assert(children.Count == 1, string.Format("Expected one child of type {0} for parent {1} '{2}' but found {3}", childType.Name, parent.GetType().Name, parent.ID, children.Count)); relationshipProperty.SetValue(parent, children[0]); } else if (relationshipProperty.GetCardinalityOfChildRelation() == RelationshipCardinality.OneToZeroOrOne) { Debug.Assert(children.Count <= 1, string.Format("Expected zero or one child of type {0} for parent {1} '{2}' but found {3}", childType.Name, parent.GetType().Name, parent.ID, children.Count)); if (children.Count == 1) { relationshipProperty.SetValue(parent, children[0]); } } else { relationshipProperty.SetValue(parent, children); } foreach (var child in children) { await this.PopulateChildrenRecursiveAsync(child as IBlueSphereEntity, connection); } } }