Implementing an XML Section Handler in .NET
Introduction
For years I've been doing contract software development for companies in North America. Somehow I seem to spend winters on the west coast and summers on the east coast. While on the west coast I like to get down to Las Vegas and play some Blackjack. Of course, winning is more desirable than losing, so I decided to look up the odds for all of the Blackjack dealer/player combinations and write a small Blackjack game. (To take a look, click the Blackjack Game Setup link at http://www.softconcepts.com.) The game uses a six-deck dealer chute, cards.dll (the same .DLL used to create games like Solitaire that come with Windows), and .NET's randomization to deal random hands. Doubling, splitting, surrendering, and betting are supported. For each hand, the game suggests the best possible play. The results have been positive when I play for cash; while I don't bet huge dollar amounts, I've had much better Blackjack rounds and win more consistently.
My Blackjack game is written in C#. One of its features is to externalize default settings; for example, whether surrendering is supported. Because these default settings were intended to be used as part of the state of the default player options, they conceptually comprise the state of a logical object PlayerDefaultOptions, and it was desirable to treat them that way. This is what the IConfigurationSectionHandler does.
The AppSettings collection permits easy reading of individual data items from .config files in .NET, but sometimes you want whole objects instead of disparate, unrelated data items. Used to implement exception publishers in Microsoft's Exception Management Application Block, the IConfigurationSectionHandler is designed to support objectifying custom blocks of XML. In this article, I'll show you how to define a custom XML block, implement an IConfigurationSectionHandler, and register the assembly containing the custom XML section handler as the code that should be loaded and employed whenever your custom XML section is encountered in the .config file.