Unit testing events – Asserting event rasing

I was working on a Silverlight application where property setter raises PropetyChanged events for other read only properties that needs to be updated. Something like,

private List<User> users;
public List<User> Users
{
      get
      {
           return users;
      }
      set
      {
           users = value;
           Changed("Users");
           Changed("EnableDisableButton");
           Changed("UsersCount");
      }
}

private void Changed(string propertyName)
{
      if (PropertyChanged == null) return;
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

Here is how I came up with a test case that asserts the proper events are raised in proper order.

[Test]
public void ProperEventsAreRaisedWhenUsersChanges()
{
       var propertiesChanged = new List<string>();
       var model = new UserModel();
       model.PropertyChanged += (sender, args) => propertiesChanged.Add(args.PropertyName);
       model.Users = new List<User>();
       propertiesChanged[0].ShouldBe("Users");
       propertiesChanged[1].ShouldBe("EnableDisableButton");
       propertiesChanged[2].ShouldBe("UsersCount");
}

ShouldBe() is an extension method which does Assert.That internally.

What is the difference between typeof(Foo) and myFoo.GetType()?

This question is taken from Scott Hanselman‘s What a .NET developer should know post. To understand the difference, let us write a simple program.

class Foo
{
    /* .... */
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("typeof(Foo) = " + typeof(Foo).Name);
        Foo f = new Foo();
        Console.WriteLine("f.GetType() = " + f.GetType().Name);
    }
}

Here is the output.

command-prompt

Let us add a subclass for Foo and try again.

class Foo
{
}

class SubFoo : Foo
{
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("typeof(Foo) = " + typeof(Foo).Name);
        Foo f = new SubFoo();
        Console.WriteLine("f.GetType() = " + f.GetType().Name);
    }
}

command-prompt1

In general, f.GetType() returns runtime type of the instance. typeof(Foo) gives the compile time type of Foo.

Single click builds for different environments using Visual Studio

A neat build system is vital for all projects. Good build system should be capable to work without *any* user intervention. Here is what Joel Spolsky says on his post

Can you make a build in one step?

By this I mean: how many steps does it take to make a shipping build from the latest source snapshot? On good teams, there’s a single script you can run that does a full checkout from scratch, rebuilds every line of code, makes the EXEs, in all their various versions, languages, and #ifdef combinations, creates the installation package, and creates the final media — CDROM layout, download website, whatever.

Ask yourself the same question – Can you build your application in one step? Continue reading

ADO.NET best practices – Reading data from data reader

I have seen many people using DataReader incorrectly. In this post, I will try to explain some good practices that can be followed when reading from a data reader. Consider the following problematic code,

SqlDataReader reader = /* ... */;
while (reader.Read())
{
    string userName = reader["user_name"].ToString();
    int age = int.Parse( reader["age"].ToString() );
    /* ... */
}
reader.Close();

How many problems can you figure out from the above code? There are many problems with this code, Continue reading

Limiting number of objects for a class

I have seen in many interviewers asking about how we can limit the number of objects created for a class. This post explains how to do this in C#. This can be done using a static variable.  Static variable will be invoked before the execution of constructor and it’s value will be destroyed when associated application domain ends.

In this we discuss two methods to limit the class instance to a particular number say 5 Continue reading

Using Transactions in C#

When programming with c# and SQLServer, you will come into a situation where you need to execute more than one SQL statements, and ensure all commands executed successfully. If one fails, then the entire process should be aborted and moved to the initial stage.

In this scenario we need to make use of transactions. Transactions can be written inside a stored procedure or it can be written inside a C# program. ADO.NET comes with a SQLTransaction class, which can be used for this. This post explains transactions with working example.

We use pubs database for the examples. Executing following scripts will create two tables, Table1 and Table2. Continue reading

Generics in C#


public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable
{
}

Any body seen a class something similar to this, and surprised about what would be <T> means ? Stack class can be called as Generic type. They are newly introduced in .NET2005. It’s something similar to C++ templates. But it is not exactly like templates. It lacks some functionalities that templates have.

What does a Generic class or method means ? Continue reading