IEditableObject, help me!

Mar 24, 2010 at 9:30 AM
Edited Mar 24, 2010 at 9:39 AM

Hi,

I would like to use IEditableObject interface in my ViewModel...

I have to make our PersonViewModel class editable implementing IEditableObject interface...

 public class PersonViewModel : ViewModel<IPersonView>, IEditableObject
    {
        ...
        private Person cache;
        private bool isEditing;


        public void BeginEdit()
        {
            cache = new Person();
            cache = person;
            IsEditing = true;
        }
        public void CancelEdit()
        {
            person= cache;
            cache = null;
            IsEditing = false;
        }
        public void EndEdit()
        {
            cache = null;
            IsEditing = false;
        }

Then I modify PersonController with this:

 private bool CanUpdatePerson() { return personListViewModel.SelectedPerson != null; }

        private void UpdatePerson()
        {
            personViewModel.BeginEdit();
        }

Now I need a Cancel button that call CancelEdit().

I don't think that EntityController is a good space to implement Cancel() method, but all roads lead me there

Any suggestions?

Thanks

Aldo

Mar 25, 2010 at 11:16 AM

My solution is as following:

I add a Cancel() method to IEntityController and its implementation in EntityController class:

 public void Cancel()
        {
            if (HasChanges)
            {
                foreach (var entry in this.entities.ObjectStateManager.GetObjectStateEntries(EntityState.Modified))
                {

                    // exclude relationships from stateEntries object (filter only entities)

                    if (entry.Entity != null)

                        this.entities.Refresh(System.Data.Objects.RefreshMode.StoreWins, entry.Entity);

                }

                this.entities.AcceptAllChanges();
            }


        }

Then I have to understand how editing mode should be managed... I suppose by IsEditing property.

But...anyone uses this framework?