So, here's the thing. In a WPF application, I am populating a
BindableCollection
(see:
Caliburn.Micro[
^] ) from the ViewModel constructor.
Here's the constructor:
public UsersViewModel(IUsersUoW uow, IEventAggregator eventAggregator) : base(uow, eventAggregator)
{
DisplayName = "Users";
Users = new BindableCollection<User>(_uow.userrepo.FetchAll());
}
In short, _uow is my Unit of Work and userrepo is my repository. So, this actually populates the property Users which is bound to a Combobox in my View. There's a SelectedUser property which indicates the chosen item of the ComboBox. Now, there's a load of Textbox in view which display various properties of SelectedUser. One can edit the properties by editing the text in TextBox.
Now, in order to save the changes, I have a method, like so:
public void Modify()
{
var currentuser = _uow.userrepo.Find(SelectedUser.ID);
currentuser.addr = Address;
_uow.userrepo.Update(currentuser);
_uow.Complete();
Status = "Modified!";
}
The problem is, this change is being properly saved in the DB, but when I change the selected item in ComboBox to some other item, and then change it back to the item which I edited, the stale data is displayed.
The Update method of the repository is:
virtual public void Update(T updatedentity)
{
_ctx.Set<T>().Attach(updatedentity);
_ctx.Entry(updatedentity).State = EntityState.Modified;
}
What I have tried:
One way to alleviate this problem is to repopulate the ComboBox once the modification is made. But that seems to kludgy to me.
What is the standard protocol to avoid stale datum like this? Should I make the property Users like this?
public BindableCollection<User> Users => new BindableCollection<User>(_uow.userrepo.FetchAll());
In this case, I think there would be a performance hit, since every time I access the ComboBox, the repository would be accessed.