ProductBase.cs
C#
Copy Code
public class ProductBase :
   INotifyPropertyChanged
{
    static string[] _lines = "Computers|Washers|Stoves".Split('|');
    static string[] _colors = "Red|Green|Blue|White".Split('|');
    static Random _rnd = new Random();

    // ctor
    public ProductBase()
    {
        Line = _lines[_rnd.Next() % _lines.Length];
        Color = _colors[_rnd.Next() % _colors.Length];
        Name = string.Format("{0} {1}{2}", Line.Substring(0, Line.Length - 1), Line[0], _rnd.Next(1, 1000));
        Price = _rnd.Next(1, 1000);
        Cost = _rnd.Next(1, 600);
    }

    // object model
    [Display(Name = "Line")]
    public virtual string Line
    {
        get { return (string)GetValue("Line"); }
        set { SetValue("Line", value); }
    }

    [Display(Name = "Color")]
    public virtual string Color
    {
        get { return (string)GetValue("Color"); }
        set { SetValue("Color", value); }
    }

    [Display(Name = "Name")]
    public virtual string Name
    {
        get { return (string)GetValue("Name"); }
        set { SetValue("Name", value); }
    }

    [Display(Name = "Price")]
    public virtual double? Price
    {
        get { return (double?)GetValue("Price"); }
        set { SetValue("Price", value); }
    }

    [Display(Name = "Cost")]
    public virtual double? Cost
    {
        get { return (double?)GetValue("Cost"); }
        set { SetValue("Cost", value); }
    }

    // get/set values
    Dictionary<string, object> _values = new Dictionary<string, object>();
    protected virtual object GetValue(string p)
    {
        object value;
        _values.TryGetValue(p, out value);
        return value;
    }
    protected virtual void SetValue(string p, object value)
    {
        if (!object.Equals(value, GetValue(p)))
        {
            _values[p] = value;
            OnPropertyChanged(p);
        }
    }
    protected virtual void OnPropertyChanged(string p)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(p));
    }

    // factory
    public static string[] GetLines()
    {
        return _lines;
    }
    public static string[] GetColors()
    {
        return _colors;
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;