Currently on my application I am doing logging on any value the user changes, which works fine. I have a class object MyObject and Logs object and on each property I am raising an event as such:
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);
}
}
}
The MyObject class also implements INotifyPropertyChanged and I have following event:
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() : ""
});
}
Everything works fine except for he CreatedBy field above. As it is coming from MyObject's ModifiedBy property, the update doesn’t happen immediately because at the time the field will contain previous user’s name(hope it makes sense).
How can I reflect the changes as it happens, I cannot raise the event on ModifiedBy property as it is going to create a new row in Logs table, so is there any other options?
Ended up using the current user from cache this way it will update it properly.