T4 Code Generator for WCF RIA Services

I had always wanted to have a technical blog since I joined Microsoft, but somehow never ended up writing one. Now, finally, after we released WCF RIA Services V1.0 SP1 Beta and WCF RIA Services Toolkit October 2010 last week, I have decided to blog about the T4 Code Generation feature we have added to the WCF RIA Services Toolkit.
This post will focus on the design and use of the feature. I will follow-up with a post on the extensibility mechanism of the code generator.

The Code Generation module that ships with RIA Services is now extensible to allow different Code Generators to be used instead of the default CodeDom based code generation. This is to allow customers to modify the code generation process and generate custom code on the client.

To that end we have shipped the T4 Code Generator that is customizable to allow you to tweak the code generation and modify the generated code as required. Currently it supports only C# based code generation.


The T4 Code Generator consists of 2 parts:
1. CSharpClientCodeGenerator

This is the main component of the T4 Code Generation process that hooks into the extensibility features of the WCF RIA Services Code generation process. It controls code generation of different types on the client.

2. The 5 different code generators:

  • CSharpDomainContextGenerator
  • CSharpEntityGenerator
  • CSharpComplexObjectGenerator
  • CSharpEnumGenerator
  • CSharpWebContextGenerator

As their names indicate, each of these generators generate code for their respective types (CSharpEntityGenerator generates Entity types in C# and so on). These code generators generate code using the TextTemplates. (For more information about T4 you can follow the msdn documentation or Oleg Synch’g blog)

The CSharpClientCodeGenerator implements the IDomainServiceClientCodeGenerator interface and returns a string containing the generated code. It also declares itself to the WCF RIA Services code generation using the [DomainServiceClientCodeGenerator] attribute (which uses a MEF based approach underneath).

The code generation process calls passes a list of DomainServiceDescriptions to the CSharpClientCodeGenerator. Using that list, it generates code for DomainContexts, Entities, ComplexObjects, Enums and the application wide WebContext by calling into the respective individual code generators.

The CSharpClientCodeGenrator also contains public properties for ClientCodeGenerationOptions and ICodeGenerationHost. It acts as the context for each of the different code generators.

Using the T4 Code Generator

To use the T4 Code Generator instead of the default CodeDom based code generator you will have to do the following:

  1. Add a reference to the assembly Microsoft.ServiceModel.DomainServices.Tools.TextTemplate which contains the T4 Code Generator.
  2. Edit the silverlight project file and add the following tag:
    Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35

Now when you build, the code will be generated on the client using this code generator.

I will cover how this T4 Code Generators can be extended to generate custom code in my next post.


About varunpuranik
I am a Developer at Microsoft. I work in the App Server Group on WCF RIA Services.

8 Responses to T4 Code Generator for WCF RIA Services

  1. kumar228 says:

    Great to hear about this feature.

  2. Pingback: T4 Code Generator Dec ‘10 Update « varunpuranik

  3. Pingback: RIA Services

  4. Ken says:

    Add Microsoft.ServiceModel.DomainServices.Tools.TextTemplate reference to what?!! People don’t always know what you assume they know dude. Typical of MS people.

  5. Ken says:

    Heres another one.. do you expect me to just know where the Microsoft.ServiceModel.DomainServices.Tools.TextTemplate assembly is? Idiots

  6. I see that VB developers are getting the shaft again. I don’t know why I even bother.

  7. Ravi says:

    Hi Varun

    I am looking on feasibility of following approach to create WCF generator using T4 template:

    • Visual Studio Add-in Template – Titled “WCF Generator”
    • Dynamic generation of class file from Linq to SQL based on Database connection information and database name.
    • Dynamic Generation of WCF Service by including above bulleted class file’s methods and properties.

    Could you please provide few inputs on this.


  8. “T4 Code Generator for WCF RIA Services varunpuranik” actually enables me personally ponder a small bit extra.

    I really enjoyed every single element of this blog post.
    Thanks for the post -Delmar

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: