Garry Pilkington


Application Developer
Liverpool, UK

October 2008 - Posts

Being saved by...unit tests

The time comes in every developers life when a higher up requests what they think is a minor change. You think about it and agree also thinking that it would not take too much time. It is only when you get back to your desk, check out the code and look to where the change is going to happen when you realise the worst. If you make a change here what repercussions will you introduce, is it possible you could introduce a defect in the code which could then go on to really crap up the application. This happened to me last week, luckily I already had some unit tests in place covering the code which was in need of changing. I knew what the method in question was going to return, and so did the unit tests. I made the necessary changes and re-run the tests. Fantastic they all pass. The code checked in, the tests passed again and the nightly build was successful. The moral to this little tale? Introduce unit tests as soon as you can to test your code. As Ian Cooper points out:-

"The problem is that the most important step is not doing it right, but doing it at all."

So if you are writing some code, stop have a think and throw some unit tests in first. Even if you think they are not of a good standard, it doesn't matter. If your tests are simple and easy to understand and they pass the method they are testing, excellent work. Give yourself a pat on the back because 6 months down the line when a higher up asks you to change that method, you can go home in the knowledge that the unit test is working for you.

Singleton in action

I had one of those 'wow that's cool' moments last weekend. This sounds really sad and you may think that I need a life, but I was playing with the code for a basic singleton pattern found at:-

http://www.dofactory.com/Patterns/PatternSingleton.aspx

I ran it with VS 2005 and stepped in to the code. I have used CamStudio to catch the goodness of this moment so you can all see. Repeat after me...wow.

Watch the screencast here. (I did attempt to have the video embedded on the page, but it took up too much room with this layout)

This is my first screencast, there is no sound so do not adjust your pc. It is hosted by screencast.com which looks to be pretty good video hosting provider.

This is a very simple implementation of the singleton pattern and is not thread safe so should not be used on production code. I will do a video of thread safe singleton using double-check locking after I have calmed down from this experience.

Posted: Oct 28 2008, 02:11 PM by capgpilk
Filed under: , , ,
Protect your job...perhaps by learning new tech goodies

It is in the news so much recently that there is no avoiding the fact we are either in or about to go in to a recession (here in the UK that is). There is already job losses in the tech industry, wether these losses were going to happen anyway, and now is a good time to blame the economic outlook is not a topic I am trying to cover; this has been covered here. What I am trying to address really is how do you protect your job? And if you do loose your job, how do you improve your chances of getting straight back in to employment?

As you are most likely aware, ASP.Net MVC has gone beta and Silverlight 2 is out. Are you going to learn about them. What about all the other new and not so new goodies that are coming out of Microsoft?

new_ms_technologies

This is my simple attempt at placing where I see these new technologies laying. Personally I am mostly working in the web world, although up until quite recently I have been moving more in to the windows development world. As it seems that Microsoft is concentrating more on web stuff than Windows stuff recently, it would be in my best interests to stay up to date with these new web related technologies and instead of becoming a jack of all and a master of none, not devote too much time using the new Windows stuff.

Scott Hanselman has now published the results of his survey looking into what .Net technologies were being used out there.

[http://www.hanselman.com/blog/SurveyRESULTSWhatNETFrameworkFeaturesDoYouUse.aspx]

Taking out WebForms and WindowForms, it seems that the respondents are mainly using AJAX, ASMX, WCF, ADO DataSets and then Linq to SQL. Interestingly MVC is ahead of WPF even though it is still in beta, could we expect this number to shoot up when it gets released in its final form?

As the tech industry squeezes itself, opportunities may arise for the developer wanting to learn this new stuff. Projects can be put on the back burner leaving what would be development time passed over to maintenance and leveraging the technology already in use.  Part of this time could be spent learning or researching for possible future projects, perhaps creating dummy test projects which could then be demoed to the higher ups. This may be the time to go back to our primeval instincts and get the upper hand to survive what the scaremongers say could be a very difficult time ahead (it could also turn out to be not as bad as they fear, but we don't have the power of hindsight).

It is of course down to a number of things as to why a person may become unemployed, it's just that it may be in your interest to better position yourself to try and fend that situation off.

Configure SubSonic for a Windows application and sort out the table names

Web or Windows applications?

SubSonic was originally developed to aid in the creation of web applications, but with a bit of tweaking it is also possible to use it for Windows or console applications. When you generate your DAL using the Sonic.exe tool, it is looking for a web.config file in your project root. Add this config file with your usual settings like in my previous post and generation will work fine. However when running the application you need the same config file, but renamed to match your application.exe name such as application.exe.config. I don't quite know why this has to be done, but it works for me and I have never looked in to a better way of doing it. Also if like me you use a NAnt script, then you can copy and rename your original web.config to this application.exe.config everytime you perform a build.

Those darn table names...

Ok you have SubSonic set up and you have generated a DAL for your database, but wait on you don't want all the tables in your database. You can filter your choice of tables by using the includeTableList attribute in your web.config. Also if you find your tables have crappy names which don't mean anything when you come to use your DAL in your application, you can use the regexReplaceExpression to swap these with more meaningful names.

   1: <SubSonicService defaultProvider="providername">
   2:     <providers>
   3:       <clear/>
   4:       <add name="providername"
   5:                  type="SubSonic.SqlDataProvider, SubSonic"
   6:                  connectionStringName="connectionname"
   7:                  generatedNamespace="yournamespace"
   8:                  includeTableList="table1, table2, table3"
   9:                  regexDictionaryReplace="table1,Accounts;table2,Customers;table3,Products"/>
  10:     </providers>
  11:   </SubSonicService>
Posted: Oct 23 2008, 12:04 PM by capgpilk
Filed under: ,
A quick getting started guide to SubSonic

This is a post originally published on my previous blog over on Blogger. I have copied it here as I very rarely go to my old blog anymore and this info may be of some use to someone out there.

Subsonic; an ORM which just works. I cannot believe how simple this was to set up.

1. Download it from CodePlex and run the executable.

2. Once it has installed create a link to the command in Visual Studio by going to Tools>>External Tools>>Add

subsonic_tools_pic

This will generate the code for the DAL based on your settings in the web.config file.

3. Speaking of which here is a sample of the important parts of the web.config

   1: <configSections>
   2: <section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic" requirePermission="false" />
   3:  
   4: <connectionStrings>
   5: <add name="connectionname" connectionString="Data Source=servername; Database=databasename; Integrated Security=true;" />
   6: </connectionStrings>
   7:  
   8: <SubSonicService defaultProvider="providername">
   9: <providers>
  10: <clear/>
  11: <add name="providername" type="SubSonic.SqlDataProvider, SubSonic" connectionStringName="connectionname" generatedNamespace="yournamespace"/>
  12: </providers>
  13: </SubSonicService>
  14: </configuration>

Just swap out connectionname, databasename, providername and yournamespace with your values.

4. Now the fun bit. Run Tools>>SubSonic. This will automatically generate the code for your ORM classes in a directory called Generated within your VS project. That is all you need to get up and running.

References:-

Getting started with SubSonic
For Web Sites - Using the BuildProvider
Video on getting started with SubSonic
Setting up SubSonic

Posted: Oct 22 2008, 12:52 PM by capgpilk | with 4 comment(s)
Filed under: ,
Autumn of Agile

Following on from the Summer of NHibernate, Stephen Bohlen is now treating us to an Autumn of Agile. The initial screen cast has been published, go check it out here :-

Autumn of Agile

Posted: Oct 21 2008, 04:36 PM by capgpilk
Filed under:
No pressure then...

Now some of you out there know pressure when they experience it, but shed a thought for the NASA engineers fixing the Hubble Space Telescope. There has been a malfunction in the Science Data Formatter which is responsible for downloading data to Earth based stations, NASA want to switch over to the redundant unit which hasn't been used since pre-launch. Now if this doesn't go according to plan, then potentially the HST is useless. Add to that pressure because Atlantis is posed for launching to service the HST, albeit not this component therefore if it doesn't work that mission could be scrubbed. Then add to that more pressure as Atlantis is not able to get to the ISS in case of emergency, then Endeavour is also ready to launch as a rescue vehicle.

I can just imagine some poor engineers sweating profusely totally engulfed in computer printouts trying to fix the problem.

overworked

Well that puts my day job into perspective. I don't think I will complain about being under pressure again.

Posted: Oct 09 2008, 02:27 PM by capgpilk | with 1 comment(s)
Filed under:
Auto-generating help with NAnt and CC.net

We are about to be audited here where I work. That is the IT department, not the company as a whole. As part of this we have to get all our IT documentation up to scratch. One problem being we don't know when they are coming and what exactly they are wanting to look at. So as far as we can see we have three areas of documentation:-

1. Procedures - Basic how to's for everyday operations of our IT department

2. User help - This documentation covers basic help files for our end users (our end users are actually fellow employees as all our applications are for in house use only)

3. Code documentation - If we were developing components for use outside our company we would be creating API documentation, as we are not we use XML comments within our applications to explain what classes and methods do.

In getting ready for this audit, one of my tasks has been to get number 3 ticked off. As we have implemented a CI environment, I wanted to get an automated system in place that would generate html help style documentation. I decided to look in to the Sandcastle project and specifically the Sancastle Help File Builder tool.

The Sancastle Help File Builder tool is a fantastic GUI interface to configure and create both HTML Help and web page documentation (I wont' discuss this in depth here). Basically just point it at your .exe or .dll set a few parameters and you are off. Great when you are manually doing it, but for automation; no. Well inside the deployment directory is a command line tool; SandcastleBuilderConsole.exe. Use the GUI tool to create your project and set any parameters and then save it as an .shfb file. Then pass your .shfb file generated to the SandcastleBuilderConsole application and your help file will be created.

Now both the Sandcastle Help File Builder tool and its command line version need the XML comments from your application. Visual Studio will generate this automatically by going to the project properties build tab and selecting the XML documentation file tick box. If however you are wanting NAnt to do it automatically each time you perform a build, add the doc attribute to your csc element like this:-

   1: <csc target="library" output="nant_build\ReportsPdf.dll" 
   2:          doc="nant_build\ReportsPdf.xml">

Then somewhere within your target element, add the command to run the exe which points to your SandCastleBuilderConsole.exe, like this:-

   1: <exec 
   2:       program="C:\Program Files\EWSoftware\Sandcastle Help File Builder\SandcastleBuilderConsole.exe"
   3:       commandline="&quot;${SandCastleDocPath}\${SandCastleHFBProject}&quot;"
   4:       failonerror="true"/>

The two properties for the commandline attribute are set earlier on in the NAnt file like so:-

   1: <property name="SandCastleHFBProject" value="sandcastle.shfb"></property>
   2: <property name="SandCastleDocPath" value=".\Sandcastle\"></property>

For some of our projects it can take a while to generate this help file, so you may want to add an extra project to your ccnet.config file which will run at a specified time instead of watching for changes in source control. I have covered these options in this post.

When the help file is produced, at the bottom of each page is the assembly build number. Now this is fine, but if you want to configure the build number to be the same as that which cc.net assigns you need to do a bit more work.

Firstly you need to call the asminfo element in your NAnt script like this before you call csc:-

   1: <asminfo output="Properties\AssemblyInfo.cs" language="CSharp">
   2:     <imports>
   3:       <import namespace="System" />
   4:       <import namespace="System.Reflection" />
   5:     </imports>
   6:     <attributes>
   7:       <attribute type="AssemblyTitleAttribute" value="ReportsPdf" />
   8:       <attribute type="AssemblyCopyrightAttribute" value="Copyright (c) 2008" />
   9:       <attribute type="AssemblyVersionAttribute" value="${CCNetLabel}" />
  10:     </attributes>
  11:   </asminfo>

It is the CCNetLabel which does the magic work. Now in your ccnet.config file you need to add the labeller element inside your project element like this:-

   1: <labeller type="defaultlabeller">
   2:   <prefix>1.0.0.</prefix>
   3:   <incrementOnFailure>False</incrementOnFailure>
   4: </labeller>

Now after performing a build, you will get documentation labelled the same as cc.net and is generated as and when you add your XML comments and check them in to source control.

Now I have the less interesting task of making sure we have all the methods etc documented. Could take some time.

More Posts