9.3.05

Microsoft Enterprise Library - custom Serializer Transformer provider

A couple of weeks ago I started playing with MS Enterprise Library 1.0. Basically MS EntLib is a
package with various logically connected Application Blocks put together. If you've used AppBlocks before EntLib won't be a problem to understand, moreover the concept is the same but "better".
Great feature is Enterprise Library Configuration utility which enables you to make all necessary adjustments to your .config file through UI. :)
I only regret that assemblies that contain your custom providers must reside in a GAC or folder where EntLibConfig utility is installed in. Or you could add <codebase> node in Enterprise Library or machine config files. Wouldn't it be cool if you could tell EntLibConfig utility: "Look, the thing I wrote is correct, so ignore reflection errors you're receiving" or instruct it to look for assemblies in a specific folders.
Well, whatever...

I jumped on a Enterprise Library bandwagon starting with Configuration Application Block regarding project I'm currently involved with (check my previous post) and although I still have to crawl through all of the blocks in Library, I already ran into problem with XML Serializer Transformer provider.

What was the problem? Basically put, I wrote custom objects to hold configuration data and decided to use builtin XML Serializer Transformer provider to write configuration data to XML file. (XML Serializer Transformer uses .NET's XML serialization.)
My configuration objects hold many name-value pairs of information, but still require several custom properties, so I inherited them from DictionaryBase. Problem No.1 XmlSerializer (which is used by XML Serializer Transformer) doesn't support serializing object which implements IDictionary interface.
Ok, I'll find some other way to create dictionary-like class. How about NOT inheriting object from DictionaryBase but adding a NameValueCollection or Hashtable as a private member and wrap calls to them in custom methods. Wrong! Same result as Problem No.1: these types implement IDictionary. :(
Googling for an hour or so, and a possible result is here: inherit from NameObjectCollectionBase. Problem No.2 Ok, now it serializes name-value pairs, but it doesn't serialize public properties. (Apparently that's BY DESIGN?!?) DAMN! Now I'm really pissed!
Back to Google...

Thank you http://groups.google.com and someone who posted this solution: SOAP serialization will serialize almost anything to XML without a problem (how would .NET Remoting work without it? :) ). I honestly don't know why I didn't remember this myself...

So I wrote custom serializer transformer provider for Enterprise Library Configuration Block :"SoapSerializerTransformer". The process is quite simple...
1. Create a class, inherit it from TransformerProvider
2. Implement three methods: void Initialize(ConfigurationView configurationView), object Serialize(object value) and object Deserialize(object section) - you can copy method bodies from
XmlSerializerTransformer and then make changes, pretty straightforward.
3. Make adjustments in your .config file (you can use Enterprise Library Configuration utility - but don't forget to copy the assembly where your provider is implemented in folder where EntLibConfig utility resides or EntLibConfig will report reflection errors during Validation process)
And that's it!

To help you get started feel free to download and freely use my implementation (C#).

Pozdrav!