to T4 templates and how you can use them in your projects now to save you time. Richard Goforth [email protected] Contact Magenic 1600 Utica Avenue South, Suite 800 St. Louis Park, MN 55416 1 (877)-277-1044 [email protected]
Text blocks with control logic (similar to programming for the web) » Regular code, a small isolated program with a very specific purpose » Any input and any output (you can do anything you want, though it is not necessarily wise).
» The ability to repeat yourself less often then before » More places with single location maintenance than you were able to do without T4 » Reduce manual mistakes and omissions in syncing code changes across different types of files and databases
Online Templates -> Search » Tangible T4 Editor » Syntax highlighting » IntelliSense in code blocks » T4 Toolbox » Includes several basic templates to get you started on a number of different tasks
{}) Loops and Decision Trees <# Inline Evaluation Used for a substituting a single field or value Use anything in the current scope <#= Class Function Location doesn’t matter Class blocks that sit next to the generating class <#+ Directive Statement Define output, imports, and include statements New in VS2010: template parameters <#@
extension=".cs" #> <#= System.DateTime.Now.ToString() #> //Comment at the top of the file <# foreach(var prop in getProperties()) { WriteLine(prop.key); } #> <#+ public Dictionary<Type, string> getProperties() { … } #>
TextTransformation class » Write / WriteLine » Text blocks are converted to “Write” statements on preprocessing » PushIndent / PopIndent /ClearIndent » Use these with a string argument to set the indent to that string. » Warning » Will output a Visual Studio warning upon compiling the template » Error » Catch Exceptions in your template and use the error method to output for a better debugging experience
think of Edmx Model First EF Uses Microsoft Libraries to deal with the edmx file SQL Database Enums in a DB First Environment CRUD Stored Proc Generation Linq To Sql Code from an existing DB
T4 Template Manager class by Damien Guard » http://damieng.com/blog/2009/11/06/multiple-outputs-from-t4- made-easy-revisited » The EF Way » EntityFrameworkTemplateFileManager in EF.Utility.CS.ttinclude
defaults defined in the Model output as part of the db script. » SSDLToSQL10.tt » Add a call to a new method (WriteDefault()) to output the default value for a property in the edmx file to the sql output » GenerateTSQL.Utility.ttinclude » Create the write default method that takes the EDMProperty and extracts the appropriate sql text for a default value
extra method on each class » I wanted to take the dictionary created from Telerik controls and populate values on my generated classes directly from them. » I wanted to set associated navigation properties as well » SetPropertiesFromDictionary()
import namespace="System.Diagnostics" #> » <# Debugger.Launch(); #> » Added at the beginning of your code, this will prompt you to open a new instance of VS and debug the template generation » <# Debugger.Break(); #> » re-enter the debugger later in the code
don’t have to run in the devenv process, and thus can be debugged directly. » “PreProcessed” is now “Design-Time” » In 2010 » Template inheritance » Parameters » In Tangible T4 Editor 2.0 » Transform on Build