0

I've just read this question: How to cancel an edit to an object using MVVM?

I have the exact same question and would like to have a simple solution. The first one looked very promising, however, I'm using entity framework and my classes are automatically generated, so that's not an option.

How can I do this easily with EF?

EDIT: My ViewModel:

public List<Player> Players
{
    get { return repository.Players.OrderBy(x => x.Firstname).ToList(); }
}

public Player CurrentPlayer
{
    get { return currentPlayer;  }
    set
    {
        if (currentPlayer != value)
        {
            currentPlayer = value;
            RaisePropertyChanged("CurrentPlayer");
        }
    }
}

Players is bound to a datagrid, CurrentPlayer to the selecteditem of that. Below the datagrid, I have textboxes where the user can edit the player info.

When the user presses the save button, this code is executed:

private void SaveExecute(object parameter)
{
    repository.SavePlayer(currentPlayer);

    Editing = false;
}

Very easy. When the user presses the cancel button, this is executed:

private void CancelExecute(object parameter)
{
    if (currentPlayer.Id == 0) // id = 0 when a new player is being added
    {
        CurrentPlayer = null;
    }
    else
    {
        // here, the CurrentPlayer should be set back to it's previous state.
    }

    Editing = false;
}

CurrentPlayer is an object of Player, which is an entity class generated by EF.

Community
  • 1
  • 1
Bv202
  • 3,924
  • 13
  • 46
  • 80
  • Do not use EF entities as your viewmodels would be my first suggestion. – Quintin Robinson Nov 17 '12 at 23:00
  • 1
    Then you need to expand on your question as the mechanism you use to retrieve and store the data should not really matter to your VM in terms of binding a view to a cancel mechanism in the viewmodel. Do you have an example of what you *are* doing and why using EF is an impediment in this situation? – Quintin Robinson Nov 17 '12 at 23:11
  • I have edited the question. I think it should be clear now ;-) – Bv202 Nov 17 '12 at 23:21

1 Answers1

0

I don't understand the problem. If the user is editing a new item (State == ObjectState.Added) then you discard that, (and maybe set the CurrentPlayer to what it was before pressing the "New" Button?), else just retrieve the entity from the database again and that's it...

A better way to solve this problem is to have your CRUD and your List VMs have separate instances of the entity.

For example, when I create a List view (Datagrid or otherwise), usually the data displayed in that is just a subset of the whole data displayed in the full CRUD View. So, in order to show the entity in the CRUD, I need to Get() the entity again using the necessary Includes. This resolves the whole cancel problem, because the entity instance you're modifying is actually not the same as the one shown in the List view. If the user presses Save, you can replace the instance shown in the list view with the edited one, and if the user presses cancel, don't do anything.

Edit: Also be aware that if your entities are being generated by a T4 Template such as the Entity Framework STE Template, you can modify the .tt file and customize it to generate whatever code you need in your entities.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154