It’s Easy to Forget

I’m a big fan of Linq. The more I learn about functional programming, the more I love (and use) Linq.

However with any higher level abstraction there is the possibility of the mysterious inner workings differing from your intuitive expectations. (This one of the dangers of frameworks being ‘too helpful’ to developers.)

Today I was looking at some code for creating new accounts for a web app. We query to see if the account name and email address are unique before adding a new account.

var matching = from account in _session.Linq<Account>()
               where account.Name == requeseted.AccountName
                     || account.EmailAddress == requeseted.EmailAddress
               select account;

I had a single query using NHibernate.Linq. Then I used the query in something like this:

if (matching.Count() <= 0) return; // no matches so move on…

if (matching.Any(x => x.Name == requested.AccountName))
{
    result.AddError(x => x.AccountName,
                     "The account name you chose is not available.");
}

if (matching.Any(x => x.EmailAddress == requested.EmailAddress))
{
    result.AddError(x => x.EmailAddress,
                     "That email address is already in use.");
}

When monitoring this with NHProf I found that it executed three separate queries. One for the Count() and one for each Any(). I had only expected one.

Of course, after seeing it I immediately realized why it was happening but I had been ignorantly skipping along when I wrote the code.

It’s an easy mistake to make.


Posted 12-31-2009 2:32 PM by Christopher Bennage
Filed under: ,

[Advertisement]

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)