WP7.5 #2 ClientModel IEditableObject

In order to make our data oriented applications, we have to implement a way to generalize and automatize the adding, updating and deleting items, that is the function of the IEditableObject and IDeletableObject for our ClientModel which has INotifyPropertyChanged, here is the code you need to use later in the generic viewmodel we will use in #3 adding the new Interfaces:

public class ClientModel<T> : INotifyPropertyChanged, IEditableObject, IDeletableObject where T : class
    {
        public T Model;
        public ClientModel()
        {
            Model = Activator.CreateInstance<T>();
        }

        #region IEditableObject Fields
        private T BackupModel;
        public bool IsEditing = false;
        public bool IsAdding = false;
        public Action<T> Update;
        public Action<T> Add;
        public Action<T> Remove;
        #endregion

        #region INotifyPropertyChanged
        /// <summary>
        /// Notify Property Changed
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        protected virtual void NotifyPropertyChanged<C>(Expression<Func<C>> expression)
        {
            string propertyName = GetPropertyName(expression);
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        /// <summary>
        /// Notify Property Changed (Shorted method name)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        protected virtual void Notify<C>(Expression<Func<C>> expression)
        {
            string propertyName = GetPropertyName(expression);
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        /// <summary>
        /// Get the string name for the property
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        protected string GetPropertyName<C>(Expression<Func<C>> expression)
        {
            MemberExpression memberExpression = (MemberExpression)expression.Body;
            return memberExpression.Member.Name;
        }

        /// <summary>
        /// Property Changed Event
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region IEditableObject Members
        public event EventHandler Commited;
        public void BeginEdit()
        {
            if (!IsEditing)
            {
                BackupModel = Model;
                IsEditing = true;
            }
        }

        public void CancelEdit()
        {
            if (IsEditing)
            {
                Model = BackupModel;
                IsEditing = false;
                if (Commited != null)
                    Commited(this, new EventArgs());
            }
        }

        public void EndEdit()
        {
            if (IsAdding)
            {
                BackupModel = null;
                IsAdding = false;
                Add(Model);
                if (Commited != null)
                    Commited(this, new EventArgs());
            }
            else if (IsEditing)
            {
                BackupModel = null;
                IsEditing = false;
                Update(Model);
                if (Commited != null)
                    Commited(this, new EventArgs());
            }
        }

        #endregion

        #region IDeletableObject Members

        public void Delete()
        {
            Remove(Model);
        }

        #endregion
    }

    public interface IDeletableObject
    {
        void Delete();
    }

With this we will implement a Base ViewModel that can automatically make database operations.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s