Thursday, November 6, 2008

C# Refactoring with Design Patterns, Singleton

Design patterns have been around for a very long time. As a matter of fact, I found an MSDN article dating back to 2001, mentioning the Singleton, Strategy, Decorator, Composite and State design patterns.

Why don’t we start looking into refactoring with the Singleton design pattern. As you probably know, the Singleton pattern is about offering a single, global point of access to a class, that has only one instance.

One thing I usually do for every application that I work on, is to create a configuration class, that retrieves all of the .config settings, and in some cases, also retrieves configuration settings for the application from a back-end SQL database.

Up until learning about Singletons, I passed the configuration class on to other classes that needed it. Instead, the Singleton design pattern offers a much more elegant approach. Let’s look at my configuration example, without using the Singleton pattern:

public class Config
{
public Config()
{
ConfigSetting1 =
ConfigurationManager.AppSettings[“Setting1”];
}

public string ConfigSetting1 { get; private set; };
}
Now, this is pretty straightforward. When you instantiate the Config class, you’ll get access to the properties that got fetched for you, from the .config file, and/or from other sources. Then I would normally pass my config instance along to the constructor of other classes and so forth. This can be completely avoided with the Singleton design pattern. The idea is, that since my Config class will only exist once in memory, I can simply let my other worker classes access the static singleton config class directly.

Here is the basic approach to create a singleton class in C#:
public sealed class Config
{
// Static constructer.
static Config()
{
}

// This will trigger the private constructor of this class.
private static readonly Config _Config = new Config();
    // On first access, the static constructor will fire, in
// turn triggering the private constructor.
public static Config Instance
{
get { return _Config; }
}

// Prevents a default instance of the Config class from being created.
private Config()
{
// Do your work here...
}
}
The beauty of this approach is that it is thread-safe, something that is extremely important for this design pattern. For a much more detailed explanation of this, along with alternate approaches, I urge you to have a look at this very good article by Jon Skeet:

Implementing the Singleton Patterin in C#
http://www.yoda.arachsys.com/csharp/singleton.html

Wednesday, November 5, 2008

SysFader/AutoComplete: iexplore.exe - Application Error

For some time at work, I had this really weird problem, in that I couldn't open any links to Office documents from wtihin Internet Explorer. A google search revealed a lot of fixes involving disabling page transitions in IE, or adjusting cache sizes etc. All of them didn't do the trick for me, until I came across this link on Jeff Widmer's blog:

http://weblogs.asp.net/jeffwids/archive/2007/03/12/internet-explorer-and-office-documents-sysfader-iexplore-exe-application-error.aspx

Basically, the issue is on a box that has had Office 2003 and Office 2007 installed, and the beautiful fix is to rename the

OWSSUPP.DLL file in the C:\Program Files\Microsoft Office\Office12 folder.

Lets not discuss how elegant that solution is, but give our minds some peace by accepting that the problem is now gone :-)

Update 11/11/2008 - Turns out, that simply renaming the OWSSUPP.DLL file doesn't fix the issue. Apparently, there is a compatiblity issue with the Office 2007 version of this file. The solution is to copy the one from your Office11 folder. So, rename your OWSSUPP.DLL in the Office12 folder, copy the one from you Office11 folder, and register it. To register the DLL, open a command prompt by running the cmd executable, and navigate to your Office12 folder (see above) and issue this command "regsvr32 owssupp.dll". This will give you an error message, but the registration still was successful. Test it by opening an office document on a SharePoint site.

Hope this helps.

Tuesday, November 4, 2008

Regular Expression Cheat Sheets

Speaking of putting useful things on your (cubicle) wall at work. Today, I realized I needed to lookup some regular expression syntax, and remembered at one time having a really useful cheat sheet up on the wall.

So, setting out to find one, I came across these excellent sites:

Added Bytes:
http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/

RegExLib.com:
http://regexlib.com/CheatSheet.aspx

OpenCompany.org:
http://opencompany.org/download/regex-cheatsheet.pdf

Monday, November 3, 2008

Visual Studio 2008 Keyboard shortcuts

Many years ago, I was fortunate enough to have my mouse die on me, in the middle of a weekend of intense software development. Back then, I used the mouse for everything. So, without a mouse, I had to figure out shortcuts for things I would always use the mouse for. Over time, I became much faster on the computer using keyboard shortcuts, as opposed to grabbing the mouse to do some task. To keep up with that speed advantage, one needs a good list of keyboard shortcuts for the most common applications used. What better one to pick here, than Visual Studio 2008.



Print this one out in Landscape mode and hang it up on your wall.

Download the colored and grey-scaled images here:
http://www.microsoft.com/downloads/details.aspx?familyid=e5f902a8-5bb5-4cc6-907e-472809749973&displaylang=en

Sunday, November 2, 2008

C# 4.0

Now that PDC 2008 is over, you've probably heard all about C# 4.0. Seems like there are lots of possibilities to explore on the new dynamic keyword. The two top features I immediately like, are named parameters, and covariance.

Here is an example of a method taking three parameters, where the first is required, and the remaining two are optional:

public void Method(int a, int b = 7, int c = 10)
{

}

Note that optional parameters must be placed at the end of the parameter list.

Now, to call this method, you have several options, since you don't have to specify the parameters in order, if you prefix them by their name. So, here some examples of valid method calls:

Method(1);
Method(1, c:0);
Method(1, b:10, c:30);

So, for the nice class library developer, this should make life easier, since you don't have to overload your class constructors, or methods, into many mutations, because you want to accomodate all kinds of scenarios in your class.

Check out this Channel 9 video for a great demo of the new features from the C# team itself:
http://channel9.msdn.com/shows/Going+Deep/Inside-C-40-dynamic-type-optional-parameters-more-COM-friendly/

Also, Bart de Smets has a good blog entry on optional parameters:
http://community.bartdesmet.net/blogs/bart/archive/2008/10/31/c-4-0-feature-focus-part-1-optional-parameters.aspx

And finally, Anders Noraas has a good article on covariance:
http://andersnoras.com/blogs/anoras/archive/2008/10/28/c-4-0-covariance-and-contra-variance.aspx