private void AddToChangeLog(IMethodInvocation input, string propertyName, IBaseEntity entity) { if (_changeLog.ContainsKey(propertyName)) { var item = _changeLog[propertyName]; item.NewValue = input.Arguments[0]; item.TimeStamp = DateTime.Now; } else { UnitOfWorkItem item = new UnitOfWorkItem { OldVaue = input.Target.GetType().GetProperty(propertyName).GetValue(input.Target), NewValue = input.Arguments[0], TimeStamp = DateTime.Now, PropertyName = propertyName, ObjectId = entity.Id, DomainObject = input.MethodBase.DeclaringType?.Name, ChangedBy = Common.Constants.Constants.UserId }; _changeLog.Add(propertyName, item); } }
/// <summary> /// Implement this method to execute your behavior processing. /// </summary> /// <param name="input">Inputs to the current call to the target.</param> /// <param name="getNext">Delegate to execute to get the next delegate in the behavior chain.</param> /// <returns> /// Return value from the target. /// </returns> /// <exception cref="System.NotImplementedException"></exception> public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { if (input.MethodBase.Name == "get_ChangeLog" && input.MethodBase.DeclaringType == typeof(IUnitOfWork)) { return(input.CreateMethodReturn(ChangeLog.Values.ToArray())); } if (input.MethodBase.Name == "AddChange" && input.MethodBase.DeclaringType == typeof(IUnitOfWork)) { UnitOfWorkItem unitOfWorkItem = input.Arguments[0] as UnitOfWorkItem; if (!_changeLog.ContainsKey(unitOfWorkItem.PropertyName)) { _changeLog.Add(unitOfWorkItem.PropertyName, unitOfWorkItem); } return(input.CreateMethodReturn(null)); } IBaseModel model = input.Target as IBaseModel; if (IsRelevantProperty(input) && (model.State == Constants.Constants.EntityState.Unchanged || model.State == Constants.Constants.EntityState.Modified)) { if (model.State == Constants.Constants.EntityState.Unchanged) { _changeLog.Clear(); } string propertyName = input.MethodBase.Name.Substring(4); if (_changeLog.ContainsKey(propertyName)) { var item = _changeLog[propertyName]; item.NewValue = input.Arguments[0] == null ? null : input.Arguments[0].ToString(); item.TimeStamp = DateTime.Now; } else { object oldValue = input.Target.GetType().GetProperty(propertyName).GetValue(input.Target); if (oldValue == null && input.Arguments[0] == null) { return(getNext()(input, getNext)); } if (oldValue != null && oldValue.Equals(input.Arguments[0])) { return(getNext()(input, getNext)); } UnitOfWorkItem item = new UnitOfWorkItem { OldVaue = oldValue == null ? null : oldValue.ToString(), NewValue = input.Arguments[0] == null ? null : input.Arguments[0].ToString(), TimeStamp = DateTime.Now, PropertyName = propertyName, ObjectId = model.Id, DomainObject = input.MethodBase.DeclaringType?.Name, ChangedBy = Constants.Constants.UserId }; _changeLog.Add(propertyName, item); } } return(getNext()(input, getNext)); }