public virtual void Insert(IEnumerable <T> entities, Database.IConnection transactionConnection) { var connection = transactionConnection ?? _dataService.GetDBConnection(); foreach (var entity in entities) { this.InsertRecursive(entity, connection); } }
/// <summary> /// Deletes all items of the specified type from their respective tables /// plus any child types as specified by the ChildRelationship attribute /// </summary> /// <param name="type"></param> private void DeleteAllRecursive(Type type, Database.IConnection connection) { this.DeleteAllFromTable(type, connection); var childTypes = type.GetChildRelationTypes(); foreach (var childType in childTypes) { this.DeleteAllRecursive(childType, connection); } }
/// <summary> /// Deletes a potentially nested object graph from the appropriate tables using the /// ChildRelationship and ForeignKey attributes to guide the process /// </summary> /// <param name="entity"></param> private void DeleteRecursive(IBlueSphereEntity entity, Database.IConnection connection) { try { connection.Delete(entity); } catch (Exception ex) { throw new Exception(string.Format("Failed to delete from table {0}, entity ID {1}.", entity.GetType().GetTableName(), entity.ID), ex); } var relationshipProperties = entity.GetType().GetChildRelationProperties(); foreach (var relationshipProperty in relationshipProperties) { this.DoRecursiveTreeAction(entity, relationshipProperty, (parent, child) => { this.DeleteRecursive(child, connection); }); } }
/// <summary> /// Private non-async version of PopulateChildrenRecursiveAsync for use within a transaction /// </summary> private void PopulateChildrenRecursive(IBlueSphereEntity parent, Database.IConnection 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 = this.GetChildren(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) { this.PopulateChildrenRecursive(child as IBlueSphereEntity, connection); } } }
public virtual void Insert(T entity, Database.IConnection transactionConnection) { var connection = transactionConnection ?? _dataService.GetDBConnection(); this.InsertRecursive(entity, connection); }
// Deletes all items from the table associated with the specified type private void DeleteAllFromTable(Type type, Database.IConnection connection) { string command = string.Format("delete from {0}", type.GetTableName()); connection.Execute(command); }
private IList GetChildren(IBlueSphereEntity parent, Type childType, string childIdentifyingPropertyName, object childIdentifyingPropertyValue, Database.IConnection connection) { var tableMapping = connection.GetMapping(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 = connection.Query(tableMapping, query, parent.ID, childIdentifyingPropertyValue); } else { queryResults = connection.Query(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); }