Universal Comparer for .NET

Back in November 2004, I had the idea of making a “Generic Sorter for Strongly-Typed Collections”, by harnessing the mighty power of reflection. Well, earlier this month somebody anonymously emailed me some code that takes this to a whole different level, enabling sorting on multiple properties and to arbitrary depths within the domain model hierarchy, all by taking a single SQL-like parameter. It’s really very clever indeed, and I’m going to use this method in the future – thanks, anonymous code fairy! ...

27 February 2006

String.IsNullOrEmpty

The single most popular entry on this blog is, surprisingly enough, this quick post from Summer 2004 where I did a quick experiment to “prove” that in .NET the quickest way of proving that a string is empty is to compare its length to zero. Well, as of .NET 2.0, we have a new static method on the System.String class – IsNullOrEmpty – that allows you to easily test whether a string is, well, null or empty! ...

25 February 2006

ASP.NET 2.0 – one month on

It’s over a month now since Visual Studio 2005 officially RTM’d, and during that time I’ve been fortunate enough to have been using it for the majority of my time on most working days. One of the things we’re trying to achieve is the integration of several distinct internal corporate web applications into a bespoke portal system, with single sign-on, common authorization model, common look and feel, etc. There’s been quite a heavy R&D / proof-of-concept aspect to the workload so far, as we try out different ways of achieving this, and grow familiar with the new technologies available to us in ASP.NET 2.0. ...

10 December 2005

Stopwatch Class for .NET 1.1

Paul Welter has backported the System.Diagnostics.Stopwatch class from .NET 2.0 to 1.1. Handy. http://weblogs.asp.net/pwelter34/archive/2005/07/01/416999.aspx

5 July 2005

New Recommendations for String Comparisons in .NET 2.0

I know it’s a rather geeky and dry subject, but I found this MSDN article on new recommendations for using strings in .NET 2.0 quite interesting. It’s going to be difficult to stop using CultureInfo.InvariantCulture habitually!

4 July 2005

eXtreme .NET

Up at 0500 yesterday and onto the early GNER Mallard down to that London town for the MSDN briefing on eXtreme programming with .NET. It was pleasant not to be driving for a change, and speeding through the Yorkshire countryside early on a midsummer morning was very relaxing – what a stark contrast with the hot crowded tube that I had to catch on arrival to the big smoke. Three stops down to Oxford Circus seemed to last as long as the entire preceeding train journey. How people do this every single day I do not know.. ...

18 June 2005

Tech Books

I succumbed to temptation and bought another book at the MSDN briefing yesterday, pausing only out of concern that the guy who mans the Computer Manuals stand at these events might think that I’m stalking him (or fancy him!). Having determined that I could hand over my Barclaycard whilst flashing my wedding ring, I brought my current reading list up to three: Developer to Designer: GUI Design for the Busy Developer – this was a wedding present from my mother-in-law and is proving to be an interesting, easy-going read so far. My competencies lie in the middle tier and database, and although I’m technically capable of making GUIs, I prefer to leave the design up to others. It’s inevitable though that most coders end up doing some design, and this book is, er, designed (sorry) to help with that process. The early section of the book concentrates on each UI element in turn, whilst the later part of the book looks at putting it all together, and has particular focus on the web – layout design patterns and interaction patterns. It’s a good read, making a nice contrast from the heavier books, and I’m pleased to learn that I’m not alone in hating Outlook’s labyrinthine Tools->Options dialog! ...

18 June 2005

Framework for Persisting .NET Classes Into SQL Server Image Fields, With Locking Ability

There seems to have been a lack of .NET-related stuff on this blog recently, so let me show you something I cobbled together for work purposes last Wednesday: A common requirement we have in our ASP.NET applications is how to reliably persist object instances for a lengthy period of time. End users shouldn’t have to concern themselves with the concept of Session expiry, and it’s unacceptable for them to return to their PC after nipping off for a coffee to be confronted with the dreaded “Object reference not set to an instance of an object” when they try to continue editing their basket / sales order / timesheet / whatever. ...

18 January 2005

A Generic Sorter For Strongly-Typed Collections

Late last night I was doing some development work on a website for our investment club. I had a strongly-typed collection of “Stock” objects, and wanted to be able to sort them on any parameter (StockName, SharePrice, Volume, MarketCap, etc..) in either descending or ascending order. I didn’t want to have to write a separate comparer for each property. Reflection to the rescue! Below is a GenericSorter class, the use of which allows for the development of a Sort() method on the collection taking as a parameter the name of the property on which to sort. Smart. ...

7 November 2004

NHibernate

There’s so much good open-source .Net development going on these days – .Text, nGallery, nUnit, etc – it’s a really interesting time to be a developer. My colleague Colin just drew my attention to nHibernate, an object persistence library for relational databases. TheServerSide.net has an excellent article summarising its use. Having just finished a long project where a large amount of time was spent writing “plumbing” code to persist objects to and from a SQL Server database, I’m finding the idea of using an O/R mapper such as this very appealing the next time round. Just as incorporating the MS DAAB block into solutions has reduced the amount of data access code required, I’m hopeful that embracing frameworks such as nHibernate will leave us with more time to spend focusing on addressing the business needs and less on re-solving technical issues. ...

3 September 2004

Is My String Empty? Some C# Performance Metrics

Update (25/02/2006) – Hey, we now have String.IsNullOrEmpty, you know! What’s the quickest way, in C#, to test whether a string is empty? This is a question which came to my mind yesterday. I’ve always been fond of: if (myString==String.Empty) but it occured to me that this necessitates the creation and destruction of a second object (the empty string with which we’re comparing), so maybe it would be quicker to write: ...

30 July 2004

Inheriting From BaseValidator to Make Custom Validation Controls

For months now, I’ve been desperate for a decent excuse to make a custom ASP.NET server control, so I was secretly pleased today when a CustomValidator originally coded for use on a single page was required elsewhere. It’s surprisingly easy to do, although it took me a while to find a decent example online, which eventually came courtesy of the 4GuysFromRolla. In my particular scenario, I needed to check that the text entered into a TextBox did not equal any of a selection of “banned” strings (which was available via a static property on another class). ...

30 June 2004