WP7.5 #1 Model and Client Model

Introduction

Today I looked at MSDN Basic WP7Local Database and a few days ago I started using Linq2Sql for SQL Server Compact instead of my libraries. Both are the same.

There are several methods that does not exist in the library like ‘CreateTable’, ‘TableExists’, ‘DeleteTable’, and shortening ‘Insert’ instead of ‘dataContext.GetTable().InsertOnSubmit(entity);’ and ‘Table’ instead of ‘dataContext.GetTable()’ which at the beginning it cost me to understand because GetTable has an instance of Table when it database does not exists, so I prefer to call it Table.

The only thing I do not like is that I cannot inherit as usual, because typically we create a base class with an ID field but we cannot do that using the actual version of Linq2Sql.

Here is the source code: (.doc to .cs) WP7Extensions

I take a look to the new examples of Mango, that are great for it purposes, but I recommend to split the Data Model from the Client (Model), because in the case you are using WCF you will have to recode all.

The Model
So first create the Data Model very simple (I have read a lot that this is what you have to do).

[Table(Name = "Users")]
    public class User
    {
        [Column(Name = "id", IsDbGenerated = true, IsPrimaryKey = true)]
        public Int32 id;

        [Column(Name = "isdeleted")]
        public Boolean isdeleted;
        
        [Column(Name = "name")]
        public string name;

        [Column(Name = "age")]
        public Int32 age;
    }

The Client Model

The idea in following posts is that the class stores the information without calling commands, do it automatically.

Now we create the Client Class which inherits from the ClientBase class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Linq.Expressions;

namespace WPFTest.Model
{
    public class ClientModel<T> : INotifyPropertyChanged where T : class
    {
        public T Model;
        public ClientModel()
        {
            Model = Activator.CreateInstance<T>();
        }

        #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
    }
}

Finally the Client Model:

public class ClientUser : ClientModel<User>
    {
        public string Name
        {
            get { return Model.name; }
            set
            {
                if (Model.name != value)
                {
                    Model.name = value;
                    Notify(() => Name);
                }
            }
        }

        public Int32 Age
        {
            get { return Model.age; }
            set
            {
                if (Model.age != value)
                {
                    Model.age = value;
                    Notify(() => Age);
                }
            }
        }
    }

In following posts I will show you how to implement IEditableObject and a Custom Interface IDeletableobject in our client model in order to automatize the updates and delete.

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