/// <summary> /// Restores the object's properties to their original values /// </summary> public void Reset() { if (originalValues == null) { throw new InvalidOperationException("You cannot reset a new object. Just create a new one?"); } foreach (var kvp in originalValues) { PropertyHelper.SetValue(this, kvp.Key, kvp.Value); } ResetFKProperties(); foreach (var ep in DwarfHelper.GetOneToManyProperties(this)) { var key = GetCollectionKey(ep.Name); CacheManager.RemoveKey(key); } foreach (var ep in DwarfHelper.GetManyToManyProperties(this)) { var key = GetCollectionKey(ep.Name); CacheManager.RemoveKey(key); } if (oneToManyAlternateKeys != null) { oneToManyAlternateKeys.Clear(); } }
private void OnBeforeDeleteInternal() { //We want to persist all the Inverse OneToManyCollection upon deletion. Let's remove all lists that doesn't //need persistance first (all objects therein will be deleted in the database via delete cascades anyways) foreach (var pi in DwarfHelper.GetOneToManyProperties(this)) { var propertyAtt = OneToManyAttribute.GetAttribute(pi.ContainedProperty); if (propertyAtt == null) { throw new NullReferenceException(pi.Name + " is missing the OneToMany attribute..."); } if (!propertyAtt.IsInverse) { continue; } var obj = (IDwarfList)pi.GetValue(this); var owningProp = oneToManyAlternateKeys.ContainsKey(pi.Name) ? oneToManyAlternateKeys[pi.Name] : GetType().Name; obj.Cast <IDwarf>().ForEachX(x => PropertyHelper.SetValue(x, owningProp, null)); obj.SaveAllInternal <T>(); } if (DbContextHelper <T> .DbContext.IsAuditLoggingSuspended || DwarfContext <T> .GetConfiguration().AuditLogService == null) { return; } var traces = (from ep in DwarfHelper.GetDBProperties(GetType()).Where(x => !x.Name.Equals("Id")) let oldValue = originalValues[ep.Name] where oldValue != null && (oldValue is string? !string.IsNullOrEmpty(oldValue.ToString()) : true) select new AuditLogEventTrace { PropertyName = ep.Name, OriginalValue = oldValue }).ToArray(); var collectionTraces = (from ep in DwarfHelper.GetGemListProperties(GetType()) let oldValue = (IGemList)ep.GetValue(this) where oldValue != null && oldValue.Count > 0 select new AuditLogEventTrace { PropertyName = ep.Name, OriginalValue = oldValue }).ToArray(); var many2ManyTraces = (from ep in DwarfHelper.GetManyToManyProperties(GetType()) let oldValue = ep.GetValue(this) where oldValue != null && ((IList)oldValue).Count > 0 select new AuditLogEventTrace { PropertyName = ep.Name, OriginalValue = oldValue }).ToArray(); DwarfContext <T> .GetConfiguration().AuditLogService.Logg(this, AuditLogTypes.Deleted, traces.Union(collectionTraces).Union(many2ManyTraces).ToArray()); }
/// <summary> /// Creates and returns an AuditLogEventTrace object for every modified OneToMany relationship /// </summary> private IEnumerable <AuditLogEventTrace> CreateTraceEventsForOneToMany() { if (!IsSaved) { return(new AuditLogEventTrace[0]); } return(from ep in DwarfHelper.GetOneToManyProperties(this) where IsCollectionInitialized(ep.ContainedProperty) let list = ep.GetValue(this) let adds = ((IList)ep.PropertyType.GetMethod("GetAddedItems").Invoke(list, null)).Cast <IDwarf>().ToList() let dels = ((IList)ep.PropertyType.GetMethod("GetDeletedItems").Invoke(list, null)).Cast <IDwarf>().ToList() where adds.Count > 0 || dels.Count > 0 select new AuditLogEventTrace { PropertyName = ep.Name, NewValue = list }); }
/// <summary> /// Automatically handles default persistance operations over DwarfLists for OneToManyRelationships. /// Should a manual persistance be used via PersistOneToMany, do so before the base call in AppendStore /// </summary> private void PersistOneToManyCollections() { foreach (var pi in DwarfHelper.GetOneToManyProperties(this)) { if (!IsCollectionInitialized(pi.ContainedProperty)) { continue; } var propertyName = pi.Name; var propertyAtt = OneToManyAttribute.GetAttribute(pi.ContainedProperty); if (propertyAtt == null) { throw new NullReferenceException(propertyName + " is missing the OneToMany attribute..."); } var obj = (IDwarfList)PropertyHelper.GetValue(this, propertyName); var list = obj.Cast <IDwarf>().ToList(); var owningProp = oneToManyAlternateKeys.ContainsKey(propertyName) ? oneToManyAlternateKeys[propertyName] : GetType().Name; list.ForEach(x => PropertyHelper.SetValue(x, owningProp, this)); var deleteObjects = obj.GetDeletedItems(); if (propertyAtt.IsInverse) { deleteObjects.ForEach(x => PropertyHelper.SetValue(x, owningProp, null)); deleteObjects.SaveAllInternal <T>(); } else { deleteObjects.DeleteAllInternal <T>(); } list.SaveAllInternal <T>(); oneToManyAlternateKeys.Remove(propertyName); } }