/// <summary> /// Delete Thing entities from the store, including sub entites marked for deletion /// </summary> /// <param name="entitiesBeingHandled">Entities already being deleted further up the delete stack</param> /// <param name="things">The Things to delete</param> /// <param name="toDelete">Entity types to cascade to, if they are loaded</param> internal void Delete(List <Entity> entitiesBeingHandled, Flags toDelete, IEnumerable <Thing> things) { if (things == null) { throw new ArgumentNullException("things"); } Log("Delete", things.Select <Thing, int>(entity => entity.Identity).ToArray <int>(), EntityType.None); // Copy the list of entities being handled, and add this new set of entities to it. // We're handling those now. List <Entity> entitiesNowBeingHandled = new List <Entity>(entitiesBeingHandled); entitiesNowBeingHandled.AddRange(things); // Loop over each entity and delete it. foreach (Thing thing in things) { // Already being deleted higher up the stack? if (entitiesBeingHandled.ContainsEntity(thing)) { continue; } //Allow partial/subclasses to perform additional processing OnBeforeDeleteEntity(thing); // Delete child entities if ((toDelete & EntityType.Widget) == EntityType.Widget && thing.WidgetListPopulated) { foreach (Widget childWidget in thing.WidgetList) { if (!entitiesBeingHandled.ContainsEntity(childWidget)) { WidgetRepo.Delete(entitiesNowBeingHandled, toDelete, childWidget); } } } // Now delete the entity if (this.Mapper.Delete("DeleteThing", thing.Identity) != 1) { ThrowThingEntityException(thing.Identity); } thing.ResetChanged(); //Allow partial/subclasses to perform additional processing OnAfterDeleteEntity(thing); } //Save to the repository updates table if (things.Count() > 0) { RaiseModelChanged(); } }
/// <summary> /// Save (insert/update) a Thing into the store /// </summary> /// <param name="entitiesBeingHandled">Entities already being saved further up the save stack</param> /// <param name="things">The Things to save</param> /// <param name="toSave">Entity types to cascade to, if they are loaded</param> internal void Save(List <Entity> entitiesBeingHandled, Flags toSave, params Thing[] things) { if (things == null) { throw new ArgumentNullException("things"); } Log("Save", things.Select <Thing, int>(entity => entity.Identity).ToArray <int>(), EntityType.None); // Copy the list of entities being handled, and add this new set of entities to it. // We're handling those now. List <Entity> entitiesNowBeingHandled = new List <Entity>(entitiesBeingHandled); entitiesNowBeingHandled.AddRange(things); // Loop over each entity and save it. foreach (Thing thing in things) { // Already being saved higher up the stack? if (entitiesBeingHandled.ContainsEntity(thing)) { continue; } // Allow derived/partial class to do extra work OnBeforeSaveEntity(thing); bool saved = false; try { // Save the entity if (thing.IsNew) { this.Mapper.Insert("InsertThing", thing); saved = true; } else if (thing.IsChanged) { if (this.Mapper.Update("UpdateThing", thing) != 1) { ThrowThingEntityException(thing.Identity); } saved = true; } } catch (Exception ex) { throw EntityLogger.WriteUnexpectedException( ex, "Failed to insert/update Entity", Category.EntityFramework, thing); } // Save child entities... update their key to us if ((toSave & EntityType.Widget) == EntityType.Widget && thing.WidgetListPopulated) { foreach (Widget childWidget in thing.WidgetList) { childWidget.ThingId = thing.Identity; if (!entitiesBeingHandled.ContainsEntity(childWidget)) { if (childWidget.IsDeleted) { WidgetRepo.Delete(entitiesNowBeingHandled, toSave, childWidget); } else { WidgetRepo.Save(entitiesNowBeingHandled, toSave, childWidget); } } } } // Post save protocol if (saved) { // Allow derived/partial class to do extra work OnAfterSaveEntity(thing); thing.Reset(); //The insert/update will have resulted in a new database_update row, inform interested parties RaiseModelChanged(); } } }