public virtual async Task MoveToTrashAsync(TEntity entity) { entity.MoveToTrash(); await _auditManager.AuditDeletion(entity); Context.AddCommand(() => DbSet.ReplaceOneAsync(Builders <TEntity> .Filter.Eq(x => x.Id, entity.Id), entity)); }
public async Task AuditAsync(ChangeTracker changeTracker) { var entries = changeTracker.Entries() .Where(x => x.Entity.GetType().IsClass && !x.Entity.GetType().IsAbstract && x.Entity.GetType().GetInterfaces().Contains(typeof(IDomainEntity)) && (x.State == EntityState.Added || x.State == EntityState.Modified)) .ToList(); if (entries.Count == 0) { return; } var creationAuditedInterface = typeof(ICreationAudited <,>); var creationAudits = entries.Where(x => IsElegibleToAuditByInterface(x, EntityState.Added, creationAuditedInterface)).ToList(); foreach (var entry in creationAudits) { await _auditManager.AuditCreation(entry.Entity as IDomainEntity, GetAuditableInterfaceArguments(entry, creationAuditedInterface)); } var modificationAuditedInterface = typeof(IModificationAudited <,>); var modificationAudits = entries.Where(x => IsElegibleToAuditByInterface(x, EntityState.Modified, modificationAuditedInterface)).ToList(); foreach (var entry in modificationAudits) { await _auditManager.AuditModification(entry.Entity as IDomainEntity, GetAuditableInterfaceArguments(entry, modificationAuditedInterface)); } var deletionAuditedInterface = typeof(IDeletionAudited <,>); var deletionAudits = entries.Where(x => IsElegibleToAuditByInterface(x, EntityState.Modified, deletionAuditedInterface) && (bool?)x.Entity.GetType().GetProperty("IsDeleted")?.GetValue(x.Entity) == true) .ToList(); foreach (var entry in deletionAudits) { await _auditManager.AuditDeletion(entry.Entity as IDomainEntity, GetAuditableInterfaceArguments(entry, deletionAuditedInterface)); } }