T4 Code Generator December 2010 Update

As I mentioned in my first blog on the T4 Code Generator the T4 Code Generator was released as a experimental feature with almost no testing done. Naturally, it had bugs which many of you have faced. And although working around the issues is relatively easy because of the extensibility features of the T4 code generator, it is certainly unnecessary work. So we have made a number of code fixes and improvements to the T4 Code Generator that will be released with the WCF RIA Services Toolkit release today.

Here is the list of what has changed and what has not in the T4 Code Generator –

  1. First of all, the API remains the same. So the list of methods that you can override to customize the generated code and the way to do it remains the same (as described in my previous post).
  2. There were bugs around the type names being generated incorrectly (including the one blogged by Jeff in his T4 Walk-up experience post). They have been fixed now. So you should not get errors related to type names being generated incorrectly (including enums, generic types, arrays, etc).
  3. There were also bugs around the code that was being generated for Association Properties. Those have been addressed as well. We have done functional testing for this particular fix and made sure that all the association property cases work fine with the T4 Code Generator.
  4. We have fixed bugs and done QA testing to make sure that the code generated by the T4 Code Generator builds fine for all the different scenarios handled by WCF RIA Services. Note that this was not functional testing, but testing to simply ensure that the code generated builds without error.
    So in short, the state of the T4 Code Generator has improved significantly since the Beta release last month. We are still some distance away from the place we want to be, where all WCF RIA Services apps out there are guaranteed to work with T4 Code Generator, but this is a definite step in that direction. We hope to get there in one of our next releases.

Please let us know if you find any more bugs in the code generator and we will try to address them as soon as possible. Also, feedback about the API, features, usability and the T4 Code Generator in general will be greatly appreciated.


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

11 Responses to T4 Code Generator December 2010 Update

  1. Pingback: [Profoundly Esoteric Image]

  2. Marco says:

    This is a great news!
    I’m working on an enterprise size product where WCF Ria Services sit on top of nHibernate to deliver data on the backend. The ability to customize the generated domain context it’s a great, great news. I’ve tested the feature just 2 days after it’s deliver in the late october and despite some bugs it has provided me a new way to solve some issues.
    I’ve also some little suggestion on improving it’s usability, it would be good if you can give us more overridable methods (I’ve not tested this last bits, just downloaded and run my previous tests) and, maybe make those methods return the string that they are going to add in the underlying “GenerationEnvironment”.
    What I’m pointing out here is that in order to change the generated code I have to either cut ant paste with the exposed StringBuilder before and after the “base call” of the overrided method or rewrite the method from scratch. This is good, but I’ve seen that methods in the CSharpGenerators namespace are well splitted and the ability to override them one at once would be better!

    Anyway thanks for the news and for the update!


    • varunpuranik says:

      Thanks, Marco. I am glad to know that you are finding the T4 Code Generator useful.

      I also appreciate your feedback on improving the usability. Are you suggesting that we should add more virtual methods to the CSharpGenerators to make it more granular?

      Also can you please elaborate a little on what you are trying to accomplish. Because to change the generated code, you should not have to modify the exposed StringBuilder or the GenerationEnvironment. They are part of the underlying architecture used by Text Templates to build the output. You can refer to my previous post that goes through the process in some detail. I will also try to post a sample that modifies the generated code using the extensibility mechanism.

      Your feedback on the latest released bits will be greatly appreciated. Please let us know if you try them out.


      • Yes, exactly, just give us more virtual methods, in order to let us granurally override them.
        Ok, I’m going to better illustrate what I’m trying to do.
        Basing on some server side configuration, my custom implementation of T4 Code generator, modify the generated property getter/setter of entity on the domaincontext to lazy load their association.
        Imagine that I have an entity “Order” with a property “Customer” (association) and a collection of “OrderDetails”. Analyzing the nHibernate mapping, some xml metadataattribute and the query exposition method, my code decide to make the “OrderDetails” association “Lazy”, so that when they are first accessed, an asynchronous call is sent to the Domainservice, and the related OrderDetails are loaded.
        In order to accomplish this I have overriden the “GenerateAssociation” method of CSharpEntityGenerator. Well… both because I’m a bit lazy and because the generated code is almost perfect, I didn’t rewrote the whole method, but, after doing a base call I’ve played with the GenerationEnvironment to insert the code that I need.
        As you know the GenerateAssociation method is splitted in two methods (Singleton & Collection) and each of them is splitted in other methods etc etc. For this case could be useful if I could just override the “GenerateSingletonAssociationPropertyGetter” and “GenerateCollectionAssociationPropertyGetter”.
        Do you think I’m doing a misuse of T4 Code Generator ?
        Sidetracking to this, lazy load isn’t that easy neither isn’t a panacea (ther are more things to keep in mind that what I wrote here) but in certain context it works very well.
        Ok , I know, I can get through withoud those virtual methods, but they would make this task easier!

        p.s. I’m already using in development the last bits and until now it works seamlessly!

      • varunpuranik says:

        Ah, I see. I understand that in your scenario, maybe it is easier for you to let the base generator generate the code first and then go and modify it. However, that is not how we intended the T4 Code Generator to be used.
        Ideally, if you wanted to change something in the middle of the code that one of the virtual method generates, you would override that method and write the code to generate the entire modified code. But I agree having finer granularity would certainly help you here. We tried to strike the balance between having sufficient granularity without flooding the developer with dozens of virtual methods which would get equally confusing.

        In any case, I will keep this suggestion in mind and try to open up those virtual methods in our next release.

        Your feedback on the latest bits is greatly appreciated!

  3. srecov says:

    I have rather trivial problem. I wish to use code generator, however I can not find the assembly to reference it. I have installed WCF RIA Services V1.0 SP1 Beta (4.1.51027.0) and toolkit (4.1.51021.0). Ihave already reinstalled it. What have I not understood?

    • varunpuranik says:


      You need to add a reference to the assembly Microsoft.ServiceModel.DomainServices.Tools.TextTemplate.dll in your server project. You will also have to add a property to the client project indicating the fully qualified name of the code generator (for the defalt CSharpClientCodeGenerator included in the RIAServices toolkit, add

      Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35

      to your client project.)
      If you want more details on this, please refer to my post on T4 Code Generator Extensibility here.

      • srecov says:

        Thank you, I have seen this, but there is no such assembly on my computer, I can not find it neither in “Add Reference” dialog, neither from search.

  4. Julio Santos says:

    I love the subject of code generation, I’ve been using it for a decade or so, and the combination of RIA services delightful architecture and the extensibility provided by code generation makes me believe that I will use these technologies in all my future projects (the short budget and scope of my current project prevents me from engaging intelligently in this subject right now). But I’d like to mention how much I appreciate you blogging about it. I will get back to you in the near future, meanwhile thanks a million.

  5. Justin says:

    This is great, except that I can’t seem to customize the code gen for a AuthenticationDomainContext that derives from AuthenticationDomainContextBase. Is this feature not supported?

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: