Sunday, December 28, 2008

The joy of discovering new music

A couple of months ago, I came to an important realization. Many of my favorite bands and artists have become my favorites, because of the guitar. This is especially true for many of my favorite songs. "Fragile" by Sting, "Private Investigations" by Dire Straits, most everything by Mike Oldfield, and the list goes on and on.

I believe I've had this thought for many years, the thought of discovering music, that was all about the guitar, the acoustic guitar. Every time that I've tried to find some artists, that do pure guitar music, I've failed. But not this time. With a little help of some cool new websites, I got on track to find a truly amazing duo, Strunz & Farah.

I initially tried to google spanish guitarists, but the Flamenco genre was not quite what I was looking for. Pandora.com to the rescue. I added the artists that I've found, and hit play. The first few songs got a thumbs down, but slowly, I was on my way to find Strunz & Farah, two truly amazing musicians. You can read more about them here:


I fell in love with their music instantly, and the first thought was, wow, it would be really cool to go to a concert with these guys. Are they still active? They have been together for some 30 years. Luck was on my side, and I found a venue close to home, which was perfect. My wife and I enjoyed the concert tremendously. Admittedly, me more than her, but it was an amazing display of how you can be in supreme control of a guitar, and produce beautiful music.

If they come back, I'm going to go see them again.

Wednesday, December 3, 2008

Speeding up your INSERT's with SqlBulkCopy

With version 2.0 of .NET, we got access to a new class in System.Data.SqlClient called SqlBulkCopy. This class will greatly improve your performance when doing mass inserts into your database.

Using SqlBulkCopy instead of regular business objects doing inserts in a loop, reduced the time to insert about 120K rows from 18 minutes to 3 minutes.

Most examples you find googling SqlBulkCopy will show you an example of how to use the class copying from one table to another. I needed an example of how to assemble my own source data into a DataTable, and then copy that to a new Sql table using SqlBulkCopy. So, here goes.

First, we need an instance of the SqlBulkCopy class. In this example, we're passing along a connection string, and the option to keep the identity of the Id column, we'll be setting up in our source data table:
SqlBulkCopy sbc = new SqlBulkCopy();
We also need the DataTable to use as our source for the copy:
DataTable newTable = new DataTable("MySourceTable");
Once you're done adding rows to your newTable, you can submit the the entire table contents to the server with this statement:
sbc.WriteToServer(newTable);
That's it! In some cases you need to use the SqlBulkCopyMapping class to map columns from your data source, to columns in your target table. There's lots of good documentation on that on a google search.

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