In this post I would to present a basic text template, and show how it could be easier to generate code using text templates than with the code DOM.
A text template is a document with special escape sequences to indicate where variable portions of text should be inserted. The escape sequences can also be more complex by having code to generate the text that is to be included. This is quite useful in the dynamic generation of web pages but it can also be applied to other types of text documents. In particular, the ability to enable automatic generation of code could ease the burden of repetitive coding tasks. Why not use the Code DOM? You may be thinking that .Net framework already has a way to generate code files, the code DOM (System.CodeDOM). It even can render in more than one language. Why not use it?
The code DOM is a .Net API that allows you to build an object model that represents your code, and gives you the ability to either compile it directly to a binary or generate source code as output. This is very powerful, and there are cases where the code DOM is useful, but there are cases where use of text templates is better. I would like to illustrate that it could take you more time, and more code, to generate the code DOM equivalent of some existing code than creating a text template. As an example, let’s take the common hello world console application shown below as a starting point: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world!");
}
}
}
To generate these 15 lines of code, it requires 73 lines of code to generate those lines from the code DOM. The generation code is shown below: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using Microsoft.CSharp; namespace MakeHelloWorld
{
class Program
{
static void Main(string[] args)
{
CodeCompileUnit unit = new CodeCompileUnit(); CodeNamespace ns = new CodeNamespace("HelloWorld");
unit.Namespaces.Add(ns); // Using directives
ns.Imports.Add(new CodeNamespaceImport("System"));
ns.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
ns.Imports.Add(new CodeNamespaceImport("System.Linq"));
ns.Imports.Add(new CodeNamespaceImport("System.Text")); // Add class Program
CodeTypeDeclaration myClass = new CodeTypeDeclaration("Program"); myClass.IsClass = true;
// Add Main method
CodeMemberMethod method = new CodeMemberMethod();
method.Name = "Main";
method.Attributes = MemberAttributes.Static; CodeParameterDeclarationExpression param =
new CodeParameterDeclarationExpression(typeof(string[]), "args"); method.Parameters.Add(param);
Read more: Developers' Hangout Blog
A text template is a document with special escape sequences to indicate where variable portions of text should be inserted. The escape sequences can also be more complex by having code to generate the text that is to be included. This is quite useful in the dynamic generation of web pages but it can also be applied to other types of text documents. In particular, the ability to enable automatic generation of code could ease the burden of repetitive coding tasks. Why not use the Code DOM? You may be thinking that .Net framework already has a way to generate code files, the code DOM (System.CodeDOM). It even can render in more than one language. Why not use it?
The code DOM is a .Net API that allows you to build an object model that represents your code, and gives you the ability to either compile it directly to a binary or generate source code as output. This is very powerful, and there are cases where the code DOM is useful, but there are cases where use of text templates is better. I would like to illustrate that it could take you more time, and more code, to generate the code DOM equivalent of some existing code than creating a text template. As an example, let’s take the common hello world console application shown below as a starting point: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world!");
}
}
}
To generate these 15 lines of code, it requires 73 lines of code to generate those lines from the code DOM. The generation code is shown below: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using Microsoft.CSharp; namespace MakeHelloWorld
{
class Program
{
static void Main(string[] args)
{
CodeCompileUnit unit = new CodeCompileUnit(); CodeNamespace ns = new CodeNamespace("HelloWorld");
unit.Namespaces.Add(ns); // Using directives
ns.Imports.Add(new CodeNamespaceImport("System"));
ns.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
ns.Imports.Add(new CodeNamespaceImport("System.Linq"));
ns.Imports.Add(new CodeNamespaceImport("System.Text")); // Add class Program
CodeTypeDeclaration myClass = new CodeTypeDeclaration("Program"); myClass.IsClass = true;
// Add Main method
CodeMemberMethod method = new CodeMemberMethod();
method.Name = "Main";
method.Attributes = MemberAttributes.Static; CodeParameterDeclarationExpression param =
new CodeParameterDeclarationExpression(typeof(string[]), "args"); method.Parameters.Add(param);
Read more: Developers' Hangout Blog
0 comments:
Post a Comment