public int Log(AuditModel model) { return(AuditUtil.Add(_auditDb, model)); }
// Ако е нужно един ChangeLogger да се ползва многократно, методите за инициализация и описание може да се подадат еднократно. //private readonly Func<Log> _logInitializer; //private readonly Func<PropertyDescriptorParams, Task<string>> _propertyDescriptorAsync; //public ChangeLogger(Func<Log> logInitializer, Func<PropertyDescriptorParams, Task<string>> propertyDescriptorAsync) //{ // _logInitializer = logInitializer; // _propertyDescriptorAsync = propertyDescriptorAsync; //} //public Task<int> SaveChangesAsync(DbContext db, Func<Task<int>> saveChagesAsyncMethod) //{ // Log log = (_logInitializer != null ? _logInitializer() : null) ?? new Log(); // return SaveChangesAsync(db, saveChagesAsyncMethod, log, _propertyDescriptorAsync); //} public async static Task <int> SaveChangesAsync( DbContext db, AuditContext auditDb, Func <Task <int> > saveChagesAsyncMethod, AuditModel audit, Type auditMasterEntityType, string auditMasterEntityId) { if (db == null) { throw new ArgumentNullException(nameof(db)); } if (saveChagesAsyncMethod == null) { throw new ArgumentNullException(nameof(saveChagesAsyncMethod)); } if (audit == null) { throw new ArgumentNullException(nameof(audit)); } if (auditMasterEntityType == null) { throw new ArgumentNullException(nameof(auditMasterEntityType)); } // Ако е подаден генериран клас, наследник на entity класа (т.нар. dynamic proxy), се взима entity класът. if (auditMasterEntityType.BaseType != null && auditMasterEntityType.Namespace == "System.Data.Entity.DynamicProxies") { auditMasterEntityType = auditMasterEntityType.BaseType; } audit.EntityName = auditMasterEntityType.Name; audit.EntityRecordId = auditMasterEntityId; audit.AuditTypeCode = AuditTypeCode.Write; audit.Sanitize(); if (audit.AuditDetails == null) { audit.AuditDetails = new List <AuditDetailModel>(); } int result; IEnumerable <EntityEntry> addedEntries = GetAddedEntries(db); try { var logConfigDictionary = await auditDb.AuditConfigs.ToDictionaryAsync(l => l.EntityName + "." + l.PropertyName); await GetLogDetailsForUpdatedAndDeletedAsync(db, audit, logConfigDictionary); result = await saveChagesAsyncMethod(); await GetLogDetailsForAddedAsync(db, audit, logConfigDictionary, addedEntries); } catch (Exception ex) { throw ex; } audit.GetMissingEntityRecordIdFromDetails(); // да не се записват записи за промяна без реални промени if (audit.AuditDetails.Count > 0) { AuditUtil.Add(auditDb, audit); // Извиква SaveChanges(). } await UpdateUserHostFromIpAsync(audit); return(result); }