public AuditlogBuilder <T> WithChildEntityCollection <T2, T3>(Expression <Func <T, T2> > expression, Expression <Func <T3, string> > collectionItemPropertyname, AuditlogBuilder <T3> builder) where T2 : IEnumerable <T3> where T3 : IEntity { _actions.Add(new Action(() => WithChildEntityCollectionInternal(expression, collectionItemPropertyname, builder))); return(this); }
public AuditlogBuilder <T> WithChildEntityCollection <T2, T3>(Expression <Func <T, T2> > expression, AuditlogBuilder <T3> builder) where T2 : IEnumerable <T3> where T3 : IEntity { return(WithChildEntityCollection(expression, null, builder)); }
private void WithChildEntityCollectionInternal <T2, T3>(Expression <Func <T, T2> > expression, Expression <Func <T3, string> > collectionItemPropertyname, AuditlogBuilder <T3> builder) where T2 : IEnumerable <T3> where T3 : IEntity { var propertyName = ((MemberExpression)expression.Body).Member.Name; T2 currentValue = _current != null?expression.Compile()(_current) : default; T2 previousValue = _previous != null?expression.Compile()(_previous) : default; var status = GetAuditlogStatus(currentValue, previousValue); currentValue ??= (T2)Activator.CreateInstance(typeof(List <>).MakeGenericType(typeof(T3))); previousValue ??= (T2)Activator.CreateInstance(typeof(List <>).MakeGenericType(typeof(T3))); var updated = currentValue .Join(previousValue, x => x.Id, x => x.Id, (current, previous) => { return(new AuditlogPair <T3> { Id = current.Id, CurrentValue = current, PreviousValue = previous }); }); var added = currentValue.Where(x => !previousValue.Any(y => y.Id == x.Id)) .Select(x => new AuditlogPair <T3> { Id = x.Id, CurrentValue = x }); var removed = previousValue.Where(x => !currentValue.Any(y => y.Id == x.Id)) .Select(previous => new AuditlogPair <T3> { Id = previous.Id, PreviousValue = previous }); var auditlogPairs = updated.Concat(added).Concat(removed); var item = new AuditlogItem { PropertyName = propertyName, Status = status, Type = AuditlogType.None, AuditlogItems = new List <AuditlogItem>() }; var i = 0; foreach (var auditlogPair in auditlogPairs) { var auditLogItems = builder.Build(auditlogPair.CurrentValue, auditlogPair.PreviousValue); if (auditLogItems.Count > 0) { i++; var auditlogPairStatus = GetAuditlogStatus(auditlogPair.CurrentValue, auditlogPair.PreviousValue); var item2 = new AuditlogItem { PropertyName = collectionItemPropertyname != null ? collectionItemPropertyname.Compile()(auditlogPair.CurrentValue ?? auditlogPair.PreviousValue).ToString() : i.ToString(), Status = auditlogPairStatus, Type = AuditlogType.None, AuditlogItems = auditLogItems }; item.AuditlogItems.Add(item2); } } if (item.AuditlogItems.Count == 0) { return; } _auditlogItems.Add(item); }
public AuditlogBuilder <T> WithChildEntity <T2>(Expression <Func <T, T2> > expression, AuditlogBuilder <T2> builder) { _actions.Add(new Action(() => WithChildEntityInternal(expression, builder))); return(this); }
private void WithChildEntityInternal <T2>(Expression <Func <T, T2> > expression, AuditlogBuilder <T2> builder) { var propertyName = ((MemberExpression)expression.Body).Member.Name; T2 currentValue = _current != null?expression.Compile()(_current) : default; T2 previousValue = _previous != null?expression.Compile()(_previous) : default; var auditLogItems = builder.Build(currentValue, previousValue); if (auditLogItems?.Count == 0) { return; } var status = GetAuditlogStatus(currentValue, previousValue); if (status == AuditlogStatus.Unchanged) { // Set status to updated because a child has changes status = AuditlogStatus.Updated; } var item = new AuditlogItem { PropertyName = propertyName, Status = status, Type = AuditlogType.None, AuditlogItems = auditLogItems }; _auditlogItems.Add(item); }