T 4 beginners – part 5

2010/06/05

no comments

T 4 beginners – part 5

T4, VS 2010, Entity Framework, Custom tool

this is the 5rd post on this series.

in this post we will focus on how to debug T4.

there is no special code for this post.

the series TOC is available here.

What cause T4 failure?

actually the T4 can fail in to different stage:

  • It can fail when VS compile the Control blocks or Directives (this is compile time failure)
  • It can fail because the Control blocks is throwing an exception (while the compiled T4 is executing).

 

Executing T4

it is important to understand the process of T4 executing in order to avoid long debugging nights.

because T4 is actually script it got to stage of execution,

as shown in the following diagram:

T4, VS 2010, Entity Framework, Custom tool

Which bug can be handle?

you cannot handle compilation bug.

if the Control block does not compile-able you cannot debug it, luckily most of the time

you will get a fairly good failure description and if you double-click on the error line,

you will get to the relevant code line.

if the Control blocks does compile you can debug it using the technique

describe in the following section.

 

How to debug T4

in VS 2010 we can open new instance of VS 2010,

open the T4 template file (from the new instance),

place break-point at any of the Control blocks (do not try to set the break-point

at the original instance, it won’t work),

and attach the original instance’s process (original VS 2010 process [ctrl + shift + p]).

the final step is: right click on the T4 file (at the original VS instance) and select run custom tool.

Update: use System.Diagnostics.Debugger.Break() within the T4 template,

there is a bug and you cannot place break point just by attaching to the process

you have to stop the the execution by using Debugger.Break(),

you can also set VS to stop on handled exceptions (ctrl+alt+E).

 

Summary

VS 2010 make it easier to debug T4 by setting break-point,

this feature was missing from previous versions.

Suggestions

in the previous post we learned how to use external class feature,

and in the upcoming post we will learn more technique for reusing T4.

it is recommended to break your T4 into smaller files and responsibilities which can be reuse,

this way you can debug smaller piece of code and ease the debugging process.

Credits

Some of the tips on this post was suggest by Igal Leibovich.

 

kick it on DotNetKicks.com Shout it

Digg This
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>