Magic Numbers: Lessons Learned

Magic numbers will kill you.  I set up a stored procedure many years ago that included the following TSQL syntax:

HAVING (((tblMeet.SeasonID)=5) AND ((tblMeet.UseForPR)=1) AND ((tblMeet.IsCurrent)=0))

I remembered to change the magic number for a couple of years, but I didn’t change the seasonId at the beginning of this season (now on season 6) so erroneous results were generated (and some kids almost didn’t get a PR ribbon, which would suck).  I changed the syntax to this:

Declare @seasonId int

Set @SeasonId = (

      Select seasonId from tblSeason

      where SeasonDesc = Convert(varchar,YEAR(Getdate())))

and later on:

HAVING (((tblMeet.SeasonID)=@seasonId) AND ((tblMeet.UseForPR)=1) AND ((tblMeet.IsCurrent)=0))

And now I don’t have to remember this change next year.  Dear Jamie2011, you are welcome….

BTW: how cool is the intellisense in SQLServer 2008 Management Studio? Awesome Microsoft. Awesome.

Advertisements

Moving SQL Server between 2 Hosting Companies

I am moving a site from Host4Life to WinHost.  There were 2 high-level tasks:

Publish the website

Move the database

 

Publishing the website was easy.   All I had to do was to change the ftp address in Visual Studio 2010 Publish Website wizard

Moving the database was more complicated.  I tried 3 different ways using SQL Server Management Studio 2008 (SSMS):

1)      Backup/restore.  The backup failed on the default file location.  However, when I backed it  up not specifying the path, the backup worked.  Somewhere on Host4Life’s data servers is a file named Test.  In any event, the fact that I could not back up to my local file system in SSMS killed this idea.  I then tried to look for test but they have a new interface (tinyhost) and my SQL Server password did not work and there was no way I am interacting with those clowns at H4L’s help desk

2)      Copy Database.  I tried this next.  It failed when trying to copy from H4L to WinHost and H4L to my local file system.  I went through a couple of iterations with my local system – I made more progress once I enabled all of the SQL Server Services, but it ultimately failed and the log did not tell me why easily

3)      Scripting.  Ultimately, this is what worked for me.  I don’t know what version you can add scripting the data (shown here) but that made all the difference.  I scripted the tables, functions, views, and stored procs (fortunately, I don’t have any recursive dependencies in my objects) in order and the database came up as expected

POCOs

I started working through the POCO Template for Entity Framework found here.  I got through the exercise and have a pretty good idea of the what (if not the why) of POCOs.  I ran into a couple of gotchas:

 

If you spell

string inputFile = @"..\POCOTTemplateWalkthrough\Blogging.edmx";

wrong

You get this error:

Error             1                     Running transformation: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.IO.FileNotFoundException: Unable to locate file

   at Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolvePath(String path)

   at Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolvePath(String path)

 

Once I got to step #9, I ran it and it worked.  I then created a new Console app to be my User Tier, changed the project type of  POCOTemplateWalkthrough to a Class Library, and moved the Program.cs file to the UI.

To my surprise, it did not compile:

I then had to add a reference to Entities (I thought my UI could be Entity-unaware?) to get it compile and run.  It compiled but when I ran it, I got the following error:

{"Violation of PRIMARY KEY constraint ‘PK_People’. Cannot insert duplicate key in object ‘dbo.People’.\r\nThe statement has been terminated."}

I then deleted the 1st record in the data table table to get it to work

 

I ran it again and it worked:

Since I did not specify the ID, it is defaulting to 0 (the tutorial needs to be fixed some).  I then looked at the database table and confirmed that there was no identity key:

I changed it to identity and updated the Entity Framework and POW!

Of course, another way to do it is to add this line of code:

person.ID = 2;

 

But who wants to keep track of Primary Keys on the Data Layer? That is what identity keys are for.  I am a fan of surrogate keys, not a fan of composite keys. 

Mythical Man Month and Software Project Survuval Guide

I finished two books over the last week.  The first is the Mythical Man-Month.  An oldie-but-goodie, it was surprising to me about how many problems that were written about in the last 30 years are still with us.  Of course, they are human problems and it is much easier to change a computer than a person.  Brooks’s hypothesis of Time Versus Number of Workers in a task with complex interrelationships is still spot on – and companies are still re-learning this lesson even today.  The interesting thing is that his solutions are not really relevant – most have been tried, modified, or discarded (the specialization of labor and the parallel documentation has been largely discredited) but the problems of conceptual integrity and communicating outside of the system are still with us.  I chuckled when he wrote “this technical fad of object oriented programming…”.

The other book I read was Software Project Survival Guide by Steve McConnell.  A PMP light read, this book is showing its age.  I enjoyed his discussion on how to treat developers and how to get them to avoid context switching (something my current employer still hasn’t figured out) and the stages in planning.  If I have to lead a project in an overly bureaucratic organization that is behind the curve in terms of industry best practices, I would certainly reach for this book.  If I have the ability to lead a project in an agile and flexible organization, this book would stay on the shelf.

Mocking and .NET Framework Client Profile

I found an interesting small gotcha with Visual Studio today.

I whipped up a quick Hello World Console application and added references for Rhino Mocks and Moq.  However, neither would compile:

I then realized that the Console application, by default, uses the .NET Framework Client profile.  You can read more about it  here – basically it is a stripped down version of the .NET Framework to allow for a smaller footprint on WinForms, Services, and Console applications.  You can see in properities:

 

Apparently, both Mocking frameworks use System.Web, which is not part of the Client profile.  Simply switching the target framework to .Net Framework 4.0 solved the issue.