mostlylucid

Code Snippets

Premature optimization and user perception...my pet project and CSS Sprites

One of the themes I'm looking at for vNext of ASP.NET is the optimization of sites by reducing the number of server requests required for a single page. A new classic in the area of improving the perceived performance of your sites by reducing the number of server roundtrips is Steven Sounders book 'High Performance Websites'. Steven's book is very small, very readable and probably the best book on anything to do with the web that I've read in years. This book was a huge revelation for me...I spent most of my coding career writing the slickest, highest performing code I could...and frankly...

Dumb little recursive, generic FindControl method...

Doing some app building and I needed to use FindControl to manipulate a control in the OnItemCreated event in a Repeater...well, to save a bit of typing I came up with this extension method:   namespace Presentation {     public static class ControlHelper     {         public static T FindControl<T>(this System.Web.UI.Control root, string controlId, bool recursive) where T: System.Web.UI.Control         {             if(root.Controls!=null && root.Controls.Count>0 && root.FindControl(controlId) != null)             { ...

Grrr...poor use of singletons and a very cool Generic Singleton pattern!

I posted earlier that I'm switching to blogengine.net, as part of this I've been fiddling around with the code (as is my way..I'll contribute back to the source when I've finished). One of my major pet hates is poor use of the singleton pattern, especially as there's a definitive article on the pattern in .NET and how to do it well. It's actually likely that this pattern is overkill in this case and a ReaderWriterLockSlim could be better (though it has it's own problems) . Anyway, on the assumption that a Singleton is the best choice here, let's look at...

Work in progress, Response.RelativeRedirect

Inspired by this post, only covers simple cases but it's a start. Essentially this is an extended version of Response which only allows redirection to pages within the same site...so allows /default.aspx, does not allow http://www.evildomain.com/default.aspx. I've also ripped off a member of my team's excellent work on Response.Redirecting to a new window. This method uses extension methods, to use it just drop the file in App_Code and Response gets two new members. Oh and it's incomplete because I didn't account for encoded / obfuscated URLs...I'll update when I do...   using System; ...

Merging Directories...dumb little code sample

As I've posted about a few times recently I've moved back to a far more technical role and as a result I've been goofing around with some code...The first thing (which I can talk about ;-)) is a little directory merging utility I've been working on...pretty simple but it was fun to play with. You can find it here if you're interested...along with the main bit of source below...more interesting stuff to come (a lot of the stuff I've been writing is using pre-release bits so I will put it here when you can actually use it!)

Why do so many people mess up Singletons?

I've written about this a few times now (use the search thingy to find where) but I'm still surprised how many people mess up the Singleton pattern.   For instance take: if (blogSettingsSingleton == null)                 {                     blogSettingsSingleton = new BlogSettings();                 }                 return blogSettingsSingleton;   Looks ok, right? But this is a classic poor pattern when dealing with multi-threaded apps. Why? Look at the initial 'if' statement, and think what happens if multiple treads hit this at the same time...one thread...

C# null Coalescing operator

Just because I always forget this...the C# null Coalescing operator: Sample below shamelessly stolen from here... MyClass anObject; ... // Variant 1: Using a full if/else clause MyClass anotherObject; if (anObject != null)   anotherObject = anObject; else   anotherObject = new MyClass();   // Variant 2: Using the ?/: conditional operator MyClass anotherObject = anObject != null ? anObject : new MyClass();   // Variant 3: Using the ?? operator MyClass anotherObject = anObject ?? new MyClass();

Update to my favorite Live.com service

I have to say, Skydrive is the best thing Live has done so far in my opinion...very easy way to share files. I use it a lot to share stuff with my family back in the UK, my parents aren't very computer savvy but the Skydrive interface is really simple to use (they're used to folders and such...).I'm going to be very cheeky and just paste a bit of the mail which one of the PMs (Program Managers) from that team sent round today: "We’re excited to announce that at 3pm, today, we’re updating Windows Live SkyDrive (http://skydrive.live.com) with some new...

Fibonacci...coding efficiently (when doing nothing is the fastest route)

Just a little diversion, I posted previously about some code which I didn't think of until I was in the elevator following  a pretty important meeting (details may follow ). I asked how you'd make this code more efficient for multi-core CPUs...well I'll get to that but one thing I didn't point out was that with this as with any other computationally intensive code, the most efficient approach is not to do the operation in the first place. Caching...not on the web but probably the simplest form of caching you could perform, store a previous result for future use. For the code I...

Cool little thing I keep losing...ObjectRepeater

This is a brilliant little server control from Rob van der Veer (found here) – which I’ve found lots of uses for but keep having to spend time tracking it down…so here it is, mainly for me but partly for you too   ///     /// A templated databound repeater that dynamically determines which     /// template to use based on the class of the object in the collection.     ///     [ParseChildren(false)]     [ControlBuilder(typeof(ObjectRepeaterControlBuilder))]     public class ObjectRepeater : System.Web.UI.Control, System.Web.UI.INamingContainer, IParserAccessor     {         private Hashtable _templates = new Hashtable();         private string _defaultTemplateName = null;         private IEnumerable _dataSource;           public ObjectRepeaterDetermineTemplateDelegate DetermineTemplate;    ...

Full Code Snippets Archive