Wednesday 23 November 2011

Useless comments, useful clean-up

I guess all seasoned developers have come across code containing useless comments.

Some comments are so obvious that they don't deserve their existence in code and when they exist, they are nothing but code pollution.

Examples:
Code Snippet
  1. // Stored procedureName <-- Useless & too obvious comment. Even my grandma could tell that this is a constant storing a stored procedure name
  2. const string storedProcedure = "GetSoftwareArchitectsByCountry";

Code Snippet
  1. // Parameters for the stored procedure <--Useless & too obvious comment
  2. database.AddInParameter(command, "ExecutionCountryId", DbType.Int32, executionCountryId);

Useless comments cause code pollution (too many lines of "code") and require code cleanup which in turns requires time and I think we all agree that time is money. So dear developers don’t waste your effort in writing useless comments… use this time to do other useful things ;)

Cheers,
Shah MOHAMOD

Friday 19 August 2011

Implementing the SQL "In" keyword behavior in C#

Often when checking whether a value matches a value in a list of values, we use syntax like this:

if (myObject.MyProperty.Equals("value1") || myObject.MyProperty.Equals("value2") || myObject.MyObject.Equals("value3"))
{
// do something
}

This syntax may become cumbersome especially if you need to check your object's value against more than 2 or 3 values. For this problem, one may ask if the SQL "IN" keyword like behavior is available in the .Net Framework. Unfortunately, the anwser is NO. You have to implement this by yourself.

The method below provides functionality equivalent to the SQL "IN" keyword, i.e., it checks if a value is found in a list of values.



Code Snippet



  1. public static bool ValueIn<T>(T testValue, params T[] values)

  2.         {

  3.             return ((IList<T>)values).Contains(testValue);

  4.         }






Then our code sysntax will look something like this:

if (MyStaticClass.ValueIn(myObject.MyProperty, "value1", "value2", "value3")
{
// do something
}

As you may have noticed, the code syntax becomes much more readable and less cumbersome to write.

By using extension methods in .Net, the code syntax could be further simplified. For example, we could write:

if (myObject.MyProperty.ValueIn("value1", "value2", "value3")
{
// do something
}

Note:



  1. In the .NET Framework version 2.0, the Array class implements the System.Collections.Generic.IList, System.Collections.Generic.ICollection, and System.Collections.Generic.IEnumerable generic interfaces.

    These implementations are provided to arrays at run time, and therefore are not visible to the documentation build tools. As a result, the generic interfaces do not appear in the declaration syntax for the Array class, and there are no reference topics for interface members that are accessible only by casting an array to the generic interface type (explicit interface implementations). The key thing to be aware of when you cast an array to one of these interfaces is that members which add, insert, or remove elements throw NotSupportedException.

BizTalk Server 2006 cannot access BizTalkMgmtDb database on SQL Server



Context:
The other day I tried to connect to a remote BizTalk Server group using my local BizTalk Administration Console.

Problem:
I was unable to do so even if I was a member of the BizTalk Administrators group of the remote installation. I was being displayed the following dialog box telling me that BizTalk Server 2006 cannot access database BizTalkMsgmtDb on SQL Server:




I checked each of the numbered points displayed in the dialog box to finally discover that I had the wrong version of BizTalk Server installed on my machine. Actually, I was trying to connect to a BizTalk Server 2006 R2 instance with a BizTalk Server 2006 (no R2) console.

More information on how to check your BizTalk version can be found here.

Solution:
I uninstalled my BizTalk Server 2006 and installed BizTalk 2006 R2 and this corrected my problem. I was able to connect to a remote BizTalk server with my local BizTalk Administration console.