Visual Studio 2010 T4 Code Generation for Templated Types

I am using the default T4 templates to generate code based on my UML Model. I have a number of classes that expose Template Parameters. These generate code as expected. For example the MessageResponse class exposes a Parameter of TMessageType. This generates the code as expected:

public class MessageResponse<TMessageType>

{}

The problem comes in when I bind this parameter to another class. For example I create a class called AccountMessageResponse which binds the Template Parameter to a class call Account. What I expect to see is a generated class as follows:

public class AccountMessageResponse : MessageResponse<Account>

{}

However, no code is generated. When I look at the output log for the code generation I see this message:

'RestModels::AccountMessageResponse' - (Class): no file generated because the 'AppliesToElement' method of the template 'ServiceClassTemplate.t4' evaluated to false.

Is code generation for templated types not supported in the current default templates? Is this something I need to do myself, or am I doing something wrong?

Thanks

Answers


It seems the problem occurs when you combine templated types with an inheritance relationship. Here is a workaround. To achieve the results required all you need to do is:

1) Add a template parameter, TMessageType, to your class RequestMessage. This will generate the following:

public class RequestMessage<TMessageType>
{}

2) Create a class called AccountRequestMessage.

3) Set the TemplateBinding parameter for AccountRequestMessage to 'RequestMessage'. Then set the TemplateBinding class to 'Account'. This will then generate the following wherever you have referenced AccountRequestMessage. For example if you have an operation Foo which returns AccountRequestMessage the following code will be generated:

public RequestMessage<Account> Foo()
{}

4) If you need to generate an actual class you then need to add a new class AccountRequestMessageImpl which inherits from AccountRequestMessage. This will generate:

public AccountRequestMessageImpl : RequestMessage<Account>
{}

This is a little counter intuitive but it is simpler than trying to modify the T4 templates.

Nick


Need Your Help

Memory Game in Python with Tkinter

python tkinter

I'm trying to make a memory game using python 3 and tkinter. I'm new to python so this is kind of hard. My problem is that I'm trying to call the method "show_word" in the class "Cell" from the class

URL Rewrite rule based on certain conditions

apache wordpress .htaccess mod-rewrite url-rewriting

I am in process to apply URL rewriting rule based on certain condition in my .htaccess file but have got struck due to my near to zero level knowledge of URL rewriting using Apache .htacces.Here is...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.