/// <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 ManyToMany relationship /// </summary> private IEnumerable <AuditLogEventTrace> CreateTraceEventsForManyToMany() { if (!IsSaved) { return(new AuditLogEventTrace[0]); } return(from ep in DwarfHelper.GetManyToManyProperties(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 let org = MergeLists(list, adds, dels) select new AuditLogEventTrace { PropertyName = ep.Name, OriginalValue = org, NewValue = list }); }
/// <summary> /// Append the Save method with additional modifications /// </summary> private void OnAfterSaveInternal() { foreach (var ep in DwarfHelper.GetDBProperties(GetType())) { SetOriginalValue(ep.Name, ep.GetValue(this)); } foreach (var ep in DwarfHelper.GetGemListProperties(GetType())) { if (IsCollectionInitialized(ep.ContainedProperty)) { SetOriginalValue(ep.Name, ep.GetValue(this)); } } foreach (var ep in DwarfHelper.GetManyToManyProperties(GetType())) { if (IsCollectionInitialized(ep.ContainedProperty)) { SetOriginalValue(ep.Name, ep.GetValue(this)); } } }
/// <summary> /// Automatically handles default persistance operations over DwarfLists for ManyToManyRelationships. /// Should a manual persistance be used via PersistManyToMany, do so before the base call in AppendStore /// </summary> private void PersistManyToManyCollections() { foreach (var pi in DwarfHelper.GetManyToManyProperties(this)) { if (!IsCollectionInitialized(pi.ContainedProperty)) { continue; } var tableName = ManyToManyAttribute.GetTableName(GetType(), pi.ContainedProperty); var obj = (IDwarfList)pi.GetValue(this); foreach (var deletedItem in obj.GetDeletedItems()) { DeleteManyToMany(this, deletedItem, tableName); } foreach (var addedItem in obj.GetAddedItems()) { PersistManyToMany(this, addedItem, tableName); } } }