Service Abstraction and XSLT Transformation

Basically I’m writing a simple service to be consumed by any number of parties.  One of the requirements our principle architect gave us was to conform to a specific data format that essentially abstracts service consumers from the complexity under the hood.  So we are decoupling service consumers from the specifics of the service meaning we can significantly tweak the service and technology behind the service without breaking service consumers, too an extent.  Nothing revolutionary here, more or less contract first development providing another layer of abstraction to further insulate service consumers from the service implementation.

In adherence to “application architecture for applications and services” the service passes business entities between its various layers.  Those business entities being simple C# classes in this case modelled directly from the database schema.  I.e. if we have a workflow table in the database with x columns then we have a business entity(C# class) with x properties.

The implication of abstracting the service is having to translate from that abstracted XML to the business entities.  The obvious and correct place to do this translation is within the service facade.  One technique that we could use is to write helper methods that use XPATH to map data to the business entities.  Another way would be to deserialize the XML but again we’re still talking about a whole heap of C# properties mapping here, a truck load of code. 

Wouldn’t it be great to be able to map the abstracted XML to the business entities without writing a stack of C# code.  The technique I used was to build a business document from the business entities.  I literally defined a simple business document(C# class) that exposes public properties for each of the business entities.  I then mark those properties with the appropriate attribute so I can guarantee the serializer will serialize the business document to XML that will conform to the XML schema I’m about to generate from the business document.  I now used xsd.exe to generate an XML schema for the business document.

Having an XML schema for the business document means I can use the BizTalk mapper to map to and from the abstracted XML.  Really I can use any tool that’s going to pump out XSLT.  To actually transform the data is about 10 lines of code within the service facade as opposed to the hundreds of lines of C# property mapping code I would have otherwise had to write.

A pretty basic technique to reduce coding effort but maintain pattern purity.

This entry was posted in .NET. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s