Beispiel #1
0
        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());
        }
Beispiel #2
0
        /// <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);
            }
        }