Implementing INotifyPropertyChanged in C#5.0 way

 

All XAML developers are well aware of INotifyPropertyChanged interface. This interface is key to the data binding. Every ViewModel class or ViewModelBase abstract class implements this interface. One of the biggest issue with it is that the PropertyChangedEventHandler delegate event argument, PropertyChangedEventArgs constructor accepts the property name as string value. This can introduce bug in your code because of “MAGIC STRINGS” , where by which if developer misspell the property name or you require to rename your property , then it should be done very carefully otherwise your data binding can go completely for a toss and you would go in circles to figure out what the issue is. Let me illustrate what I am talking about with the code snippet below.

Old way implementation
  1. public class CustomerViewModel : INotifyPropertyChanged
  2.     {
  3.         private string customerNameValue = String.Empty;
  4.         private string phoneNumberValue = String.Empty;
  5.  
  6.         public event PropertyChangedEventHandler PropertyChanged;
  7.  
  8.         private void NotifyPropertyChanged(String propertyName)
  9.         {
  10.             if (PropertyChanged != null)
  11.             {
  12.                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  13.             }
  14.         }
  15.  
  16.         public string CustomerName
  17.         {
  18.             get
  19.             {
  20.                 return this.customerNameValue;
  21.             }
  22.  
  23.             set
  24.             {
  25.                 if (value != this.customerNameValue)
  26.                 {
  27.                     this.customerNameValue = value;
  28.                     NotifyPropertyChanged("CustomerName"); //Magic strings
  29.                 }
  30.             }
  31.         }
  32.  
  33.         public string PhoneNumber
  34.         {
  35.             get
  36.             {
  37.                 return this.phoneNumberValue;
  38.             }
  39.  
  40.             set
  41.             {
  42.                 if (value != this.phoneNumberValue)
  43.                 {
  44.                     this.phoneNumberValue = value;
  45.                     NotifyPropertyChanged("PhoneNumber"); //Magic strings
  46.                 }
  47.             }
  48.         }
  49.     }

In this short post I will talk about how you can avoid using these magic strings by using one of C# new 5.0 feature named as CallerMemberName attribute with support of optional parameters. So let us do some code refactoring in the above code and remove these magic strings. Have a look at the code snippet below.

New way implementation
  1. public class CustomerViewModel : INotifyPropertyChanged
  2.     {
  3.         // These fields hold the values for the public properties.
  4.         private string customerNameValue = String.Empty;
  5.         private string phoneNumberValue = String.Empty;
  6.  
  7.         public event PropertyChangedEventHandler PropertyChanged;
  8.  
  9.         private void NotifyPropertyChanged([CallerMemberName]
  10.                                            String propertyName = "")
  11.         {
  12.             if (PropertyChanged != null)
  13.             {
  14.                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  15.             }
  16.         }
  17.  
  18.         public string CustomerName
  19.         {
  20.             get
  21.             {
  22.                 return this.customerNameValue;
  23.             }
  24.  
  25.             set
  26.             {
  27.                 if (value != this.customerNameValue)
  28.                 {
  29.                     this.customerNameValue = value;
  30.                     NotifyPropertyChanged(); //Look ma no magic strings
  31.                 }
  32.             }
  33.         }
  34.  
  35.         public string PhoneNumber
  36.         {
  37.             get
  38.             {
  39.                 return this.phoneNumberValue;
  40.             }
  41.  
  42.             set
  43.             {
  44.                 if (value != this.phoneNumberValue)
  45.                 {
  46.                     this.phoneNumberValue = value;
  47.                     NotifyPropertyChanged(); //Look ma no magic strings
  48.                 }
  49.             }
  50.         }
  51.     }

Just have a look at NotifyPropertyChanged method in the above code snippet. This method argument is decorated with CallerMemberName attribute. This will replace the property name in the set accessor of that property. So you don’t need to hardcode any property name.

On the closing note I would also like to say that this is not the only way you can avoid the MAGIC STRINGS in the method name. If you are fan of functional programming like me 😉 and have some knowledge of generics you can use Linq Expressions to achieve the same. Let us see one more code snippet which does this.

Smart way implementation
  1. public class CustomerViewModel : INotifyPropertyChanged
  2.     {
  3.         // These fields hold the values for the public properties.
  4.         private string customerNameValue = String.Empty;
  5.         private string phoneNumberValue = String.Empty;
  6.  
  7.         public event PropertyChangedEventHandler PropertyChanged;
  8.  
  9.         private void NotifyPropertyChanged<T>(Expression<Func<T>> expression)
  10.         {
  11.             var lambda = (LambdaExpression)expression;
  12.             MemberExpression memberExpr;
  13.             var body = lambda.Body as UnaryExpression;
  14.             if (body != null)
  15.             {
  16.                 var unaryExpr = body;
  17.                 memberExpr = (MemberExpression)unaryExpr.Operand;
  18.             }
  19.             else
  20.             {
  21.                 memberExpr = (MemberExpression)lambda.Body;
  22.             }
  23.             if (PropertyChanged != null)
  24.             {
  25.                 PropertyChanged(this, new PropertyChangedEventArgs(memberExpr.Member.Name));
  26.             }
  27.         }
  28.  
  29.         public string CustomerName
  30.         {
  31.             get
  32.             {
  33.                 return this.customerNameValue;
  34.             }
  35.  
  36.             set
  37.             {
  38.                 if (value != this.customerNameValue)
  39.                 {
  40.                     this.customerNameValue = value;
  41.                     NotifyPropertyChanged(() => CustomerName);
  42.                 }
  43.             }
  44.         }
  45.  
  46.         public string PhoneNumber
  47.         {
  48.             get
  49.             {
  50.                 return this.phoneNumberValue;
  51.             }
  52.  
  53.             set
  54.             {
  55.                 if (value != this.phoneNumberValue)
  56.                 {
  57.                     this.phoneNumberValue = value;
  58.                     NotifyPropertyChanged(() => PhoneNumber);
  59.                 }
  60.             }
  61.         }
  62.         }

As seen this is also cool way of avoiding the magic strings by using linq expressions. Finally a quick tip always create the base class (ViewModelBase) and encapsulate all the required functionalities in it. Then all your viewmodel classes must inherit this ViewModelBase class.

Hope you must have found this post useful. Please leave your valuable feedback in the comments section.

How to read csv file and import its data with Linq

Introduction

I was recently tasked with the project of reading a CSV file and importing its data to SQL Server database. This may sound very common and I believe there are lot of traditional methods to achieve this.As always the time was a big constraint , I wanted some quick solution . I could not recommend SSIS packages as customer was using SQL Server Express Edition.

Scenario

You are given a CSV file with the "," as delimiter of the file. There are three fields in the file

  1. Customer Id
  2. Customer Name
  3. City

The first line of the file has the column headings, this line should not be imported in the table. You have the table called Customer in the database which has the same columns. You are required to import the csv file data into this table. Sounds pretty easy, you must be thinking of reading the file loop through its contents and add the each line record in the table using DML statements.C’mon guys I will show you a better and improved way of doing the same without any loops…, So your eyes are open wide, without loop how is that possible. Well Linq will help us to achieve this fete.

Solution

I knew that File class function ReadAllLines method returns array of string.And there is the catch whatever that implements IEnumerable can be queried with Linq. As I needed to store the csv data into the table. I had already added Linq to SQL class in my solution. So let us see some code.

Dim dataImport = From line As String In File.ReadAllLines("YourFileNamewithFullPath") _
                         Skip 1 _
                         Let CR = line.Split(",") _
                         Select New TblCustomer With {.CustomerId = CR(0), _
                                                 .CustomerName = CR(1), _
                                                 .City = CR(2)}

The above code reads the array of lines using Linq syntax and store each line in CR variable by splitting the line contents with delimiter using Let statement. This record is then stored in constructor of the TblCustomer, which is the Linq to SQL Class for Customer table in the database.Notice that I am using Skip clause for not to read the file header, which are actual the column names in the csv file.So now your dataImport variable contains all the data of the csv file excluding the header. Now this data needs to be imported into the Customer table in the database. Let us have look at the code for the same.

Ctx.TblCustomer.InsertAllOnSubmit(dataImport)

Ctx.SubmitChanges()

Here Ctx is the object variable of Linq Data Context class. The InsertAllOnSubmit method will do bulk insert of the data provided to it as a collection object in its parameter. So that’s it with just two lines of the code I have imported data into my table. Kudos to Linq

Closure

I always like to do the old things in newer way and this was the classic example of it. I hope you must be agreeing with my thoughts, use Technology to best of its advantage otherwise don’t use. Happy coding…