Thursday, August 31, 2006

BizTalk Server and Code Generation

BizTalk Orchestrations are visual descriptions of processes, models of behaviour and interactions. These models are the basis for a code generation step, during which BizTalk converts the orchestrations into C#. The resulting C# files are then compiled, and afterwards deleted. The models/orchestrations themselves are text files you can open in Notepad, and this holds true for all the BizTalk-specific project files, such as Maps, Schemas or Pipelines.


I am a firm believer in automation and Code Generation, and these strategies have been very helpful in BizTalk projects, which is maybe not the most obvious candidate for the use of code generation.
Everything that is done twice, the saying goes, is worth scripting. :-) As to code generation itself, I resort to it as much as possible, being it based on Domain Specific models (such as BizTalk Orchestrations), or simple Template-based generation, using tools like the excelent CodeSmith.


Typical figures state that Code Generation can be used to produce up to 60-80% of a solution's code, especially in components such as the data access layer. What if we try to apply Code Generation to BizTalk? Here are some ideas.


- Project files: typically we do "Add > New Project > Empty BizTalk Project", select the target folder, later on configure the project with the key, fill in copyright configuration, etc. This can be quick, no doubt, but if you have a lot of projects, generation is definetly quicker, and the result is uniform. These project files can also include references to other projects or components you know you need.


- Sample schemas: if you know that each integration point demands, for example, 3 schemas (source schema, canonical schema, target schema), you can generate starting schemas for these messages. If you can know in advance some characteristics of the schemas (ex: xml vs delimited) you can take this in consideration, and also fill in the appropriate schema namespaces, and other attributes.


One step further would be to generate schemas, or at least initial versions of schemas, based on a meta-description stored, for example, in an Excel file. With only 4 columns: Field name, Field type, Max Occurs and Min Occurs, you can generate very interesting base implementations.


- Sample maps: in the example above, you could generate four map files between the different formats, empty but for the selection of the correct maps as target and destination.


Again, a more ambitious step would be to describe the maps themselves using Excel, even if only with direct links. One line of metadata such as:


"name, string, 1, 1, MyName, string, 1, 0"


Could result in 2 schemas, the first with a single element "name" of type xs:string and max/min occurs 1, the second with an element "MyName", string, maxoccurs 1, min occurs 0. Plus, you could generate 2 maps, mapping the values together, from the source to the target schema, and vice-versa.


- Sample orchestrations: apart from generating empty orchestration (.odx) files, you can also do stuff such as automatically add a Receive Shape with Activate=True at the top and Long Running transaccionality turned on, or simply include common code refering to other generated schemas and/or maps.


- Deploy scripts: especially when using BizTalk Server 2004, deploy/undeploy scripts are almost critical to automate operational tasks and aid in the development cycle. These can be easily generated in a scenario such as the ones described above.


 


In a recent project, and from a single input parameter (the name of the "Concept" being integrated), we generated combinations of all of the elements described above, in a total of about 25 files, with an obvious productivity gain. It's much easier to delete the excess than to create just the needed files from scratch.


If you have any other ideas for code generation over BizTalk, please let me know. :-)


[Cross-Posted de http://www.arquitecturadesoftware.org/blogs/joaomartins]

No comments:

Post a Comment