Пример #1
0
 /// <summary>
 /// Clone the object as a means of caching the current values to allow changes to be reverted on 'Cancel'
 /// </summary>
 public Album Clone()
 {
     // Assumes this is a flat, data model object
     int ticks = (int)DateTime.Now.Ticks * -1; // hack to get a fairly unique integer; the clone isn't going to get saved so this shouldn't matter
     Album newInstance = new Album(ticks);
     CopyValues(this, newInstance);
     return newInstance;
 }
Пример #2
0
        // This stuff could move to a IRevertable interface and/or a DataModelBase class that derives from PropertyChangedBase
        public static void CopyValues(Album from, Album to)
        {
            // Use reflection to shuttle the property values from source to destination Album instances
            Type type = from.GetType();
            System.Reflection.PropertyInfo[] allProperties = type.GetProperties();

            foreach (System.Reflection.PropertyInfo property in allProperties)
            {
                if (property.CanWrite) //TODO: this could be made more flexible by honoring a custom 'DoNotCopy' attribute
                {
                    property.SetValue(to, property.GetValue(from, null), null);
                }
            }
        }
Пример #3
0
        public void DoCancel()
        {
            if (_isNew)
            {
                DoDelete();
            }
            else
            {
                Album.CopyValues(_originalValues, SelectedAlbum); // revert to original values
                _originalValues = null;
            }

            SelectedAlbum = null;
            IsEditMode = false;
            _isNew = false;
        }
Пример #4
0
        public void DoSave()
        {
            int i = SelectedAlbum.ID;

            // the actual save to local folder of json files
            JsonRepository<Album> repo = new JsonRepository<Album>();
            repo.Save(SelectedAlbum);

            SelectedAlbum = null;
            IsEditMode = false;
            _originalValues = null;
            _isNew = false;
        }
Пример #5
0
        public void DoAddNew()
        {
            int nextId = Albums.OrderBy(a => a.ID).Last().ID + 1;
            // TODO: if using repo:
            // JsonRepository<Album> repo = new JsonRepository<Album>();
            // int nextId2 = repo.GetNextId();

            Album newAlbum = new Album(nextId) {Title = "(new)"};
            Albums.Add(newAlbum);
            SelectedAlbum = newAlbum;
            _isNew = true;
            IsEditMode = true;
            RefreshActionGuards();
        }