Archive for July, 2008

Lessons Learned: Why Unit tests just won’t cut it

Over the last few days, I have had the wonderful experience of trying to implement a story just to find out, near the end, that nothing in it really worked. I had written unit tests and they all passed. My methods were hitting the dependencies and executing code exactly how I expected. And yet, my code wasn’t working. Worse was the fact that the code was throwing runtime exceptions preventing anything on the site from working.

The problem was that my code was trying to do a sort against data returned from a database call done via LING to SQL. And none of my unit tests exercised that call to the DB.

I was trying to generalized some sorts that I was doing through a builder class that returned a generic sort object. This sort object then performed that required sort on the Queryable. This meant that I had several methods like this:

public Sort<t> BuildSort(string sortBy) {
    // some condition to figure out the proper sort
    return new Sort<t>(properSortMethod());
}
private Expression<func <User, object>> SortByName() {
    return user => user.Name;
}

private Expression<func <User, object>> SortByCreatedDate() {
    return user => user.CreatedDate;
}

private Expression<func <User, object>> SortByIsActive() {
    return user => user.IsActive;
}

The return value of each of these methods was then stored in a Sorts class which performs the sort.

private Expression<func <User, object>> sort;

public Sort(Expression<func <User, object>> sort) {
    this.sort = sort;
}
public IQueryable<t> ApplySort(IQueryable<t> queryable) {
    return queryable.OrderBy(sort);
}

To me, this all makes sense. And when applied to an in memory Queryable list, it works fine. When applied to a database, it croaks.

I learned two lessons out of this: first, always code in small chunks and check those small chunks into Subversion and second, when dealing with database code, always do integration tests early.

For reference, the problem was that LINQ cannot create a Query when the second part of the Expression is set object. It needs to explicitly typed as string, bool or DateTime?. However, using the pattern above, I don’t know how to properly store the sort in such a way that each builder method can return a specific sort and have those applied using a general method. if anyone has any ideas, please let me know.

How to get Phusion Passenger and ImageMagick to work together?

For Studio Gallery, I want people to have the ability to upload avatars for the clients that they create. I am using the Paperclip plug-in for this and when running the app via script/server this works great. I installed ImageMagick and Paperclip is able to find it since all the right paths are set in my .bash_profile file.

The problem is that I can’t get Paperclip or ImageMagick to work when running the app via Passenger. I have added the following lines to my /etc/profile file:

#ImageMagick
export MAGICK_HOME="/usr/local/ImageMagick-6.4.1"
export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib"
export PATH=$MAGICK_HOME/bin:/usr/bin:$PATH

But so far, it seems that Apache or Passenger is unable to find ImageMagick. Anyone have any ideas?

Canon G9 and Lake Agnes

I just purchased the new Canon PowerShot G9 and took it on a hike up to the Lake Agnes Teahouse. The hike took us about 2 hours and 45 minutes and it offers some incredible views. The camera functioned perfectly and took some very nice images. If you are looking for a prosumer point and shoot, I highly recommend it. Here are some pics.

Mirror Lake

Playing with C# 3.0 and .NET 3.5

It has been quite a while since my last post. Mostly that is because I have been fairly busy with a new project and busy trying to get my sorry butt into shape (it is amazing how much time exercising can take up). Anyway, the new project uses C# 3.0 and .NET 3.5, ASP.NET MVC and jQuery. All three are new to me and I have to say that I really like all three.

C# 3.0 is a really nice language. It seems to have gotten generics correct making for some really nice code, especially were the database layer is concerned. We are using LINQ to SQL and we have been able to implement a single class for the repository which works with whatever model object type we give it. I also really like the inclusion of Lambdas and delegates, which make for some very clean code constructs and are much cleaner then anonymous methods in Java.

The second new technology to me is ASP.NET MVC. This allows us to do ASP.NET without actually using ASP.NET. Currently, it is still in preview releases, but it is definitely heading in the right direction. Everything (with the possible exception of views) is completely and easily testable. This means no more having to hide functionality behind presenters or controllers and hoping it works in the code behind. Controllers are completely testable right down to testing what views or redirects they return.

The last new thing I am using is jQuery. I fully understand why it is becoming the defacto Javascript library. To do most things only require a few lines of jQuery Javascript and its AJAX and JSON capabilities are incredible. One nice thing about ASP.NET MVC is that it is capable of returning JSON results with a single line of code. This makes working with jQuery seamless.

If you are starting a new .NET web project, definitely check out ASP.NET MVC, C# 3.0 and jQuery. If you are new to C# 3.0 then check out the book C# in Depth. It highlights all the changes since 1.1 in a nice concise format that will bring you up to speed quickly.