posts - 217, comments - 218, trackbacks - 239

My Links

News




I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net) and born in Bangladesh.
I work for Ocean Informatics Pty Ltd as a Senior Developer - Analyst.
I am also co-founder and core developer of Pageflakes (acquired by LiveUniverse) www.pageflakes.com
and most recently created SmartCodeGenerator

My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Smart Code Generator .NET: Usage Overview
Smart Code Generator .NET: Architectural Overview
Smart Code Generator .NET: using with NAnt and Cassini

Archives

Free Programming Language Training

Example2: SmartCodeGenerator (asp.net 2.0)and the Profile Object

Example2: Generating Code from existing project/template. And getting familiar with the Asp.Net Profile object.

Step1 Opening Existing Template, Launching and Generating Code.

Open Example2 from Example_Projects folder, as a website project in Visual Studio 2005.

Step2 Launching the Generator

Run the project you have opened from Visual Studio 2005 and this will display the default.aspx page.

Notice the property1 and the textbox to enter value. The properties lets you customize the generated code. Here we are collecting value for property1 and this can be used in the generated template.

Step3 Generating the Text or Code

Simply click the Generate Button. And this will generate code for you. If there was any compilation error VS2005 would have picked that up very easily as it normally does for your asp.net application.

Step5. Reviewing the Template

Expand the Template Folder and open Exampl2Template.ascx file and go to source view. This would look like the following.

And the Code behind is where I have declared a public property TheClassName. And assigned the Profile.ScProperties.ClassName to theclassname in the PreGenerateTemplateCode() method. [ I will discuss this bit in a short while].

And the Generated Code should look like this. The DateTime.Now.ToString() is simply replaced by the current datetime.

Step5 Understanding the role of Asp.Net Profile in SmartCodeGenerator Framework.

Step3. You might have already noticed the ClassName Property at runtime. And wondering where is it coming from.

Check the web.config profile section.

<profile ......>

  <properties>

    <group name = "ScProperties">

        <add name="ClassName" type="string" defaultValue="TestClass"/>

...

</profile>

SmartCodeGenerator framework checks the profile properties and automatically creates UIProperty for you. In this case it created the ClassName as declared as profile properties in the web.config.

Yes you guessed right if you add more properties in your web.config, the framework will also pick them up and generate UIProperty for you. You may ask why I used Profile? answer is it has intellisense support, so when we will write our template this will be very helpful. Also it acts as Global variable and can be accessed from anywhere in the website project [in our case we can use it easily in our template]. Check the Example2Template.ascx (source view) and Example2Template.ascx.cs an notice how Profile properties are accessed easily.

Note: The communitydrop1 at this stage supports only supports the following types : string, integer, boolean and SmartCodeGen.WebUtil.PropTypes.ScSqlTablesProperty

The framework is extensible and adding support to any types you desire is very easy. I ll discuss that in a seperate tutorial.

Step6. Understanding the “Generate” PipeLine and the InputTemplateAndOutPutPath Class

You might me also wondering where are we defining read from (Example2Template.aspx) and write to (Example2.cs) as output. Open the Default.aspx.cs file and you will find the region ToDo : Write/Modify Code As Required.

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/Example2Template.ascx", @"c:\temp\Example2.cs"));

//Adding a report item in the args

        ((ScEventArgs)e).Item.Add("report", string.Empty);

        lblReport.Text = string.Empty;

    }

Notice the framework comes with InputTemplateAndOutputPath Class where you can define input location and output location. So your Templates folder may end up having 100s of templates but you can run your desired templates from this section. Simply add it to the list object (IOList) like I did here.

By this time you should have also noticed this 3 methods in the region Todo : Write/Modify Code As Required

void _Default_OnPreGenerate(object sender, EventArgs e)

void _Default_OnGenerate(object sender, EventArgs e)

void _Default_OnGenerateComplete(object sender, EventArgs e)

as the name suggests it executes in this order : PreGenerate => Generate => GenerateComplete

So you can do bits and pieces before generate => OnGenerate and After Generate. And this methods will be always called.

what I did here is:

PreGenerate: I Prepared the List of InputOutputTemplate

OnGenerate: I Generated the File/Files

OnGenerateComplete: I Displayed the result in the screen...

Easy huh...

I have also used ScEventArgs and _Util.xxx in this example and I ll explain them in my next example.

Step7. Summary

We have learned how templates work in SmartCodeGenerator. Basically we define profile properties in the ScProperties section and that turns into UIProperty where we can enter desired values runtime. We can enter as many profile properties as we wish, at this stage we can only use limited types, but the framework is 100% extensible and support to other types can be introduced very easily.

We can map input and output and generate files. The default.aspx contains methods...

1. void _Default_OnPreGenerate(object sender, EventArgs e)

2. void _Default_OnGenerate(object sender, EventArgs e)

3. void _Default_OnGenerateComplete(object sender, EventArgs e)

and these are executed in this order (1->2->3).

Print | posted on Saturday, November 25, 2006 10:09 AM |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 6 and type the answer here:

Powered by: