Friday, August 26, 2011

C#: A better PropertyChanged

If you have to implement and use IPropertyChanged in your code then you might feel annoyed by all the code that must be written to support the PropertyChanged event:
public class Product : INotifyPropertyChanged
{
 public event PropertyChangedEventHandler PropertyChanged;

 private int _productId;
 public int ProductId
 {
  get { return _productId; }
  set
  {
   _productId = value;
   RaisePropertyChanged("ProductId");
  }
 }

 private string _name;
 public string Name
 {
  get { return _name; }
  set
  {
   _name = value;
   RaisePropertyChanged("Name");
  }
 }

 protected void RaisePropertyChanged(string propertyName)
 {
  if (PropertyChanged != null)
   PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
 } 
}
Not only is this code trivial and repetitive but since the property name is passed as a string then it can easily lead to typos. Luckily there is a better way to do this:
public class Product : PropertyChangedBase<Product>
{
 public int ProductId
 {
  get { return GetPropertyValue(x => x.ProductId); }
  set { SetPropertyValue(x => x.ProductId, value); }
 }

 public string Name
 {
  get { return GetPropertyValue(x => x.Name); }
  set { SetPropertyValue(x => x.Name, value); }
 }
}
PropertyChangedBase:
public class PropertyChangedBase<T> : INotifyPropertyChanged
{
 private readonly Dictionary<string, object> _properties = new Dictionary<string, object>();
 public event PropertyChangedEventHandler PropertyChanged;

 protected TProp GetPropertyValue<TProp>(Expression<Func<T, TProp>> property)
 {
  var name = GetPropertyName(property);
  return (TProp) _properties[name];
 }

 protected void SetPropertyValue<TProp>(Expression<Func<T, TProp>> property, object value)
 {
  var name = GetPropertyName(property);
  _properties[name] = value;
  RaisePropertyChanged(name);
 }

 protected void RaisePropertyChanged(string propertyName)
 {
  if (PropertyChanged != null)
   PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
 }

 private string GetPropertyName<TProp>(Expression<Func<T, TProp>> property)
 {
  return (property.Body as MemberExpression).Member.Name;
 }
}

Sunday, August 21, 2011

New job as a Consultant at Capgemini

31st of March I had my last day at Mamut, one of the leading software houses in Europe. It was a great company to work for, with great employees and great products and I truly enjoyed my time there. But everything has an end. After reviewing things I thought it was time for a change in order to get more experience from other industries and companies.

On the 1st of April I had my first working day at Capgemini, one of the worlds largest and leading IT consultancy company. So far I'm very satisfied. In a short time I've got to meet many new people and learn and experience new things.

How to enable automatic logon in Windows 7

This is how you can enable automatic logon in Windows 7:

1. Press the Windows key on the keyboard + r. This will open the Run dialog.
2. Enter "netplwiz" and click OK.
3. The User Accounts dialog will now show. Uncheck the box "Users must enter a user name and password to use this computer."
4. Click OK.

C#: How to get the all the field/column names in a DataReader

The following helper method writes out the index position, the name and the data type for all the fields/columns in a DataReader, to the output window in Visual Studio:

public static void DataReaderDebugger(IDataReader reader)
{
 System.Diagnostics.Debug.WriteLine("### DataReader Debug Info ###");
 for (int i = 0; i < reader.FieldCount; i++)
 {
  string info = string.Format("{0}. {1} ({2})",
        i.ToString(),
        reader.GetName(i),
        reader.GetFieldType(i).Name);

  System.Diagnostics.Debug.WriteLine(info);
 }
 System.Diagnostics.Debug.WriteLine("######");
}