Monday 17 November 2008

The Pixel Chicken

I just found out that my mate Liz has buggered off back to Australia without somuch as a good bye! The cow!

So anyway, I thought I'd link her up... If you're reading this, you're based in Sydney and you're looking for a top class interactive designer / developer then give Liz a shout and check out her blog over at www.pixelchicken.com.

Liz, next time try saying good bye ;)

Wednesday 12 November 2008

Book Review : ASP.NET 2.0 Step by Step

Just over a month ago I moved in with Smithy, during the expected packing and unpacking I came across a book that I bought close to 3 years ago when I started working at twentysix Leeds and was forced to learn .NET.

The book is "ASP.NET 2.0 Step by Step" from the Microsoft Press which and I've got to say, my initial opinion was not a good one, after getting just over half way through, I became disenchanted with and put to one side. I have since spent the last 8 months looking high and low for it, and even accusing my co-workers of losing it for me :S (sorry guys) as I found a desire to finish it.

My first attempt at reading the book was not a good one. I was coming from a solid 5 years of scripting things like Lingo, ActionScript, JavaScript and most importantly PHP; I was struggling with the structured and strict way that ASP.NET works when compared to (loose and, dare I say, sloppy) PHP pages. It also seemed a bit wordy for my liking. Some of my favourite programming books are the O'Reilly cookbook series which give you examples based around short, concise scenarios.

So, armed with my now solid 2 years of experience with C# and the .NET Framework I picked up the book again, found my old bookmark still stuck in at the end of chapter 13 and decided to have a little flick.

It just so happened that the next couple of chapters concerned subjects that have been on the agenda quite a bit recently, namely the caching of data and output within a web application.

These two chapters give a good introduction and now, with just over two years experience with C# and the .NET framework I find these chapters to be just enough to get me going on a subject. I now know how and where to look for further information on a subject from my time searching the usual places.

Having re-read the first 13 chapters I take back most of what I thought about it in the first place and can see that my original lack of enthusiasm for the book was not down to the book, but due to my inexperience with ASP.NET and all its in's and outs. I actually love this book now as it gives you insights into many of the key tools that you might use day to day in a ASP.NET web app.

Good book, get it read!

Tuesday 11 November 2008

The ASP.NET Singleton, Singleton classes for ASP.NET

This is a little trick I learned recently. I had a situation where I needed to use a static class in one of my ASP.NET web apps. I wanted it to provide functionality on a per-user basis, but when I put this in to practice I got some funny results and then I realised the obvious. Static classes are shared throughout any single instance of a .NET application, and each website runs within it's own w3wp.exe process, as a self contained application. This explained the funny behaviour.


So I did a little digging around and found the following code, which adapts the singleton pattern to store the instance in a per-user manner.

Each user is assigned an HttpContext for thier time visiting an ASP.NET web application. This Context object allows you to store objects.

So a classic Singleton might look something like this




class Singleton {

// private instance field
private static Singleton _instance;

// public read-only instance property
public static Singleton Instance {
get{
if(_instance == null){
_instance = new Singleton();
}

return _instance;
}
}

// private constructor
private Singleton() {
// constructor logic here
}
}


All we have to do is to replace the private field for an Item in the HttpContect object, like so...





class Singleton {

// public read-only instance property
public static Singleton Instance {
get{
if(HttpContext.Current.Items["SingltonInstance"] == null){
HttpContext.Current.Items.Add("SingltonInstance", new

Singleton());
}

// cast the item to a singleton because it is stored as a generic object
return (Singleton)HttpContext.Current.Items["SingltonInstance"];
}
}

// private constructor
private Singleton() {
// constructor logic here
}
}