Sunday 5 August 2012

Sunday 5 June 2011

Working with Entity Framework Profiler

I am very new to Entity Framework and only just getting my head around using it in my own projects.  But first I had an application with I knew was built on EF but it was exhibiting some concerning behaviour.  It was using a large number of queries to the database as determined from the SQL Profiler that comes with Sql Server 2005.  I felt this was a little excessive and suspected a SELECT N+1 Anti-Pattern was involved.

But how do I prove it?

I knew of a great sounded tool called EFProfiler and it sounded like a great way to get concrete evidence that the application was not playing nice with the database.

So I downloaded the trial version of EFProfiler to test my hypothesis.

But I run into a problem.

The documentation says I have to add a reference to a dll and in Application_Start in the Global.asax file, I have to add a single line of code.  This is a problem as I do not have access to the code and so I am very limited as to what I can change.

I consider what my options are and I recall that another great tool I make use of called Elmah has a means of easily slipping itself into a web application without needing to add code to reference it.  How it does this is by being an HttpModule.

HttpModules can be added to an application through the web.config which references the dll the module is conpiled and becomes part of the HttpApplication pipeline.  But my first read of the documentation, I can hook into events like BeginRefresh, but there is no indication that I can hook into Application_Start.

This is until I found this question on StackOverflow which lead me to an article on How to correctly use IHttpModule to handle Application_OnStart event.

So I quickly throw together a little project to create my own HttpModule.

Imports System.Web
Imports HibernatingRhinos.Profiler.Appender

Public Class EFProfilerModule
  Implements IHttpModule

  Private Shared ApplicationStarted As Boolean = False
  Private Shared ApplicationStartLock As New Object()

  Public Sub Dispose() Implements System.Web.IHttpModule.Dispose

  End Sub

  Public Sub Init(application As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init

    If Not ApplicationStarted Then
      SyncLock ApplicationStartLock
        If Not ApplicationStarted Then
          Me.OnStart(application)
          ApplicationStarted = True
        End If
      End SyncLock
    End If

    Me.OnInit(application)

  End Sub

  Public Overridable Sub OnStart(application As HttpApplication)

    EntityFramework.EntityFrameworkProfiler.Initialize()

  End Sub

  Public Overridable Sub OnInit(application As HttpApplication)

    ' put your module initialisation code here

  End Sub

End Class

Then all I needed to do was add two dll’s to the bin folder of the application.  The first being my dll with my customer HttpModule and the other being the file HibernatingRhinos.Profiler.dll.  Then I just added one line to the HttpModules section of the web.config.


<add type="CustomModule.EFProfilerModule" name="EFProfilerModule" />

I then fired up EFProf and navigated to the application and it worked!  EFProf started listing sessions and I could immediately see a number of red and grey dots signifying there were some issues found!

After just going to the login screen, signing in and taken to the home page (only two pages) the profiler picked up the following…

EFProf AnalysisNavigating to the login screen only listed two object contexts.  Signing in and loading the Home page produced 16 more object contexts.

I had my evidence and analysis from a commercial product!

My team are looking to use Entity Framework for data access in our projects and to help prevent ourselves from writing sub-optimal queries I will be advocating we purchase the full commercial version of EFProf as it takes all the pain away from detecting issues with your data access code and gives you guidance to fixing any problems.

Saturday 30 April 2011

Day 30 – Where do you see technology advancing in the next 20 years?

In IT 20 years is a very long time…

Just look back 20 years to see have far we have come!

Assuming that world economics do not collapse.  Computers will be smaller, faster and more ubiquitous.  But that prediction is so obvious.

Hopefully, the world has given up on 3D or at least in the forms that the powers that be are trying to work it.

I image CGI will have finally surpassed the uncanny valley to the point no human can distinguish a real actor from a CGI character.

I’d like to think that we have finally reached out to the asteroid belt.  We are fast running out of rare metals on Earth and before that happens, we will be forced to explore the solar system for other rich sources of raw materials.  Otherwise we are going to hit a rather nasty barrier to further enhancements in technology.  I do not believe we will find any salvation in any green alternative.

In 20 years HTML5 should be well entrenched and HTML6 should be ratified within the next decade or two.

Chrome will be almost celebrating version 100!

IE will be up to version 30 and celebrating Native HTML6.

The Google/Facebook War of 2021 will be mourning their 10th anniversary.

After a four term run as President of the European Union, Arnold Schwarzenegger will be committed after having a mental breakdown where he has been repeated found running around trying to save John Connor.

As for my involvement, I would most likely have no involvement what-so-ever.  I’ll just be home looking after my family, while doing my best to keep up with the latest and greatest tools and gadgets.

Friday 29 April 2011

Day 29 – Looking back (at geek life) would you’ve done anything different?

Reflections…

Only one thing.  I would not wait 10+ years before getting into the IT industry.  I imagine things would have very different.

Having said that, I would not trade my wonderful wife and two adorable children for the world.

Thursday 28 April 2011

Day 28 – How many computers lying about the house?

How long is a piece of string?

Six. 

Plus one PC which is in bits. 

But do you count PS3 and AppleTV?  And if you include AppleTV, should I include iPod Touch and iPhone (not that I have an iPhone).  And if I include iPhone, do I also include other smartphones?

Wednesday 27 April 2011

Day 27 – Fix a bug in some open source software and commit a patch

I got this one covered!

April 7th 2011 I submitted a patch (#9077) to ClosedXML.codeplex.com!  Hopefully that is close enough Open-mouthed smile

Note: I just noticed (23rd April) that the main developer has mentioned me in the latest change set.  I’m not sure if that is a good thing or not Confused smile.

Tuesday 26 April 2011

Day 26 – Apple – friend, foe or other?

Am I an Apple Fanboi?

I feel Apple was once more “friend” than anything just because it was an underdog but this is no longer the case.  As much as I love the iPod Touch and the iPhone, Apple has a stranglehold on what developers can and cannot create for Apple’s iOS devices and this is a real shame.  It probably wouldn’t so much of an issue if Apple’s approval process wasn’t so inconsistent. 

Developers are at Apple’s mercy.

And yet despite this their dictator like rule over apps for iOS devices, it has managed to be hugely successful regardless.  Perhaps Apple are actually on to something…

As an iPod Touch owner, I lean towards “friend”, but as a developer Apple are edging towards “foe” but only because of the steep Developer Tax™ I’ll be charged (namely an Apple computer)