目前在我的应用程序中,我正在记录用户更改的任何值,效果很好。我有一个类对象MyObject
和Logs
对象,并且在每个属性上我都会引发一个事件:
private string _occupation;
[FieldAlias("_occupation")]
public string Occupation
{
get { return _occupation; }
set
{
if (this.Occupation != value)
{
string oldValue = Occupation;
_occupation = value;
NotifyPropertyChanged("Occupation", oldValue, value);
}
}
}
该类MyObject
还实现INotifyPropertyChanged
了,我有以下事件:
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged<T>(string propertyName, T oldValue, T newValue)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new Helper.PropertyChangedExtendedEventArgs<T>(propertyName, oldValue, newValue));
}
this.Logs.Add(new Logs
{
CreatedDate = DateTime.Now,
CreatedBy = this.ModifiedBy,
PropertyName = propertyName,
NewValue = (newValue != null) ? newValue.ToString() : "",
OldValue = (oldValue != null) ? oldValue.ToString() : ""
});
}
CreatedBy
除了上面的字段外,一切正常。由于它来自MyObject's
ModifiedBy
属性,更新不会立即发生,因为当时该字段将包含以前的用户名(希望它有意义)。
我如何反映发生的变化,我不能在ModifiedBy
属性上引发事件,因为它将在Logs
表中创建一个新行,那么还有其他选择吗?