Coding Guidelines

Last night I presented to the Perth .Net Community on an upcoming tool called PEX. There were a couple of mentions in the talk of “allowable exceptions” backed up by mentions of the .Net Framework Guidelines.
I was asked by a few people afterward what the book was and whether I had presumably made these guidelines up ๐Ÿ˜‰
I was under the impression that this book was widely read, so it is clearly not as common knowledge as i may have thought.
Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries (2nd Edition)
is a must read for .Net devs that are writing code that is consumable by others (ie anything that uses public or protected accessors)

I would highly recommend this as it also gives a lot of background as to “why” behind the recommendations. It is also nice to read the comment from authors of certain .Net framework as they point out many things including their mistakes.

The books is made available online for free (not sure if it is in its entirety) at MSDN here

The allowable exception was in reference to section 7.3.5 (page 237) or a cut down version here

Oh, the links to the Pex stuff are here:

Thanks to everyone who came (especially those who bought beers afterwards) ๐Ÿ˜‰

Solution Genrating and creating your own VS Sln files

Recently I have started building my own solution generator to create all the basic stuff that I just want done before I even really start coding.
The generally idea is that I can get a basic working concept application or web site up and running quickly that still follows the basic architecture that I will implement.
As most of the applications I have built in the last few years have been NHibernate based and I follow a similar standard for them why not generate the majority of the code to get me started. Whether I use all the code is irrelevant, its a starting point and saves a lot of start up time.
I have basic table structures that I use such as Customer, Orders etc so I can use those and generate from there and I have a decent size core library for DAL and non functional aspect based stuff.
One little problem I have run into is that VS sln files are not the basic text files they appear to be. The leading characters as well as the first couple of lines dictate the version of VS that they will run under. Get it wrong and VS wont even open the files. For example the leading bytes for vs2005 (followed by the leading few lines) are:

[byte 239, byte 187, byte 191]
[\r\n or byte 13, byte 10]
Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005

Otherwise, mostly the project is coming on ok. I think this is going to be a very handy tool in the coming months, where is seem to have a lot of greenfield projects on the horizon.

SubSonic

Like a lot of people, writing Data Access Layers is not something I really want to spend a lot of time doing. Thankfully there are a vast number of ways that you can improve the process, ORM’s and code generators are the two basic methods of assisting in this fashion. Many will be familiar with Hibernate (and its .Net equivalent), possibly the most popular ORM on the market. I quiet like it, but itโ€™s fiddly and config heavy in terms of mapping files. You can Generate these thanks to product like MyGeneration but it often requires tweaking. once its up and running it really is a nice way to deal with persistence for very OO based applications. The other option I have used is Codesmith with it NetTiers templates. Well, up until finding Subsonic. It (so far) has been great.
What I like:

  • Can have the benefits of SPโ€™s and ORM
  • Can set it to generate the code at build time, so you know every time you run it it is synched with the data base
  • If you donโ€™t like the generate on build option you can easily generate the class files for the DAL as well as the create scripts for DB including data, all from with in VS (great for Source control!)
  • Author(s) seem to be very proactive in their support and maintenance
  • Can be used with in VS as an external tool, meaning one click generation from within VS.
  • Minimal additional config settings
  • Video showing you how to do it.
  • Its pretty fast

I last ran into Subsonic when it was ActionPack. Being happy with Codesmith I didnโ€™t really follow it up. However, as I no longer have a license for CodeSmith, SubSonic being OSS is looking a lot nicer ๐Ÿ™‚
No doubt as I continue to play with it I will find features and bugs, but so far so good.

NHibernate gotcha for Generated files

To save yourself repeated banging of you head make sure you HBM.XML file are embedded resources in the nhibernate project, otherwise how is nhibernate going to find these mappers….d’oh!
This highlights a point: Its nice to generate what you can. Hell, if you have already written the database tables, why not Gen the majority of your data access? You still however, have to know what is going on under the hood, whether its nhibernate, other ORM or straight ADO.Net. Otherwise how fix you fix lower level problems when you run in to them…