Quantcast
Channel: T4
Viewing all articles
Browse latest Browse all 7

T 4 beginners – part 4

$
0
0

T 4 beginners – part 4

T4, VS 2010, Entity Framework, Custom tool

this is the 4rd post on this series.

in this post we will focus on T4 Class feature control blocks.

you can download the code for this post here.

the series TOC is available here.

 

What is class feature control block?

class feature control block is one of the T4 extension mechanism that enable reuse of

T4 sections.

using class feature control block you can defines helper properties or methods,

that can be used from different T4 templates.

 

The syntax of class feature control block

class feature control block is having the following syntax <#+#>

Code Snippet
  1. <#+   // Start of class feature block
  2. private int Square(int i)
  3. {
  4.     return i*i;
  5. }
  6. #>

 

this extension can be use within control block or expression control block, as demonstrate:

  1. // This is hello number <#= i+1 #>, square <#= Square(i+1) #>

as you can the second expression block is using the Square (class feature method).

 

Where class feature control block can be define?

class feature control block can be both inline (which mean inside the T4 template)

or external (external helper file), which is more appropriate.

 

How to define external class feature control block?

to define class feature control block in external file, you should:

1. add text file into your project

(I’m changing the text file extension to *.tt.extension, but you can use whatever extension you like).

update: it is better to name the file extension *ttinclude

2. the class feature control block should be added into the extension file.

 

How to include external class feature control block?

if you want to use external class feature control block you should add

include directive to the T4 as demonstrate below:

<#@ include file="ClassFeatureBlock.tt.extension" #>

 

Summary

class feature control block is one of the strength, you can simplify the T4 template

by using predefine reusable methods.

 

Code sample available here.

 

kick it on DotNetKicks.com Shout it

Digg This

Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images