The Dirty Lies of Function Evaluation

One of the greatest things about .NET, in my opinion, is the debugger built into Visual Studio. For a developer, a great debugger can’t be beat. Unfortunately, there are some cases that a great debugger can cause more harm than help.

Since I started using NHibernate, I got into the habit of tail-ing the MySQL query logs. I have written a lot of database access code, and still don’t trust NHibernate. I started noticing weird problems when I started using NHibernate’s Load() function. According to the documentation, no query should be performed in this case. But a query was occurring.

After some digging, I realized the debugger was calling the ToString() method on the object. Since the object is proxied, any access to it causes a query to be loaded. A simple setting in Visual Studio fixes the problem. Just uncheck Tools->Options->Debugging->Enable property evaluation and other implicit function calls. This will cause the code to act more like release code, but will make debugging a little harder since all properties can’t be accessed.