Month: November 2014

Generate Your WCF Files From Your Spreadsheet

Posted on

Meet your Deadlines with T4 Template Files

Here’s my situation: we’re building an SOA and spending a long time on our design process. We need to revise our methods and parameters repeatedly, and we’re building a lot of methods and classes. We also need to build some documentation (a specification file). We have a tight deadline, so, after we finally finish modifying all those method signatures, we need to quickly build an interface for our B2B partners. It would be really nice if I could build, or rebuild, all those classes from a master definition file!

Solution

  1. Enter all the method definitions in an Excel file
  2. Also enter the data classes and their parameters in the same Excel file
  3. After all the revisions and negotiations, use the data in the Excel file to generate the code files using T4 templates
  4. Also generate the spec file using the same process

Remarks

I built a project using T4 templates to generate my code files. Note that the data contract files for WCF are mostly boiler plate, but it is still a lot of work. I can use the results to build WSDL in a few seconds. The benefits is that our B2B partners can code against that WSDL immediately, and I can update it easily.

I’m quite happy with the results. Running the app is easy, and there are no issues like improper casing for names or inconsistent spellings. Plus, when we make changes, the spec file can be immediately regenerated and be completely consistent with the code. Furthermore, it easy to write the my code generator; actually I built a working prototype one afternoon.

ScreenShot
Screen shot showing the inputs to generate the classes. The namesapces and other textboxes are auto-suggested from reading the master definition file.
All these classes were created in a split second when I clicked the button. They define the guts of a WCF web service.
I generated all these classes in a split second by clicking the button. They define the guts of a WCF web service. As mentioned above, the data classes are mostly boiler plate, the service contract (interface 100% usable straight from auto-generation)  and we can customize the service file as necessary.

,

How it Works

You can add a T4 Template file to any .NET project. T4 templates are designed for generating code from sources like a master definition file, database file, etc. T4 templates are a mixture of text (payload) and markup (your code that injects varying content). Here’s a sample for my auto-generation project:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".txt" #>
<#@ import namespace="AutogenerateWCF.SupportingClasses" #>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace <#= NamesapceToUse #>
{
    public class <#= ServiceClassName #> : <#= InterfaceName #>
    {
<# foreach(MethodInfo mi in Methods) { #>
	<#= mi.ReturnType #> <#= mi.Name #>(<#= mi.FormattedParameterList #>){
		return new NotImplementedException();
	}

<# } #>
	}
}

In the sample above, all my markup is in between the tags <# and #>. The markup references class properties on my T4 class. I wrote my T4 class to expose properties called “NamespaceToUse”, “ServiceClassName” and “Methods”. When I use my template (explained in depth below), it generates output that looks like the following:

using System.Runtime.Serialization;
using System.ServiceModel;

namespace B2BWebServcies
{
    public class B2BHPServices : IB2BWebServcies
    {
		GetLocationResponse getLocationServiceCenter(Credential credential, GetLocationServiceCenterRequest request){
			return new NotImplementedException();
		}

		GetInventoryDataResponse getInventoryData(Credential credential, GetInventoryDataRequest Request){
			return new NotImplementedException();
		}
	}
}

 Adding a T4 Template to Your Project

Add a new item to your project; for the type, choose ‘Text Template’:

How to Add a T4 Template to your Porject
How to Add a T4 Template to your Porject


After you add your Template file, change its Custom Tool to ‘TextTemplatingFilePreprocessor’

Select your new T4 file and change its custom tool to 'TextTemplatingFilePreprocessor'
Select your new T4 file and change its custom tool to ‘TextTemplatingFilePreprocessor’

Now, add a partial class with the same class name as your template file. The partial class will hold the properties you will utilize in your markup, such as ‘NamespaceToUse’. Next, populate your markup, injecting class properties where necessary.

To use your template,

  1. Set the class properties using whatever data source you need; in my case, I loaded from my master definition file (exported from Excel)
  2. Invoke your template class’s TransformText method – this returns a string containing your final output!

To learn more, download my sample code here.