private AccountState ApplyEvent(TaxAppliedDuringBilling occurred) { var trans = new FinancialTransaction(new Tax { Amount = (double)occurred.TaxAmountApplied }, (double)occurred.TaxAmountApplied); Obligations[occurred.ObligationNumber]?.PostTransaction(trans); var newState = new AccountState( AccountNumber, CurrentBalance + occurred.TaxAmountApplied, AccountStatus, Obligations, SimulatedFields, AuditLog.Add(new StateLog( "TaxAppliedDuringBilling", occurred.Message + " Balance After: " + (CurrentBalance + occurred.TaxAmountApplied).ToString("C"), occurred.UniqueGuid(), occurred.OccurredOn())), OpeningBalance, Inventroy, UserName, LastPaymentAmount, LastPaymentDate); return(newState); }
private AccountState ApplyEvent(ObligationAssessedConcept occurred) { if (!Obligations.ContainsKey(occurred.ObligationNumber)) { Console.WriteLine( $"Account {AccountNumber} does not contain obligation {occurred.ObligationNumber}. No Action Taken."); throw new Exception( $"Account {AccountNumber} does not contain obligation {occurred.ObligationNumber}. No Action Taken."); } var trans = new FinancialTransaction(occurred.FinancialBucket, occurred.FinancialBucket.Amount); Obligations[occurred.ObligationNumber].PostTransaction(trans); var newBalance = CurrentBalance + decimal.Parse(occurred.FinancialBucket.Amount.ToString()); var newState = new AccountState( AccountNumber, newBalance, AccountStatus, Obligations, SimulatedFields, AuditLog.Add( new StateLog( "ObligationAssessedConcept", occurred.Message + " Balance After: " + ((decimal)newBalance).ToString("C"), occurred.UniqueGuid(), occurred.OccurredOn()) ), OpeningBalance, Inventroy, UserName, LastPaymentAmount, LastPaymentDate); return(newState); }
public AccountState Event(SomeOneSaidHiToMe occurred) { return(new AccountState(this.AccountNumber, this.CurrentBalance, this.AccountStatus, this.Obligations, LoadSimulation().ToImmutableDictionary(), AuditLog.Add(new StateLog("SomeOneSaidHiToMe", occurred.UniqueGuid(), occurred.OccurredOn())))); }
private AccountState ApplyEvent(AccountCurrentBalanceUpdated occurred) { var adjustmentsObligation = new MaintenanceFee("AccountAdjustments", 0, ObligationStatus.Boarding); if (Obligations.ContainsKey("AccountAdjustments")) { adjustmentsObligation = Obligations["AccountAdjustments"]; } var trans = new FinancialTransaction(new Adjustment(occurred.CurrentBalance), occurred.CurrentBalance); adjustmentsObligation.PostTransaction(trans); return(new AccountState( AccountNumber, decimal.Parse(occurred.CurrentBalance.ToString()), AccountStatus, Obligations.Add(adjustmentsObligation.ObligationNumber, adjustmentsObligation), SimulatedFields, AuditLog.Add(new StateLog("AccountCurrentBalanceUpdated", "Boarding Adjustment", occurred.UniqueGuid(), occurred.OccurredOn())), OpeningBalance, Inventroy, UserName, LastPaymentAmount, LastPaymentDate)); }
public AccountState Event(AccountCancelled occurred) { return(new AccountState(this.AccountNumber, this.CurrentBalance, occurred.AccountStatus, this.Obligations, SimulatedFields, AuditLog.Add(new StateLog("AccountCancelled", occurred.UniqueGuid(), occurred.OccurredOn())))); }
public AccountState Event(SuperSimpleSuperCoolEventFoundByRules occurred) { return(new AccountState(this.AccountNumber, this.CurrentBalance, this.AccountStatus, this.Obligations, LoadSumulation(SimulatedFields, "1", "My state has been updated, see..."), AuditLog.Add(new StateLog("SuperSimpleSuperCoolEventFoundByRules", occurred.UniqueGuid(), occurred.OccurredOn())))); }
private AccountState ApplyEvent(UacAppliedAfterBilling occurred) { var trans = new FinancialTransaction(new Tax { Amount = occurred.UacAmountApplied }, occurred.UacAmountApplied); Obligations[occurred.ObligationNumber]?.PostTransaction(trans); var newBal = CurrentBalance + decimal.Parse((-1 * occurred.UacAmountApplied).ToString()); var newState = new AccountState( AccountNumber, newBal, AccountStatus, Obligations, SimulatedFields, AuditLog.Add(new StateLog( "UacAppliedAfterBilling", $"{occurred.Message} Balance After: {newBal:C}", occurred.UniqueGuid(), occurred.OccurredOn())), OpeningBalance, Inventroy, UserName, LastPaymentAmount, LastPaymentDate ); return(newState); }
private Task OnAfterSaveChanges(List <AuditEntry> auditEntries) { if (auditEntries == null || auditEntries.Count == 0) { return(Task.CompletedTask); } foreach (var auditEntry in auditEntries) { // Get the final value of the temporary properties foreach (var prop in auditEntry.TemporaryProperties) { if (prop.Metadata.IsPrimaryKey()) { auditEntry.KeyValues[prop.Metadata.Name] = prop.CurrentValue; } else { auditEntry.NewValues[prop.Metadata.Name] = prop.CurrentValue; } } // Save the Audit entry AuditLog.Add(auditEntry.ToAudit()); } return(SaveChangesAsync()); }
public async Task <int> SaveChangesWithAuditAsync(string adminUserId = null) { Guid transactionId = Guid.NewGuid(); var changed = ChangeTracker.Entries() .Where(e => e.State == EntityState.Modified); Guid?adminGuid = string.IsNullOrEmpty(adminUserId) ? default(Guid?) : new Guid(adminUserId); foreach (DbEntityEntry entry in changed) { Type entityType = ObjectContext.GetObjectType(entry.Entity.GetType()); if (!typeof(IAuditable).IsAssignableFrom(entityType)) { break; } string userId = entry.OriginalValues["Id"].ToString(); Guid _; if (!Guid.TryParse(userId, out _)) { userId = entry.OriginalValues["UserId"].ToString(); } PropertyInfo[] fullProperties = entityType.GetProperties(); var interfaceProperties = entityType.GetProperties().Where(p => p.IsDefined(typeof(AuditableAttribute), false)); if (interfaceProperties.Any()) { foreach (var prop in interfaceProperties) { var originalValue = entry.OriginalValues[prop.Name]; var newValue = entry.CurrentValues[prop.Name]; var customAttributes = prop.GetCustomAttribute <AuditableAttribute>(); if (customAttributes.LogAlways || !Equals(originalValue, newValue)) { string auditLog = customAttributes.Obfuscate ? $"{prop.Name} was updated" : $"{prop.Name} was updated from {originalValue} to {newValue}"; AuditLog.Add(new Entity.ActivityLog { DateCreated = DateTime.UtcNow, TxId = transactionId, Entity = entityType.Name, Property = prop.Name, OldValue = customAttributes.Obfuscate ? null : originalValue.ToString(), NewValue = customAttributes.Obfuscate ? null : newValue.ToString(), UserId = new Guid(userId), AdminUserId = adminGuid }); } } } } return(await base.SaveChangesAsync()); }
public AccountState Event(ObligationAddedToAccount occurred) { return(new AccountState(this.AccountNumber, this.CurrentBalance, this.AccountStatus, this.Obligations.Add(occurred.Obligation.ObligationNumber, occurred.Obligation), SimulatedFields, AuditLog.Add(new StateLog("ObligationAddedToAccount", occurred.UniqueGuid(), occurred.OccurredOn())))); }
public AccountState Event(ObligationSettledConcept occurred) { var trans = new FinancialTransaction(occurred.FinancialConcept, occurred.Amount); this.Obligations[occurred.ObligationNumber]?.PostTransaction(trans); return(new AccountState(this.AccountNumber, this.CurrentBalance, this.AccountStatus, this.Obligations, SimulatedFields, AuditLog.Add(new StateLog("ObligationSettledConcept", occurred.UniqueGuid(), occurred.OccurredOn())))); }
private AccountState ApplyEvent(AccountCancelled occurred) { return(new AccountState(AccountNumber, CurrentBalance, occurred.AccountStatus, Obligations, SimulatedFields, AuditLog.Add(new StateLog("AccountCancelled", occurred.Message, occurred.UniqueGuid(), occurred.OccurredOn())) , OpeningBalance, Inventroy, UserName, LastPaymentAmount, LastPaymentDate)); }
private AccountState ApplyEvent(SomeOneSaidHiToMe occurred) { return(new AccountState( AccountNumber, CurrentBalance, AccountStatus, Obligations, LoadSimulation().ToImmutableDictionary(), AuditLog.Add(new StateLog("SomeOneSaidHiToMe", occurred.Message, occurred.UniqueGuid(), occurred.OccurredOn())), OpeningBalance, Inventroy, UserName, LastPaymentAmount, LastPaymentDate)); }
private AccountState ApplyEvent(ObligationAddedToAccount occurred) { return(new AccountState( AccountNumber, CurrentBalance, AccountStatus, Obligations.Add(occurred.MaintenanceFee.ObligationNumber, occurred.MaintenanceFee), SimulatedFields, AuditLog.Add(new StateLog("ObligationAddedToAccount", occurred.Message, occurred.UniqueGuid(), occurred.OccurredOn())) , OpeningBalance, Inventroy, UserName, LastPaymentAmount, LastPaymentDate)); }
private AccountState ApplyEvent(SuperSimpleSuperCoolDomainEventFoundByRules occurred) { return(new AccountState( AccountNumber, CurrentBalance, AccountStatus, Obligations, LoadSumulation(SimulatedFields, "1", "My state has been updated, see..."), AuditLog.Add(new StateLog("SuperSimpleSuperCoolEventFoundByRules", occurred.Message, occurred.UniqueGuid(), occurred.OccurredOn())), OpeningBalance, Inventroy, UserName, LastPaymentAmount, LastPaymentDate)); }
private AccountState ApplyEvent(AccountBusinessRuleValidationSuccess occurred) { return(new AccountState( AccountNumber, CurrentBalance, AccountStatus, Obligations, SimulatedFields, AuditLog.Add( new StateLog( "AccountBusinessRuleValidationSuccess", $"{occurred.Message}", occurred.UniqueGuid(), occurred.OccurredOn() ) ), OpeningBalance, Inventroy, UserName, LastPaymentAmount, LastPaymentDate )); }
private AccountState ApplyEvent(AccountCreated occurred) { var newState = new AccountState( occurred.AccountNumber, accountStatus: AccountStatus.Boarded, obligations: ImmutableDictionary.Create <string, MaintenanceFee>(), simulation: LoadSimulation(), log: AuditLog.Add( new StateLog("AccountCreated", occurred.Message + " Balance After: " + ((decimal)occurred.OpeningBalance).ToString("C"), occurred.UniqueGuid(), occurred.OccurredOn() ) ), openingBalance: occurred.OpeningBalance, currentBalance: 0, //best to affect currBal explicitly with an event inventory: occurred.Inventory, userName: occurred.UserName, lastPaymentAmount: occurred.LastPaymentAmount, lastPaymentDate: occurred.LastPaymentDate ); return(newState); }
private AccountState ApplyEvent(PaymentAppliedToObligation occurred) { var trans = new FinancialTransaction(occurred.FinancialBucket, occurred.Amount); Obligations[occurred.ObligationNumber].PostTransaction(trans); decimal newBal = CurrentBalance - decimal.Parse(occurred.Amount.ToString()); return(new AccountState( AccountNumber, newBal, AccountStatus, Obligations, SimulatedFields, AuditLog.Add(new StateLog( "PaymentAppliedToObligation", $"{occurred.Message} Balance After: {newBal :C}", occurred.UniqueGuid(), occurred.OccurredOn())) , OpeningBalance , Inventroy , UserName , lastPaymentAmount: occurred.Amount , lastPaymentDate: DateTime.Today)); }
private List <AuditEntry> OnBeforeSaveChanges() { ChangeTracker.DetectChanges(); var auditEntries = new List <AuditEntry>(); foreach (var entry in ChangeTracker.Entries()) { if (entry.Entity is AuditLog || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged) { continue; } var auditEntry = new AuditEntry(entry) { TableName = entry.Metadata.Relational().TableName }; auditEntries.Add(auditEntry); foreach (var property in entry.Properties) { if (property.IsTemporary) { // value will be generated by the database, get the value after saving auditEntry.TemporaryProperties.Add(property); continue; } string propertyName = property.Metadata.Name; if (property.Metadata.IsPrimaryKey()) { auditEntry.KeyValues[propertyName] = property.CurrentValue; continue; } switch (entry.State) { case EntityState.Added: auditEntry.NewValues[propertyName] = property.CurrentValue; break; case EntityState.Deleted: auditEntry.OldValues[propertyName] = property.OriginalValue; break; case EntityState.Modified: if (property.IsModified) { auditEntry.OldValues[propertyName] = property.OriginalValue; auditEntry.NewValues[propertyName] = property.CurrentValue; } break; } } } // Save audit entities that have all the modifications foreach (var auditEntry in auditEntries.Where(_ => !_.HasTemporaryProperties)) { AuditLog.Add(auditEntry.ToAudit()); } // keep a list of entries where the value of some properties are unknown at this step return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList()); }
public AccountState Event(AccountCreated occurred) { return(new AccountState(occurred.AccountNumber, LoadSimulation(), AuditLog.Add(new StateLog("AccountCreated", occurred.UniqueGuid(), occurred.OccurredOn())))); }