Save time with AutoMapper

Recently did a load of view model classes which were basically flattened or otherwise simplified versions of an underlying DAL project which served up EntitySet objects – with all the heavy change tracking logic and navigation properties that involves.

Half ways through this, RSI was setting in, and I was getting really (really) bored of writing code like this.CustomerId = customer.CustomerId; – and when you get bored, you start making mistakes – and… well it was starting to look like a very long afternoon, let’s leave it at that.

So – I remembered someone once talking about AutoMapper being an excellent free tool for managing this kind of logic. So headed over to and downloaded the project – and after some very simple configuration steps I was able to map entire objects in one line of code.
AutoMapper at the most basic “out of the box” level will simply map properties on their name.
For me it worked really well to AutoMap most of the properties -and for anything more advanced, I would simply name the target property differently and do whatever custom mapping logic was required manually.
If you don’t have this level of flexibility – or if you just want to get more involved there is a vast amount of configuration options for you to look into.

Quick overview of steps to setup in an ASP.NET (MVC) application:

Add a static method to configure AutoMapper:

	public class InitAutoMapper
		public static void Configure()
			Mapper.CreateMap<Category, CategoryModel>();
			Mapper.CreateMap<CategoryModel, Category>(); //Two way binding

Configure AutoMapper on application start (Global.asax):

	protected void Application_Start(object sender, EventArgs e)

Start using – in this example an overloaded constructor is doing the mapping:

	public class CategoryModel
		public CategoryModel(Category category)
			AutoMapper.Mapper.Map(category, this); //(source, destination) - will map all properties where names match

Improving ASP.NET Compile Time

Very useful article here:

The biggest and easiest win is tweaking the System.Web.Compilation section of your web.config:

  • Setting the batch attribute to false (mean each page class is compiled when a request hits it, if batch is on, this compilation is … batched). In effect you choose between many small performance hits (unbatched) or one big hit (batched) – and for the usual “change – build – test” cycle of web dev – you want the first.
  • Setting optimizeCompilations to true will allow you to deploy an updated binary to the websites bin folder (for example if you have built your BLL or DLL project) without triggering a full recompile – this is very useful if you’re working with e.g. CMS platforms where you might copy your bespoke code into the main platform website.
    For Web Projects this also applies to changes to the app_code (but really, stay away from web projects).
  • Setting up a RAM Disk and pointing the tempdirectory attribute to this can also improve compile times.

Interesting feature (for very large projects) is parallel builds:

Finally – another main factor is apparently buying a better PC!

jQuery Fundamentals

Like most web devs I’ve been using jQuery for some time now – and managed to do some fairly advanced things, without ever really reading up on the framework. If anything this probably speaks for the intuitive syntax and the excellent range of examples available at the main jQuery and jQuery UI sites.
However, this kind of learning does mean that you end up with some potentially embarrassing holes in your basic knowledge and understanding. Like for example a while back when, in a less successful job interview, I was asked what this jQuery code does:

$(function() {});

My immediate answer was “nothing”, which is true enough – but the follow up question from the examiner was then “well, when does this code run?”
Turns out it’s a shorthand for the usual $(document).ready(/*...*/) loader – so the two code examples below are functionally identical:

	$(document).ready(function() {
		alert('Document is ready for ya!');

	$(function() {
		alert('Document is ready for ya!');

So – there’s something you can use to confuse, befuddle and confound your co-workers…
For me, it was a reminder to get back to reading the excellent free jQuery Fundamentals.
Also Using Objects to Organize Your Code has been on my jQuery/javascript reading list for a long time now.

Formatting Code Examples in WordPress

Had a mare getting WordPress to display code samples in a consistent way – the handling seems to vary between different themes as well.
Paying $15 for the custom CSS seems a bit much for something as basic as this
Found the below after some experimentation, seems to work in all themes so far:

            //code here (pasted in tabs are preserved)
            //And the text does not wrap - see:....................................................................................................................................................

“Cufon, meet Raphael”

Recently worked a very odd job where the client – for good reasons, believe it or not – needed not only a bespoke font embedded in a HTML page, but also wanted this rotated 90 degrees.
Flash (ie Sifr) was not an option and the solution had to be IE6 compatible….right.

Amazingly this actually did not prove a huge problem – using the Cufon javascript library to generate and embed the font ( and then the excellent Raphaël javascript library ( to rotate this – the solution was as simple as the below (abbreviated, but you’ll get the gist).
Note one detail – on the Cufon generator page – you have to select the option to use Raphael as the target – in the final 3rd party customisation section.
For more Raphael options see this page:

	//User Raphaël js library to rotate the Cufón replaced text ( +
	function setRaphaelCufont(holder, text, fontSize, holderWidth, holderHeight, fontColor, leftAlign) {
		//left: 1720px; top: 45px; height: 1000px; width:170px;
		var r = Raphael(holder, holderWidth, holderHeight); //args: containerdiv, width, height
		var myFont = r.getFont("[first word of the font name defined using cufont]");
		var txt = r.print(0, 0, text, myFont, fontSize); //args: startX, startY, text, font, fontsize
		txt.attr({ fill: fontColor });

		//Note: default rotate behaviour will rotate each individual letter, so below says:
		// ... "rotate each letter 270 degrees *around the entire textareas top left corner*, and then afterwwards move down the page (translate) the pixel width of the textarea"
		txt.rotate(270, txt.getBBox().x, txt.getBBox().y);
		if (leftAlign) { //Move the printed text to start of containing box:
			txt.translate(0, holderHeight);

Another interesting library for font replacement is Google’s new WebFont Loader.