Recently, I was asked to look at some code and I want to share something that I think should ever be placed in an enterprise level application. Let me say that we are not permitted to use Entity Framework, so everything that we do now is hand jammed.

In this application we have 20 classes in our DataAccessLayer and in 10 of those there is a private method called GetSql().

It looks like this:

public string GetSql()
{
StringBuilder sp = new StringBuilder();

sp.Append("Select");
sp.Append("Field1");
sp.Append(", Field2");
sp.Append(", Field3");
sp.Append(", Field4");
sp.Append(", Field5");
sp.Append(", Field6");
sp.Append(" FROM Table");

return sp.ToString();
}

If there were Joins and Where Clauses they would have their own methods that are designed the same way. I understand why you would use a stringbuilder, as a matter of fact there is a place where this is used in a proper scenario

static string Main(List<int> arguments)
{
StringBuilder builder = new StringBuilder();
foreach (int i in arguments)
{
builder.Append( i.ToString() + ",");
}
return builder.ToString();
}

But I ask and plead, please don’t write the first scenario, use the StringBuilder properly. That is my rant for today, thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *