Tuesday, 13 October 2009

NHibernate – When ‘Load’ does ‘Load and Update’

I came across a truly bizarre update occurring within my Gemini Mailbox Processer Application. It was reported to me by a friend who did some monitoring on the application. We were both completely baffled why NHibernate would be doing any form of update on a simple List<IEmail> call. Let’s look at the details.

Firstly, let’s see the interface which defines the class. I have removed some fields for brevity.

 3_Interface

Then here is our code which will load the Emails. Here we create our criteria object, and add some filtering, some ordering, and then retrieve a list of IEmail objects. Nothing seems strange here.

1_Code

This is the mapping (abbreviated version) to the email class. You might spot the issue in here (but I’ll spell it out later)

2_Mapping

Ok, so running this, let’s use NHibernate Profiler to see what is going on.

4_ProfilerUpdate

As you can see, there is a bizarre update statement. At least NHibernate was batching it up into one statement, but WHY! Let’s look at the specific details for this query. Again some items have been removed to keep the snippet small.

5_ProfilerDetails

I decided to comment out lines in the mapping and run a test app to see what results were returned. Each time I ran it until I saw this update appear. Trial an error narrowed it down to the Priority column.

We can see the priority is looking a little strange. In the interface it is an enum, and in the mapping it is specified as an Int32. So it seems strange that NHibernate it sending it as Text? Let’s remove the Type=”Int32” from the mapping and see what happens.

6_ChangeMapping

Rerunning it shows it is all ok now. So while I do not know why, I do now know how to prevent it. I assume it is to do with the type conflict between the enum and specifying it as an Int32.

7_ProfilerFixed

Windows Live?

Apparently you can make posts with Windows Live. It seems fairly straightforward to do.

Just enter your URL, your user name and password and it logs in and downloads stuff. We will see if this post makes it to the blog, or I’ve just compromised the security of the blog :-)

Thursday, 30 July 2009

Could not load file or Assembly CustomMarshalers.dll

I had an interesting error after installing VS2008 and trying to create ANY new project. I tried, web, console, services, and none would be created. Each time I had the following error:

Could not load file or assembly CustomMarshalers, Version=2.0.0.0 etc

For some reason, this file was not installed into the GAC

c:\>gacutil -i C:\Windows\Microsoft.NET\Framework\v2.0.50727\CustomMarshalers.dll

command into the VS2008 command prompt solved the issue.

Useful Links for New Projects

NHibernate Download
NHibernate Homepage

NHibernate Profiler

Resharper

NVelocity and Engine Wrapper


Template Web Project Structure using NHibernate and NVelocity

Wednesday, 15 July 2009

Validation and Testing

We all know that validation of user input is always a good thing. After all, even the most astute of user, is still at the end of the day, a user and that means if there is a way to cock something up, they normally find it... So therefore we validate what a user inputs to make sure it is sensible.

So when I read this article on the BBC news site http://news.bbc.co.uk/1/hi/world/americas/8152278.stm I was left pondering quite what had gone wrong. For those that don't want to read the article, it is basically about a man being charged $23 quadrillion for a packet of cigs! Maybe America are trying a new tactic of health awareness, preventing people from smoking? We'll probably not, since that amount is several times more than the entire national debt of the USA!

Just how could someone be charged so much, and just how big was his credit limit? I want one of those cards! I did find his response of "Something I could never, ever, afford to pay back. " was very astute. If he worked 8 hours a day, for 364 days of the year (well let's have Christmas off!) and for 20 years, he would need to earn $397,473,477,132 per hour. that's $400 Billion an hour.

I want that job :)

Thankfully, the bank has rectified the "blip" and even refunded the $15 overdraft fee. How kind of them :-) perhaps some more unit testing would be beneficial here, it would be most unfortunate if the transaction credited him that much!