示例#1
0
        private void TrackRecord(ChangeTracker changeTracker)
        {
            if (changeTracker == null)
            {
                return;
            }

            var when = DateProvider.UTC();

            string userName;

            try
            {
                userName = ContextService?.UserContext?.Name ?? ANONYMOUS_USER;
            }
            catch
            {
                userName = ANONYMOUS_USER;
            }

            var addedLongEntries   = changeTracker.Entries <IAuditEntity>().Where(p => p.State == EntityState.Added);
            var addedGuidEntries   = changeTracker.Entries <IAuditEntity>().Where(p => p.State == EntityState.Added);
            var updatedLongEntries = changeTracker.Entries <IAuditEntity>().Where(p => p.State == EntityState.Modified);
            var updatedGuidEntries = changeTracker.Entries <IAuditEntity>().Where(p => p.State == EntityState.Modified);

            var addedAuditedLongEntities = addedLongEntries.Select(p => p.Entity);

            foreach (var added in addedAuditedLongEntities)
            {
                added.OnAuditInsert(userName, when);
            }

            var addedAuditedGuidEntities = addedGuidEntries.Select(p => p.Entity);

            foreach (var added in addedAuditedGuidEntities)
            {
                added.OnAuditInsert(userName, when);
            }

            var modifiedAuditedLongEntities = updatedLongEntries.Select(p => p.Entity);

            foreach (var modified in modifiedAuditedLongEntities)
            {
                modified.OnAuditUpdate(userName, when);
            }

            var modifiedAuditedGuidEntities = updatedGuidEntries.Select(p => p.Entity);

            foreach (var modified in modifiedAuditedGuidEntities)
            {
                modified.OnAuditUpdate(userName, when);
            }
        }