Jarloo

Menu

Create a Generic Lookup Item Class

I can’t tell you how many times I make a class for my model layer that basically boiled down to (id,name). This class does that and lets you supply the types. So if you wanted a class that exposed a string as an id and a name as a string you go LookupItem<string,string> etc…

The class uses INotifyPropertyChanged so it can used for DataBinding in WPF correctly.

 

The code

A simple looking class but if you give it a chance you’ll see that in most enterprise apps this has a very big place.

using System.ComponentModel;

namespace Jarloo
{
    public class LookupItem<I, N> : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private I id;
        private N name;

        public N Name
        {
            get { return name; }
            set
            {
                name = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Name"));
            }
        }

        public I Id
        {
            get { return id; }
            set
            {
                id = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Id"));
            }
        }
    }
}

Categories:   Code

Tags:  , ,

Comments