Leaning on the Language and Leaning on the Libraries
What fun I'm having. I'm collaborating on a project for work. We're still using Visual Studio 2003 and .NET 1.1, but we're still wildly productive.
One interesting thing I've learned is that I lean on the libraries, while Paul, my counter-part leans on the language. He knows what language constructs to use and I know what BCL methods to use. Together we make an effective pair, but this realization helped us both better understand our weak areas. One can write lots of C in C#, if you know what I mean.
Do you lean on the language or the libraries?
We've been doing some fun stuff with XML lately, you might have noticed. We've used:
- XmlBookmarkReader - An XmlReader implementation that lets you rewind. A Helena Kupková production.
- StringWriterWithEncoding - A StringWriter that is a schtickle smarter about text encoding.
- Abstract BoilerPlate HttpHandler - Re-ripping off Phil's Improved Rip-off of me.
-
XmlFragmentWriter - Serializing objects to fragments of XML.
-
XmlNamespaceUpgradeReader - Deserializing objects from fragments of XML.
-
SgmlReader - An XmlReader implementation that reads in SGML and autocloses tags. Then we changed how the autoclosing worked.
-
A cheesy-but-working-so-far-not-quite-SGML-writer-derived-from-XmlWriter-but-really-using-HtmlTextWriter. (Code below, for educational purposes)
-
We've also used Helena's Xml Diff to do differences on the infoset, rather than the syntax.
Also. we're using:
- TestDriven.NET, NCover, NCoverExplorer with TDD
- CodeRush with Refactor Pro
- I awoke today to see that a new version of CodeRush and Refactor was released. It includes 11 new refactorings and my favorite new feature - it paints a tiny number to the left of each method that is the Cyclomatic Complexity for that method. Brilliant.
class KindOfPartialSgmlButMoreOfAnOfxXmlWriter : XmlWriter
{
public KindOfPartialSgmlButMoreOfAnOfxXmlWriter(TextWriter w)
{
_htmlWriter = new HtmlTextWriter(w);
}
public override void Close()
{
_htmlWriter.Close();
}
private Stack _tags = new Stack();
private HtmlTextWriter _htmlWriter = null;
private bool _suppressNextEndElement = false;
private bool _suppressNextText = false;
private string _previousElement = null;
public override void WriteStartElement(string prefix, string localName, string ns)
{
_htmlWriter.WriteFullBeginTag(localName);
_previousElement = localName;
_tags.Push(localName);
}
public override void WriteString(string text)
{
if (_suppressNextText == false)
{
_htmlWriter.Write(text);
_suppressNextEndElement = true;
}
}
public override void WriteEndElement()
{
string endtag = _tags.Pop() as string;
if (_suppressNextEndElement && endtag == _previousElement)
{
_suppressNextEndElement = false;
}
else
{
_htmlWriter.WriteEndTag(endtag);
}
}
public override void Flush()
{
_htmlWriter.Flush();
}
public override void WriteWhitespace(string ws)
{
_htmlWriter.Write(ws);
}
public override void WriteRaw(string data)
{
_htmlWriter.Write(data);
}
public override void WriteChars(char[] buffer, int index, int count)
{
_htmlWriter.Write(buffer, index, count);
}
public override void WriteQualifiedName(string localName, string ns)
{
_htmlWriter.WriteBeginTag(localName);
}
public override void WriteEndAttribute()
{
_suppressNextText = false;
}
public override void WriteStartAttribute(string prefix, string localName, string ns)
{
_suppressNextText = true;
}
public override void WriteRaw(char[] buffer, int index, int count)
{
_htmlWriter.Write(buffer,index,count);
}
public override void WriteProcessingInstruction(string name, string text)
{
_htmlWriter.Write(text);
}
#region Stubs
...here are overriden versions of everything else from XmlWriter, stubbed and not used. Removed for tidiness
#endregion
}
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
Comments are closed.