How not to Code: The String Identity Function
Yesterday I started working on a small system that’s been having some problems lately. Reviewing the code, I quickly came across the following function:
public static string IsNullToString(string _str)
{
string name = null;
if (_str != null)
{
name = _str.ToString();
}
return name;
}
Now, it seems that if that function receives a null it returns a null, and for other string it’s returning that string. Remembering that String is a sealed class and cannot be inherited, and that String.ToString() does very little, I fail to see why this function is used every time a string is printed out, 108 times in the code…
See Also:
Converting DataSets to Strongly-Typed DataSets
One of the first things shown to me when I started working in my company was Microsoft’s Data Access Application Block. One thing that bothered me was that I constantly had to convert DataSets it returned to strongly typed DataSets used in our programs, which usually meant merging the returned data set with a newly created DataTable. To relieve myself of those repeating tedious 6 lines of code, I made this little class that converts untyped DataSets and DataTables to a strongly typed DataTable.
In a first attempt to solve this problem, I used reflation to create an instance of the typed DataTable, so I had to pass the method the type of the DataTable, and cast it back to itself.
This is a more elegant solution, using generics:
/// <summary>
/// Helper methods and functions.
/// </summary>
/// <typeparam name="T">A strongly type DataTable.
/// A DataTable of type T will be returned from the DataSet.
/// </typeparam>
public static class DataSetAdapter<T>
where T : DataTable, new()
{
/// <summary>
/// Convert the first DataTable from a DataSet to a
/// strongly-typed data table.
/// </summary>
public static T convert(DataSet dataSet)
{
if (dataSet == null)
return null;
if (dataSet.Tables.Count == 0)
return null;
DataTable dataTable = dataSet.Tables[0];
return convert(dataTable);
}
/// <summary>
/// Convert an ordinary DataTable to a strongly-typed
/// data table.
/// </summary>
public static T convert(DataTable dataTable)
{
if (dataTable == null)
return null;
T stronglyTyped = new T();
// add data from the regular DataTable to the
// strongly typed DataTable.
stronglyTyped.Merge(dataTable);
return stronglyTyped;
}
}
The use of the class if pretty straightforward, just pass the DataSet and the Type:
DataSet employeesDataSet = OracleHelper.ExecuteDataset( connectionString, storedProcedure, parameters); EmployeesDataTable employees = DataSetAdapter<EmployeesDataTable>.convert(employeesDataSet);
Links:
Using Sharepoint’s File Type Icons
We have an old web part that displays documents from a list, and shows an icon next to file. After reading the list, the web part has a long “if-else” block that checks the files extension and displays the proper image. The images come from sharepoint template directory. I need to update this web part to display more file types icons. At first we though to create a list that maps images to extensions, but after little research I’ve decided to read sharepoint’s docicon.xml file.
First, we need to find the file docicon.xml. The file usually sits under
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\template\xml\docicon.xml
The xml directory is not mapped on the IIS, but we can still find it using this little trick:
string path = MapPathSecure("~/_layouts/");
path = Path.Combine(path, @"..\xml\docicon.xml");
Now all that’s left is to read the xml and add the data to a collection:
(This is actually the first time using XmlDataReader, but how wrong can I be?)
First, we define two members, for all files and the default icon, and then go through the XML nodes:
protected NameValueCollection fileTypeIcons;
protected string defaultIcon;
protected void readIconsXml()
{
XmlReader reader = XmlReader.Create(path);
reader.ReadToFollowing("ByExtension");
//find the first child. This doesn't skip the first node.
reader.ReadToFollowing("Mapping");
while (reader.ReadToNextSibling("Mapping"))
{
readOneNodeMapping(reader);
}
//find the default icon
reader.ReadToFollowing("Default");
reader.ReadToFollowing("Mapping");
defaultIcon = reader.GetAttribute("Value");
}
protected void readOneNodeMapping(XmlReader reader)
{
string tempKey = null;
string tempValue = null;
tempValue = reader.GetAttribute("Value");
tempKey = reader.GetAttribute("Key");
if ((tempKey != null) && (tempValue != null))
fileTypeIcons.Add(tempKey, tempValue);
}
And that’s it. We have all icons defined in the site ready in our project.
