Gray's Matter
Justice Gray - North America's favorite metrosexual software consultant

I Wish These People Updated More Than Once a Year

starwars1.jpg

[This posting series had been on hiatus for a long time, as I assumed knowing the difference between processes and threads as well as distinguishing between HTTP GET and POST would have been enough for my grandmother to squeak by a Corillian interview.  However, it's been a little while and she hasn't gotten any callbacks from Scott Hanselman so I am going to continue with my series based on his previous sets of interview questions.]

So what is the GAC (Global Assembly Cache)?

Let's start with Donald Belcham - EDMUG president, unrepentant alcoholic, collector of restraining orders...and unabashed Star Wars fan.

Donald has a Star Wars party at his house once a month.  You might be wondering why you've never been invited to one of these, and the answer is that no one is invited to these parties except Donald, who proceeds to dress up in costume and practice light-saber swinging ala George Michael Bluth from Arrested Development.

swkid.png
Donald Belcham's Star Wars Party: The early years

The key ingredient to a "successful" Donald Belcham Star Wars Party is the presence of the original Star Wars movie.  Every time he holds his party, Donald goes out and rents a copy of Star Wars.  For many Saturday nights, Donald has watched and cheered alone and drunk in his apartment as Han shoots Greedo in cold blood.  This is what leads him to his frenzied Star Wars Kid style light-saber swinging!  Two hours later, exhausted, he collapses in a drunken heap on the floor, with another Saturday night well spent.

However, Matthew, another erstwhile Star Wars fan, wants to start having a Star Wars party of his own - one with other people attending!  However, seeing as Matthew is only 14 years old, he has only grown up knowing the heavily revised version of Star Wars released in 1997**.   Matthew doesn't want to watch the original version, so he calls the video store every day asking when they will have the new version.  Eventually the video store relents and the revised version is stocked in their store.

The video store's tracking system has a problem, however - adding the new Star Wars version into their system has caused a conflict with the old version - such that the old version has been lost for rental. Anyone thinking they are renting the old version (or in Donald's case, depending on it), is in for a nasty surprise.  

Donald goes home for his monthly Star Wars party and eagerly anticipates the scene that will lead him into a lightsaber swinging frenzy.  However, once seeing Greedo shooting first and Han lamely shooting back, Donald curls into the fetal position and loudly sobs for 2 hours in front of the TV until falling asleep - much like most of his other Saturday nights.

The next day, Donald races to the video store and then begins swearing angrily at the store staff, small children, and a puppy about how they have wrecked his life.  The video store apologizes: there is not way to resolve the conflict such that both the old version and the new version are rentable at the same time.  Either Donald's party fails or Matthew's does.  Donald tells the video store that he will never buy anything from the store again as long as he lives and storms out.

One of the tellers realizes that Donald has spent thousands of dollars over the past 10 years renting the same copy of Star Wars and returning it six days late.  Faced with the loss of their main revenue stream, the store works on fixing its tracking system such that *both* movies are rentable from the outlet.

Quite a while later, Donald passes by the video store to see a sign proclaiming that they now rent *both versions* of Star Wars.  They now exist side-by-side in the tracking system!  Realizing that he can finally stop having his "When Stella Got Her Groove Back" solo parties, Donald rushes into the store to rent his favorite film and reclaim his life.  Now Donald's party resumes without chaos, and Matthew can have his as well!!  Everyone is happy.

leia_bikini.jpg
Princess Leia trying to roll back her system to an earlier version pre-GAC

What I've actually described here is life before and after the Global Assembly Cache (the GAC for short).  Let's go back to life before the GAC first.  Say that we have a program on our machine called "Donald Star Wars Party".  This executable has the following supporting DLLs installed to the C:\StarWars directory, with the last three all being dependent on the first:
StarWars.dll (version 1.0) [has a method called HanShootsFirst()]
HanAndGreedo.dll
LightSaberBattles.dll
DarthVaderIsAFatWhiteGuy.dll
Jabba.dll


Several weeks later, we install a different version of the program called "Matthew Star Wars Party" which puts the following DLLs into the same C:\StarWars directory (note which ones I have bolded):
StarWars.dll (version 2.0) [has a method called HanIsAWussy()]
HanAndGreedo.dll
DarthVaderIsHaydenChristensen.dll
Jabba.dll


Once this program is installed, the "Donald Belcham Star Wars Party" program boots up but then crashes shortly after running.  Why is this?  The HanAndGreedo DLL has a method call to HanShootsFirst() that no longer exists in the new Star Wars DLL, as it has been removed and replaced with HanIsAWussy()*.  Furthermore, we have a problem - even if we had a way to roll back the StarWars.dll file and getting the "Donald Star Wars Party" program working, this just means we end up breaking the "Matthew Star Wars Party" program instead.

Now, let's say that instead of installing their DLLs to the same StarWars directory on the hard drive, these programs instead placed their DLLs in the Global Assembly Cache.  When the second StarWars DLL is installed to the GAC, the GAC looks at the major, minor, build, and revision numbers on the DLL.  Since they are different from the StarWars.dll that is already in the GAC, this file is *also* seemingly placed in the GAC without overwriting the existing version!  Actually, what really happens is that the DLL is placed in a hidden subfolder that is a hash of these numbers.  For all intents and purposes it looks like you've just stuck multiple versions of the assembly in the same directory without overwriting.  This enables both programs to run properly, each targeting their own specific version of the StarWars.dll.

Only a couple more of these and you'll get to meet Scott, Granny!  I promise! 

* You might be thinking that if these DLLs were properly designed, that the methods should likely be in the HanAndGreedo.dll and that the older method would be left in for backwards compatibility without making a change that breaks all older code.  And if you are thinking that, you're miles ahead of a particular ASP.NET web control development company...;)
** While I'm not 14 years old, I've actually never seen the original Star Wars movies, only the 1997 versions.  So I can empathize!


Wednesday, 26 July 2006 #