|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Introduction
The current feature list of
The entire development life cycle of creating custom templates using For latest downloads please refer to Codeplex. This article is based on CTP 2.6 and is more of a usage guideline that tries to answer how to use Without any further delay let's explore SCG in more details. Installing SmartCodeGeneratorNote: This article is based on CTP 2.6.0. For latest versions please refer to CodePlex.
CSharp Developers extract the SCG_CTP2.6_CSharp_VsTemplate.zip file and copy SmartCodeGenerator.zip to your My Documents\Visual Studio 2005\Templates\ProjectTemplates folder. In addition copy SmartCodeGeneratorTemplate.zip to your My Documents\Visual Studio 2005\Templates\ItemTemplates folder. Here is a glimpse of the extracted SCG_CTP2.6_CSharp_VsTemplate.zip.
VB.NET Developers extract SCG_CTP2.6_Vb_VsTemplate.zip and copy SmartCodeGeneratorVb.zip to your My Documents\Visual Studio 2005\Templates\ProjectTemplates folder. In addition copy SmartCodeGeneratorTemplateVb.zip to your My Documents\Visual Studio 2005\Templates\ItemTemplates folder.
This is what it takes to install Creating SmartCodeGenerator ProjectLets open Visual Studio 2005 and create our first
You will notice
This will automatically generate
Please keep note of the TheProperties.cs, DefaultTemplate
Note here the Custom property " Adding New Templates to the ProjectTo add a new template, right click on the Template Folder and click Add New Item.
Choose
Modifying a TemplateDouble click the DefaultTemplate.ascx file and go to source view mode in Visual Studio 2005. You will see something like this.
Note a template is nothing but a ASP.NET Modifying TheProperties.csYou should have already noticed the "
public class TheProperties
{
private string _testProperty;
public string TestProperty
{
get
{
return _testProperty;
}
set
{
_testProperty = value;
}
}
}
The SCG Framework will automatically generate a UI for this property like this.
Lets add a new property named public class TheProperties
{
private string _testProperty;
public string TestProperty
{
get
{
return _testProperty;
}
set
{
_testProperty = value;
}
}
private string _addedProperty;
public string AddedProperty
{
get
{
return _addedProperty;
}
set
{
_addedProperty = value;
}
}
}
If you run this project now, you will see the following.
Note: A textbox is automatically created for the new property we defined. You can also try adding other properties of Modifying Default.aspx.csWhen you run the project and click the GeneratedCode for Template:~/Templates/DefaultTemplate.ascx
Output filename:c:\temp\Example1.cs
You might be also wondering where it is defined to read from (DefaultTemplate.ascx) and write to (Example1.cs) as output. Please Open the Default.aspx.cs file and you will find the region " ToDo : Write/Modify Code As Required" with the following piece of code.
SCG framework comes with the InputTemplateAndOutputPath path2 =
new InputTemplateAndOutputPath("~/Templates/Template2.ascx",
@"c:\temp\Example2.cs");
this.IOList.Add(path2);
The SCG Framework also ships with
The _Default_OnGenerate and _Default_OnGenerateComplete methods are worth having a look at. void _Default_OnGenerate(object sender, EventArgs e)
{
GenerateFiles(IOList, e);
SerializeThePropertiesToFile();
}
private void GenerateFiles(List<InputTemplateAndOutputPath> ioList,
EventArgs e)
{
//This is going to Loop through all the templates of
//IOList for each TableSchemas
foreach (InputTemplateAndOutputPath io in ioList)
{
//This part shows the use of ScEventArgs that is
//passed in the pipeline
string report = ((ScEventArgs)e).Item["report"].ToString();
report = string.Format("{0} <BR>
GeneratedCode for Template:{1} Output FileName:{2}",
report, io.InputPathFilename, io.OutputPathFilename);
((ScEventArgs)e).Item["report"] = report;
_Util.GenerateOutputAsFile(Page, io);
//To Get GeneratedText use the following Method
//string generatedText = _Util.GeneratedOutputAsText(Page, io);
}
}
The above code is self explanatory and I loop through the void void _Default_OnGenerateComplete(object sender, EventArgs e)
{
lblReport.Text = ((ScEventArgs)e).Item["report"].ToString();
}
In Short the pipeline has different events that you can hook codes into and they are executed in this sequence as shown in Fig 16.
In the Default.aspx.cs I hooked up the following methods with the pipeline. void _Default_OnPreGenerate(object sender, EventArgs e)
void _Default_OnGenerate(object sender, EventArgs e)
void _Default_OnGenerateComplete(object sender, EventArgs e)
What I did here within these methods are:
The From the above discussion we have learned how templates work in Writing your First TemplateObjectiveWe will write a template which will take two properties: public class TestClass
{
public TestClass()
{
//Add constructor here
}
private string testproperty
public string TestProperty
{
get
{
return testproperty;
}
set
{
testproperty = value;
}
}
}
Step 1 - Create a new SmartCodeGenerator ProjectIn Visual Studio 2005, Select File>New>Website>SmartCodeGenerator. You will notice (SmartCodeGenerator) Project Template in your (MyTemplates) Section.
Step 2 - Declare Properties in the "TheProperties" classAs defined in the objective we want to have 2 string properties one for public class TheProperties
{
public TheProperties( )
{
//Please Assign Default Values here
this.ClassName = "TestClass";
this.PropertyName = "TestPropertyName";
}
private string className;
public string ClassName
{
get { return className; }
set { className = value; }
}
private string propertyName;
public string PropertyName
{
get { return propertyName;}
set { propertyName = value;}
}
}
We know the SCG framework will automatically read from the instance of Step 3 - Add a new SCGTemplateTo add a new Select "SmartCodeGeneratorTemplate" and name this ascx File as "ExampleTemplate.ascx". Step 4 – Modify SCGTemplateOpen the code behind file of ExampleTemlate.ascx and define a property private string theclassname;
public string TheClassName
{
get { return theclassname; }
set { theclassname = value; }
}
Inside the public override void PreGenerateTemplateCode( )
{
theclassname = TheProperties.ClassName;
}
Go to the source view of your ExampleTemplate.ascx and modify as follows. <%@ Control Language="C#" AutoEventWireup="true"
CodeFile="ExampleTemplate.ascx.cs" Inherits=" ExampleTemplate" %>
//This example Template shows creating a dynamic class from template
public class <%=this.TheClassName%>
{
public <%=TheProperties.ClassName%>()
{
//Add constructor here
}
private string <%=TheProperties.PropertyName.ToLower()%>;
public string <%=TheProperties.PropertyName %>
{
get
{
return <%=TheProperties.PropertyName.ToLower()%>;
}
set
{
<%=TheProperties.PropertyName.ToLower()%> = value;
}
}
}
While doing this, you should have already noticed Intellisense support. In this demo we used both of these. Step 5- Modify Default.aspx.csOpen the default.aspx.cs file. Go to region Todo : Write/Modify Code As Required Map the Input Template should be ExampleTemplate.ascx and Output file should be c:\temp\Example1.cs. It should look somewhat like this. void _Default_OnPreGenerate(object sender, EventArgs e)
{
//You can use the handy IOList to Map Input Template and OutputPath
this.IOList.Add(
new InputTemplateAndOutputPath
("~/Templates/ExampleTemplate.ascx",
@"c:\temp\Example1.cs"));
//Adding a report item in the args
((ScEventArgs)e).Item.Add("report", string.Empty);
lblReport.Text = string.Empty;
}
Step 6 – Run the SCGProject and Generate outputAfter you run the project you will see something like this.
Change the
Conclusion
Resources and Links related to SmartCodeGenerator
|
||||||||||||||||||||||