Friday, January 25, 2013

Implementing a flexible BaseObject

This blog is one of the exciting topic because this will give us a very common flexible base object class that we can inherit in every class of our application.

But before we continue, we are requiring you to read the following blog.

Once you're done in the topics above, then you're ready to go in this topic.

First we need to create a class named BaseObject that inherits the PropertyNotifier we created in the previous blog. See below.

[Serializable]
public abstract class BaseObject : PropertyNotifier
{
    public BaseObject()
    {
    }
}

Then we need to create a dictionary object that holds the value for our properties. This dictionary object will also be the variable that we will be using in the entire class manipulation. Let's name this dictionary object as __values. Note that we need to pass the StringComparer.CurrentCultureIgnoreCase value during the construction as it will be eliminated the case sensitive checking the dictionary keys. See below the codes.

private IDictionary<string, object> __values = new Dictionary<string, object>(StringComparer.CurrentCultureIgnoreCase);

After that we need to create a method that will set/get the property values on this class. In our case to make it very common in the purpose, let us name it GetValue and SetValue. This method uses the dictionary object we had declared above to save the values in the memory. For the generic way implementation, we need to create a type-safe function of the GetValue so it is easy for the derive class to use it. See below the codes.

public T GetValue<T>(string key)
{
    if (string.IsNullOrEmpty(key))
    {
        return default(T);
    }
    var value = this.GetValue(key);
    if (value is T)
    {
        return (T)value;
    }
    return default(T);
}

private object GetValue(string key)
{
    if (string.IsNullOrEmpty(key))
    {
        return null;
    }
    if (this.__values.ContainsKey(key))
    {
        return this.__values[key];
    }
    return null;
}

public void SetValue(string key, object value)
{
    if (!this.__values.ContainsKey(key))
    {
        this.__values.Add(key, value);
    }
    else
    {
        this.__values[key] = value;
    }
    base.OnPropertyChanged(key);
}

Now base on the code above as you analyze it, the very common purpose of it is to save/get the value from the dictionary. Also notice that on the SetValue method, we have called the OnPropertyChanged event of the base class PropertyNotifier. The purpose of this is to notify the listener of the PropertyChanged event on the current property.

Now that's it. That is just the dynamic and flexible object that we can use on our entire application. See the codes below the actual class implementation.

public abstract class BaseObject : PropertyNotifier
{
    #region Privates

    private IDictionary<string, object> __values = new Dictionary<string, object>(StringComparer.CurrentCultureIgnoreCase);

    #endregion

    #region Methods

    public T GetValue<T>(string key)
    {
        if (string.IsNullOrEmpty(key))
        {
            return default(T);
        }
        var value = this.GetValue(key);
        if (value is T)
        {
            return (T)value;
        }
        return default(T);
    }

    private object GetValue(string key)
    {
        if (string.IsNullOrEmpty(key))
        {
            return null;
        }
        if (this.__values.ContainsKey(key))
        {
            return this.__values[key];
        }
        return null;
    }

    public void SetValue(string key, object value)
    {
        if (!this.__values.ContainsKey(key))
        {
            this.__values.Add(key, value);
        }
        else
        {
            this.__values[key] = value;
        }
        base.OnPropertyChanged(key);
    }

    #endregion
}

Now, see the code below how did we use the newly created BaseObject class in our derived class.

public class Person : BaseObject
{
    public Person(string name, DateTime birthDate)
    {
        this.Name = name;
        this.BirthDate = birthDate;
    }

    public string Name
    {
        get { return base.GetValue<string>("Name"); }
        set { base.SetValue("Name", value); }
    }

    public DateTime BirthDate
    {
        get { return base.GetValue<DateTime>("BirthDate"); }
        set { base.SetValue("BirthDate", value); }
    }
}

Then, after creating an object of type Person, you will also notice that you can listen to the PropertyChanged event of the object. See the codes belw.

public App()
{
    var person = new Person("Stanley Harris", new DateTime(1978, 3, 22));
    person.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(person_PropertyChanged);
}

void person_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    /* do more stuff here */
}

Pretty interesting right? There are more interesting stuffs coming.

I hope you guys really understand how did we do the stuff above as this is very important and a pre-requisites on the next more exciting blogs on this site.

No comments:

Post a Comment

Place your comments and ideas