Wednesday, December 26, 2012

Implement GetHashCode() to track changed objects in C#

Let's say we pull information about Movies from remote XML and save it in our database. Class that represent this objects can look like:
 public class Movie  
 {  
     public int Id { get; set; }  
     public string Name { get; set; }      
     public int? Duration { get; set; }  
     public int? Director { get; set; }      
     public int[] Actors { get; set; }      
     public int[] Genres { get; set; }      
     public int[] Countries { get; set; }      
     public string PosterUrl { get; set; }  
     public double? Rate { get; set; }  
     public int? Votes { get; set; }  
     public double? Imdb { get; set; }  
     public int? ImdbVotes { get; set; }  
     public string Site { get; set; }  
     public string Limits { get; set; }  
     public DateTime? Date { get; set; }  
     public string Description { get; set; }  
     public string Trailers { get; set; }  
     public string Company { get; set; }      
 }  

Write operations are expensive if there are a lot of objects to be saved, so for performance reasons we should NOT save new object in database, if there is old object in database that is the same. So we need to extract object from databse and determine if new object differ from the old one. If objects are the same we DO NOT update it.

But how to determine that objects are the same?

Objects are differ if their contents differ. In our case we can clarify this as follows: Objects are differ if their properties are differ.

Let's override GetHashCode() and Equals() methods to define how to check equality of 2 Movie objects:
 public class Movie  
 {  
    public int Id { get; set; }  
    public string Name { get; set; }  
    public int? Duration { get; set; }  
    public int? Director { get; set; }  
    public int[] Actors { get; set; }  
    public int[] Genres { get; set; }  
    public int[] Countries { get; set; }  
    public string PosterUrl { get; set; }  
    public double? Rate { get; set; }  
    public int? Votes { get; set; }  
    public double? Imdb { get; set; }  
    public int? ImdbVotes { get; set; }  
    public string Site { get; set; }  
    public string Limits { get; set; }  
    public DateTime? Date { get; set; }  
    public string Description { get; set; }  
    public string Trailers { get; set; }  
    public string Company { get; set; }  
    
    public override int GetHashCode()  
    {  
       return HashCodeGenerator.Generate(Id, Name, Duration, Director, Actors, Genres, Countries, PosterUrl, Rate, Votes, Imdb, ImdbVotes, Site, Limits, Date, Description, Trailers, Company);  
    }  
    public override bool Equals(object obj)  
    {  
       return this.GetHashCode() == obj.GetHashCode();  
    }        
  }  

As you can see, we use custom class HashCodeGenerator to generate hash code from specified property values that we need to track.

Implementation of this class:
  public static class HashCodeGenerator  
  {  
     public static int Generate(params object[] objects)  
     {  
       unchecked  
       {  
         var offset = 13;  
         var compositionOffset = 7;  
         foreach (object obj in objects)  
         {  
           if (obj==null)  
             continue;  
           if (obj is Array)  
           {  
             var array = obj as Array;  
             foreach (var item in array)  
             {  
               offset = (offset * compositionOffset) + item.GetHashCode();  
             }  
           }  
           else  
           {  
             offset = (offset * compositionOffset) + obj.GetHashCode();  
           }            
         }            
         return offset;  
       }  
     }      
   }  

Wednesday, December 19, 2012

Using expression trees to get rid of "magic" strings in C#

"Magic" strings may appear in programming code in many cases. For example, if we need to pass property names to some method.

Let's say we need to make DatabaseIndexCreator class that creates database index for specified property name(or properties, if index is compound) of some model(in our example it's Person class):
 public class Person  
 {  
   public string Name { get; set; }  
   public string Surname { get; set; }  
   public bool Sex { get; set; }  
 }  

So, we end up with function that accept property names as strings:
 public class DatabaseIndexCreator<TEntity>  
 {  
   public void CreateIndex(string[] fields)  
   {  
     //creating index  
   }  
 }  

And use this class like this:
 DatabaseIndexCreator<Person> indexCreator = new DatabaseIndexCreator<Person>();  
 //creating simple index:  
 indexCreator.CreateIndex(new[] { "Name" });  
 //creating compound index:  
 indexCreator.CreateIndex(new[] { "Name", "Sex" });  

It's bad design, because you can forget to rename this "magic" strings after renaming properties of Person class.

But you can use Expression Trees - nice C# feature to get rid of this "magic" strings in your code.

Let's create class with extension method for Expression<Func<T, U>> expression:
  public static class ExpressionEx  
  {  
     public static HashSet<string> AsMemberExpressionKeys<T, U>(this Expression<Func <T, U>> index)  
     {  
       var exp = index.Body as NewExpression;  
       var keys = new HashSet<string>();  
       if (exp != null)  
       {  
         foreach (var x in exp.Arguments.OfType<MemberExpression>())  
         {  
           keys.Add(GetPropertyAlias(x));  
         }  
       }  
       else if (index.Body is MemberExpression)  
       {  
         var me = index.Body as MemberExpression;  
         keys.Add(GetPropertyAlias(me));  
       }  
       return keys;  
     }  
     private static string GetPropertyAlias(MemberExpression mex)  
     {  
       var retval = "";  
       var parentEx = mex.Expression as MemberExpression;  
       if (parentEx != null)  
       {  
         retval += GetPropertyAlias(parentEx) + ".";  
       }        
       retval += mex.Member.Name;  
       return retval;  
     }  
  }  

What benefits can we get from this tricky extension method?

Now we can refactor our DatabaseIndexCreator class like this:
 public class DatabaseIndexCreator<TEntity>  
 {  
   public void CreateIndex<TReturn>(Expression<Func<TEntity, TReturn>> exp)  
   {  
     string[] fields = exp.AsMemberExpressionKeys().ToArray();  
     //creating index(simple or compound)  
   }  
 }  

And use this class like this:
 DatabaseIndexCreator<Person> indexCreator = new DatabaseIndexCreator<Person>();  
 //creating simple index:  
 indexCreator.CreateIndex(t => t.Name);  
 //creating compound index:  
 indexCreator.CreateIndex(t => new {t.Name, t.Sex});  

As you see, there is no more "magic" strings, that is very good for our code design and our nerves.

Wednesday, December 12, 2012

Redirect www subdomain to root domain in ASP.NET

Let's say we want to perform permanent redirect from http://www.example.com address to http://example.com in our ASP.NET application for SEO purposes(URL canonization).

First of all, we need to download Rewrite module for IIS7:
http://www.microsoft.com/web/gallery/install.aspx?appid=urlrewrite2

Then you need to open IIS Manager and navigate to URL Rewrite section.

After proper configuration in IIS Manager you'll get section in your Web.config that can look like:
 <configuration>  
   <system.webServer>  
     <rewrite>  
       <rules>  
         <rule name ="CanonicalHostNameRule" stopProcessing="true">  
           <match url="(.*)" />  
           <conditions>  
             <add input ="{HTTP_HOST}" pattern="^www\." negate="false" />  
           </conditions>  
           <action type ="Redirect" url="http://example.com/{R:1}" />  
         </rule>  
       </rules>  
     </rewrite>  
   </system.webServer>   
 </configuration>  

Thursday, December 6, 2012

Cancelable tasks in .NET 4.0

There is new nice feature in .NET >= 4.0 - cancelling asynchronoums tasks.


Now you can do this:
 public class JobClass  
   {      
     private readonly CancellationTokenSource _cancellationSource;  
     public JobClass()  
     {  
       _cancellationSource = new CancellationTokenSource();        
     }              
     public void DoWork()  
     {  
         ...  
         Action action = () =>  
          {             
            foreach (var a in array)  
            {               
             /*do something*/  
             _cancellationSource.Token.ThrowIfCancellationRequested();  
            }  
          };  
        var task = new Task(action, _cancellationSource.Token);  
        task.Start();         
     }  
     public void Cancel()  
     {  
       _cancellationSource.Cancel();  
     }                       
   }  

As you can see, we created Task with cancellation Token. After this we can use cancellationSource to cancel this Task any time we want. It should be noted, that we use Token.ThrowIfCancellationRequested() method in async method body to throw exception if cancellation was requested.