The issue I'm trying to resolve is that I'm trying to convert a Winforms app that relies heavily on INotifyProperty
based data binding to track and log changes to the model, enabling a separate 'Value changed' audit to be written to another table.
I've searched for quite a while on this one and can't seem to find an answer that doesn't reference samples / examples using Entity Framework.
I've bound the data to the form and can return the updated information using either the IFormCollection
object, TryUpdateModelAsync(model)
or just passing the model directly to the PostFunction
. However, short of retrieving a copy of the original and changed model data then comparing the entities one property at a time (which is far from ideal), I can't seem to find a way of just identifying the changed values.
Is this even possible, is there a best practice for it and if so can someone direct me to a good reference source?
Current examples of post function to retrieve data:
public IActionResult Post_Client(ClientDataModel client) {
Console.WriteLine(client.Surname);
return RedirectToAction("Workspace");
}
public IActionResult Post_Client(IFormCollection formData) {
var client = new ClientDataModel();
client.Surname = list.FirstOrDefault(a => a.Key == "Surname").Value;
Console.WriteLine(client.Surname);
return RedirectToAction("Workspace");
}
public IActionResult Post_Client() {
var client = new ClientDataModel();
TryUpdateModelAsync(client);
Console.WriteLine(client.Surname);
return RedirectToAction("Workspace");
}
How it worked in Winforms:
public class ClientDataModel{
private string _surname;
public string Surname { get => _surname; set => _surname = value.Truncate(50); }
}
public class ClientBindingHelper:BaseNotifier{
public ClientData Model {
get {
return this.objmodel;
}
set {
this.objmodel = value;
}
}
public string Surname {
get {
return this.Model.Surname;
}
set {
this.Model.LogChange("Surname", this.Model.Surname, value);
this.Model.Surname = value;
base.NotifyPropertyChanged("Surname");
}
}
}
And then on the form itself the bindinghelper object would be bound to the control:
Private void BindData(){
var helper = new ClientBindingHelper()
txtSurname.DataBindings.Add("Text", myBindingHelper, "Surname");
}