Archive for the ‘Uncategorized’ Category

Well, this month EDMUG was hard up for speakers, so out of desperation they asked if I had anything to say.  I figured I could talk for about 30 minutes on MonoRail, and then read the newspaper out loud for the next 90 minutes.  Oddly, the EDMUG executive was okay with that.  This morning they sent out their notice to the membership:

Thursday, April 24th will not just be one of the greatest EDMUG evenings of all time, but it will also be the opportunity of a lifetime.  It will be your chance to rub shoulders with a legend that walks like a man.  And the name of that legend is:

Tom Opgenorth!! 

Yes, as unbelievable as it sounds, pick your jaws up off the floor because Edmonton’s newest Microsoft MVP is coming to chat with EDMUG about Monorail!  The man with the largest collections of Hawaiian shirts, functioning rifles, and spandex LuluLemon tops in the entire province will be here next Thursday for the presentation NO ONE dares to miss!  As per usual, the presentation will be at the Edmonton Public Library; doors are open at 5:30 and the presentation will start at 6 PM.  Be there or be riddled with gunfire*!
ABSTRACT:

Viewstate, PostBack, and Page LifeCycle! Oh My!

Interested in seeing another way to ASP.NET applications without using
Web Forms?  Come to the April meeting of EDMUG and experience Monorail
from the Castle Project.  What is this Monorail we speak of?  Monorail
is a mature, stable, open source MVC (Model-View-Controller) framework
for ASP.NET from the Castle Project.  Come learn about web
applications without the Web Forms.
BIO:
Tom Opgenorth is a local .NET aficionado and consultant who has a
tendency to dabble in Linux.  He was recently received a Microsoft MVP
Award for 2008 in C#.

* For the skittish, gunfire will not actually occur if you miss the presentation, but we highly recommend you attend

Really, I think that there should be two things banned:  One is Justice and writing, the other is D’Arcy and Paint.NET.

Yesterday myself, Don, D’Arcy, and Eric were wandering around Seattle, just checking out the sights after the MVP Summit.  We took a break from the usual "run down D’Arcy and Manitoba" shtick, and actually had a serious conversation on the relevance of stored procedures for your typical database development/line of business app.  D’Arcy blogged take on it already.  I was going to reply in his comments, but as the comments grew, I figured my own post was in order.

I wouldn’t go quite so far as to say that sprocs are obsolete, but I really don’t think that they are that important anymore.  Sure, there will be certain circumstances where you need stored procs:  reporting comes to mind, or maybe you need to to some really DB specific things.

However, for a lot of your day to day CRUD, you don’t need stored procs.  Use an ORM.  I use NHibernate, and as such will be speaking from that point of view.

obsolete_2 To support your typical CRUD for a table, you usually need at least our stored procs:  Insert, Update, Delete, and Find.  Usually it’s more – a lot more because of querying concerns.  These have to be maintained, and versioned, and kept in sync with the application.  This is friction.  Using NHibernate, you have a mapping file per table that is typically embedded in your assembly as a resource.  This is frictionless (relative to stored procs).

It will be said that stored procs provide you with a single API for your database which will shield your application for DB changes.  Here, I don’t agree.  A lot of times if you’re making drastic changes like this to the database, you’re probably making changes to your application.  You’re probably going to be recompiling and sending out new binaries and new stored procs.

Now, I’m sure that everybody in the stored proc camp is loading up their blunderbusses full of "stored procs are faster" shot and getting ready to fire a volley.  And, I’m sure that they have the "dynamic SQL is insecure" ammo in easy reach for the second volley.  This is not true.  NHibernate creates parameterized queries – parameterized queries are just as fast as stored procs.  And, because these are parameterized queries, they are just as secure as stored procs (edit:  by secure, I mean resistant to injection attacks and the like).

Another point to consider is this:  most programmers are not DBA’s.  Their proficiency is in writing code (C#, VB.NET).  Keep your programmers doing what they do best: coding in the language they are most proficient in.  I don’t want to say that programmers write bad SQL, but that NHibernate will probably create the SQL statements that are just as good (or better) than what a programmer will do.  There isn’t just one database driver in NHibernate churning out generic SQL for all database platforms.  The database drivers are very specialized modules that are optimized for the database in question (i.e. MySQL, Oracle, SQL Server 2000, SQL Server 2005).

Honestly though, this debate (ORM vs stored procs) has been going on for while.  I know that there are still many places that are firmly entrenched and will only use stored procs.  So, to say that stored procs are obsolete might be a bit harsh.  I do believe that they aren’t as relevant anymore.

101EdmontonFusiliers_2 In April, 1908 the 101st Edmonton Fusiliers were established as Alberta’s first infantry regiment.  The first commanding office was Lieutenant-Colonel E.B. Edwards.  The 101st developed slowly, as most in the Edmonton area were attracted to the 19th Alberta Dragoons, a cavalry regiment which had been established in January of 1908.

When war broke out in 1914, there was some question about the legality of using the existing militia regiments to fight overseas in Europe.  As such, the newly formed Canadian Expeditionary Force was comprised entirely of new battalions and the 101st was not allowed to go overseas.  The 101st Edmonton Fusilier recruited and contributed 1,200 men to these new battalions of the 1st Canadian Division, C.E.F in 1914.

ler_revised_2 The 101st Edmonton Fusiliers are not longer around in any official capacity.  The legacy of the of the 101st Fusiliers is perpetuated today by the Loyal Edmonton Regiment.

The Loyal Edmonton Regiment was formed up in 1915 as the 49th Battalion, C.E.F. to fight overseas.  Many of the men that were originally recruited by the 101st Edmonton Fusiliers ended up serving in the 49th Battalion.  As such, it was decided at the end of the First World War that the 49th Battalion, The Edmonton Regiment would perpetuate the 101st Edmonton Fusiliers.

In 1943, His Majesty King George VI decreed by royal charter that the Edmonton Regiment would be know as The Loyal Edmonton Regiment.

If you do live in Edmonton, you might want to check out the Loyal Edmonton Regiment Museum at the Prince of Wales Armoury one day.  Lots of interesting history there, if you ask me.

Thanks to Phil Haack‘s online poll, there is a bit of controversy over where one’s unit tests should reside:  should the unit tests be housed in a separate project and assembly, or do they shack up in the same project as the code that they are testing?  Like Bil, I figured that rather than a big long blog comment, I’d be better of with my post.

Way back when I started out with TDD, I used to put the test fixtures in the same file as the class under test, sort of like:

public class ClassUnderTest
{
}

#if DEBUG
[TestFixture]
public class Tests_for_ClassUnderTest
{
}
#endif

My logic was basically this way, the test is right there – quick and easy to access.  Tests are, after all, supposed to serve as documentation, so by making the two classes share the same file, it was easy to find.  Plus, if you changed your class, you had quick access to your tests. When it came time to deploy, you could just did a RELEASE build, and the unit tests would not be included.  Or, if need be, send out the code with the unit tests included, and then your QA department could run your tests on their environment if desired.

However, I eventually moved away from this, for a couple of reasons:

I was still learning TDD, and I noticed I was doing a lot of thrashing.  This was because my test code is right next to my production code and I found myself falling into a "test last" pattern.  That is to say, I would write my code first, then my tests.  When my code didn’t work, I’d change it, and then "fix" my test so that my code would work.  I lacked the discipline and experience to write good, proper tests and I felt that part of the problem was it was to easy to fall back into the "Old Ways".

I found that by putting my test classes in a separate assembly, I would actually think about what I need, and that would result in better tests and (IMHO) better code.  After all, one of the real benefits about TDD is that it forces you to think about your design up front.  I found that by using a separate assembly to house my unit tests, I ended up changing how I approached TDD.  I spent a bit more time thinking about HOW my test would work, and HOW the code would be structured.  It sounds crazy, but it worked for me. 

In my experience, the good people in QA didn’t give two rips about our unit tests.  They added no value to them, as they were using their own tests.  So, if nobody cared to run the tests outside of the developers, there is no value in sending them out.  Think lean here:  no value, so no point to make the developer’s unit tests available.

Also, at the time, I didn’t like the idea that the code that was being deployed was the code that was being tested.  I’m not a big fan of compile time directives, and compile time directives change your code.  They always seemed to cause pain when you forgot one or misspelled it.  Of course, now that we’re older, and using Nant zealously, this isn’t really an issue.

So, for those reasons, I changed the structure of my solutions.  I briefly experimented with one test assembly per production assembly, but gave that up as to unwieldy.  If you had a project with six assemblies, then you’d double it up with six test assemblies.  Now when you wanted to run tests, you’d have 12 assemblies to compile and six to run the tests on.  Not a lot of fun.  Now imagine going onto a client site with 60 project solutions:  it gets nasty fast.

I eventually settled down to the conventional "…one test assembly to rule them all…" approach, and have been doing that for a while.  It seems to work well enough, and I never worried to much about the "urban sprawl" in my namespaces.  Resharper makes it pretty easy to find classes and such, so if the namespace of my testing assembly didn’t quite match up with that of my application, I never considered it to be a big deal.

I was never really concerned about making a class public, or a method public to aid in testability.  Most (all) applications I work on are basically in house line of business applications.  I’m not writing frameworks or API’s to be consumed to the general public.  The people that are using my classes can be trusted.  Besides, they all had access to the code anyway, so what is to stop them from just going in and change the class/method from internal to public anyway?

So, where should your tests live?  Really, I don’t think it matters.  It seems natural to me to have a separate test project, but that is only because I’ve been doing it that way for a long time.  I don’t really see anything wrong with putting your test fixtures in the same project as your production code.  I’d think it was great that you had unit tests (something one rarely sees here in Edmonton), so if housing tests and production code in the same assembly works for you, then why not?

In my last post, I described how easy jQuery makes it to call an ASP.NET Web Service via AJAX.

Currently, I’m developing on Windows XP (SP2), Resharper 4 (running inside Visual Studio 2008), and using the Visual Studio Web Server.  We deploy to Windows 2003 R2/IIS6.  When I pushed the code described in my last post to the development server to test it out there, the web service call kept failing with an HTTP Status of 500 (internal error).  This basically means something went wrong on the server.

This type of error situation drives me crazy, as in most cases it is a configuration error:  something different between the platform you are developing on and the platform you are deploying to.

After adding a bunch of logging code and some poking and prying, I determined that the web service was indeed working on the server and that authentication was NOT the problem.  The problem turns out that I had forgotten to allow HTTP POST as a protocol for invoking the Web Service.  The trick to allowing HTTP POST is to put this XML

<webServices>
  <protocols>
    <add name="HttpPost" />
  </protocols>
</webServices>
into your Web.config file in the system.web section.

Moral of the Story:  If your development platform is exactly the same as your deployment platform, you can avoid a lot of these problems

Just figured I point out ConnectionStrings.com.  I find this site rather handy because it shows you the syntax for the connection strings of pretty much every database you’d want to use in .NET.

Today I got a new hard drive for my web server, to replace the one that died back in February.  Hopefully the new 750GB drive will serve me as well as (or better) the old 200GB WD Cavair.  I must admit, I was a bit suprised at how cheap it was to get such a big HD.

The first order of business was to move my web site off it’s current home and onto it’s new, more permanent one.  I went about installing Ubunutu 7.10 64 bit, and them VMware server 1.0.4 on the new computer.  If you Google it, are a lot of articles on installing VMware on Ubunutu.  However, I did run into one stumbling block:  with 64bit Ubunutu you need one more dependency: ia32-libs

If you don’t install this, then the VMWare installer can’t create an SSL Certificate and your install of VMWare will fail.

So, to recap, on Gutsy Gibbon 64-bit you need to install:

    sudo apt-get install build-essential linux-headers-`uname -r` ia32-libs

From there, follow basically any of the HOWTO’s on installing VMWare Server, and all should work for you.

As it has been slow lately, I thought I would take a minute to say "Happy Birthday" to Don Belcham, aka the Igloo Coder.

Don, for those of you who don’t know, is potentate of the Edmonton .NET User Group:
DonBelcham_EDMUG_Prez

He’s acquired some fame, mostly for his ability to consume large amounts of single malt whisky before public speaking. Inspired by Justice Gray, here is a little story* I’d like to share about Don.

peyote In the fall of 2007, Don & I decided to attend Desert Code Camp 3 as presenters. I went down with the wholesome and altruistic intentions to speak about Mono, and Test Driven Development. Don wanted peyote. Don didn’t know that peyote doesn’t grow (naturally) in Arizona.

Needles to say, this didn’t make Don happy. It was very grumpy Don who went to the session on "Intelligent Singletons/Factories using Dependency Injection in ObjectBuilder / Composite UI Application Block". I went to watch. Don proceeded to get liqoured up and throw empty whisky bottles whenever "intelligent singleton" was mentioned. I guess the moral of the story is that Don is a grumpy drunk when he doesn’t get his peyote.

Anyway, happy birthday Don. You don’t look a day over 48.

Last night I spent some quality time looking at vincent-vega, aka Continuous Database Integration for SQL Server. This is a pretty simple and handy task for Nant that helps bring continous integration to your databases. I use updateSqlDatabase like as part of my integration tests like so:

  1. Drop the database
  2. Create a new database
  3. Run some SQL scripts to bring my database up to date
  4. Run various database integration tests, repeating steps 1 -3 as necessary.

What this project will add a new task you: updateSqlDatabase. With this task, you can do one of three things: Add a database, Create a Database, and Update a database. updateSqlDatabase will look for SQL script files , and will execute those SQL scripts for you in the order they are found. Information about what scripts have been run are kept in a table that updateSqlDatabase creates for you when you create your database.  This will allow you to see the history of when your database was updated, and with scripts.  This
How to get started and use it? First, get the code from Google Code. Compile the code. Next copy the output of the build to your Nant bin directory.  See the image below.  You probably won’t need log4net.dll or Nant.Core.*.

CDBI_BuildFiles 

Next use the updateSqlDatabase task to perform the drop/create/update as desired. And how do you do that? Well allow me to use athe build from Code Camp Server as an example. Here are three targets from the nant script:

<target name="create-database" depends="version, init">
    <echo message="Creating database ${database.name}..."/>
    <updateSqlDatabase scriptDirectory="${database.dir}" action="Create" server="${database.server}" database="${database.name}" />
    <echo message="Current Database Version: ${usdDatabaseVersion}" />
</target>

<target name="drop-database">
    <echo message="Dropping database ${database.name}..."/>
    <updateSqlDatabase action="Drop" database="${database.name}" server="${database.server}" failonerror="false" />
</target>

<target name="update-database">
    <echo message="Updating database..."/>
    <updateSqlDatabase scriptDirectory="${database.dir}" action="Update" server="${database.server}" database="${database.name}" />
    <echo message="Current Database Version: ${usdDatabaseVersion}" />
</target>

In these samples, we’re assuming integrated security. If you were using SQL authentication, you can, but I’m not going to cover that. Look at the code for how to do that.  It’s good for you to read other peoples code.  Anyway, here is an explaination of the parameters for the task:

  • action: one of Add/Update/Drop. Note that this action is also used by updateSqlDatabase to find the script files to run.
  • scriptDirectory: This will hold our database scripts. Note that they must have the .SQL extentsion.  Inside this directory we create one or two folders which will hold our scripts.  So, what are the rules surrounding the locating of SQL script files?  Basically, it goes something like this:

    For a Create action:  get the scripts from first the Create folder (if there is one), and then the scripts from the Update folder.  The scripts in the Create folder get run first, then the scripts from the Update folder.

    For an Update action:  get the scripts from the Update folder.  Run them.

    For the Drop action:  Ignore all the folders.  Don’t execute any scripts.

CDBI_ScriptDirectory

  • server: The database server that the scripts will be run against.
  • database: The database the scripts will be run against.

My only complaint about it is that it’s SQL Server only (for now). But, I won’t complain to loud, as I suppose on the eighth day I could probably add support for MySQL to the project.

Now, why would I go through all this?  Here is a quick summary of why I think it’s worth effort (in case the ‘continous integration’ didn’t give it away):

  1. It helps me ensure that the database I think I’m testing against is the database that I should be testing against.  It minimizes the friction of integration testing when you don’t have to chase down and resolve schema or data issues. 
  2. Exercise and test any database scripts that might have to be applied in production. 
  3. I can keep my schema in my source code management tool and manage changes to my database.

Once again, appealling to the niche (non-existant?) market, I give you a quick run down on using db4o, and the Castle Windsor db4o facility.  Yes, I know the link for the db4o facility link doesn’t work.  That is what prompted me to do this blog post. This is because, a while ago, the db4o facility was pulled from the Castle Project.  Apparently there was a licencing concern.  It’s my understanding that this has since been resolved, and I seen now that there is again the db4o facility.

First off, a safety tip:  the lastest Castle binaries are compiled against db4o 6.1.  The current version of db4o is 6.4.  The two assemblies are not compatible.  If you need to use db4o v6.4, I suspect that you’ll have to compile the Castle code on your own.  Me, I just rolled back to the older version of db4o.

With that out of the way, let me make the following assumption:

  1. You are already using Castle Windsor and have that already set up in your application.
  2. You are using XML files to configure Windsor (i.e. not Binsor).  It probably wouldn’t be hard to adapt this to Binsor, but for now, please humour me.

You’ll need to copy two files to your bin directory:

  • Castle.Facilities.Db4oIntegration.dll
  • Castle.Services.Transaction.dll

Note that you don’t have to reference these files in your project, but they do have to be somewhere that .NET (and Windsor) can find them and load them.

You probably have a config file where you register your components/facilities.  I’ll show you the contents of my facilities config file.  It’s actually pretty simple:

<facilities>
  <facility id="chronydownload"
            type="Castle.Facilities.Db4oIntegration.Db4oFacility, Castle.Facilities.Db4oIntegration">
    <container id="db4o.container" databaseFile="data/reloading.db4o" />
  </facility>
</facilities>

With this little snippet, Windsor will now resolve provide you an IObjectContainer using the database reloading.db4o.

There are some other values you can pass to the container.  The best source of documentation in this matter would be the source code for the facility.