/// <summary> /// Invoked when disposing or finalizing this instance. /// </summary> /// <param name="disposing">True if the object is being disposed, false otherwise.</param> protected override void OnDispose(bool disposing) { try { if (_Command != null) { _Command.Dispose(); } } catch { } _Command = null; base.OnDispose(disposing); }
/// <summary> /// Invoked to execute this operation. /// </summary> internal void OnExecute(object origin = null) { lock (Repository.MasterLock) { List <object> list = null; ChangeEntry change = null; IDeleteCommand cmd = null; DebugEx.IndentWriteLine("\n- Preparing 'Delete({0})'...", MetaEntity); try { list = MetaEntity.GetRemovedChilds(forgetRemoved: true); foreach (var obj in list) { if (obj == null) { continue; } if (object.ReferenceEquals(obj, origin)) { continue; } var type = obj.GetType(); if (!type.IsClass && !type.IsInterface) { continue; } var meta = MetaEntity.Locate(obj); var map = meta.UberMap ?? Repository.LocateUberMap(type); if (map == null) { throw new NotFoundException("Cannot find map for type '{0}'.".FormatWith(type.EasyName())); } var op = map.Delete(obj); try { ((IUberOperation)op).OnExecute(origin: Entity); } finally { op.Dispose(); } } list.Clear(); list = null; list = MetaEntity.GetDependencies(Map, MemberDependencyMode.Child); foreach (var obj in list) { if (obj == null) { continue; } if (object.ReferenceEquals(obj, origin)) { continue; } var type = obj.GetType(); if (!type.IsClass && !type.IsInterface) { continue; } var meta = MetaEntity.Locate(obj); var map = meta.UberMap ?? Repository.LocateUberMap(type); if (map == null) { throw new NotFoundException("Cannot find map for type '{0}'.".FormatWith(type.EasyName())); } var op = map.Delete(obj); try { ((IUberOperation)op).OnExecute(origin: Entity); } finally { op.Dispose(); } } list.Clear(); list = null; cmd = Map.GenerateDeleteCommand(Entity); if (cmd != null) { DebugEx.IndentWriteLine("\n- Executing '{0}'...", cmd); try { MetaEntity.ValidateRowVersion(); int n = cmd.Execute(); } finally { DebugEx.Unindent(); } } Map.Detach(Entity); change = new ChangeEntry(ChangeType.Delete, Map, Entity); Repository.ChangeEntries.Add(change); list = MetaEntity.GetDependencies(Map, MemberDependencyMode.Parent); foreach (var obj in list) { if (obj == null) { continue; } if (object.ReferenceEquals(obj, origin)) { continue; } var type = obj.GetType(); if (!type.IsClass && !type.IsInterface) { continue; } var meta = MetaEntity.Locate(obj); var map = meta.UberMap ?? Repository.LocateUberMap(type); if (map == null) { throw new NotFoundException("Cannot find map for type '{0}'.".FormatWith(type.EasyName())); } change = new ChangeEntry(ChangeType.Refresh, map, obj); Repository.ChangeEntries.Add(change); } } finally { if (cmd != null) { cmd.Dispose(); } cmd = null; if (list != null) { list.Clear(); } list = null; DebugEx.Unindent(); } } }