Speed up with Project Templates
page 4 of 4
by Ameet Phadnis
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 31276/ 86

Putting all together

Putting it together –

In this section we are going to build a sample template using the knowledge in this article. Let us assume that we have an application called XYZApp. Developers build their own modules that actually plugs into  the bigger application XYZApp. All modules will have at least one web user control and one Class file. The Web User control file name by default should have the same name as project name. The class file should have the same name as project with the word DB at the end. For example, you are creating XYZMod module then the user control should be called XYZMod.ascx and the class file should be called XYZModDB.vb. The class in the XYZModDB should be called XYZModDB. All the classes should fall under the XYZApp namespace.

 

To achieve this first you need to create the tempXYZ.ascx and tempDB.vb files. You will also need Assemblyinfo.vb file.

The best way to do this is –

  1. Create new Web Application called tempXYZMod.
  2. Once Visual Studio.NET creates the Web application, delete all the files except for the AssemblyInfo.vb file.
  3. Add Web User Control called tempXYZ.ascx.
  4. Add class called tempDB
  5. Save the application.
  6. Open the code for tempXYZ.ascx
  7. At the top before Public Class tempXYZ insert the namespace XYZApp. So, now your code will look like     

                        Namespace XYZApp

                                    Class tempXYZ

  1. Open the tempDB class code. Insert the namespace as above.
  2. Save the application

 

Now you can close Visual Studio.NET. Follow these steps to create your template folder –

  1. Under Program Files -> Microsoft Visual Studio .NET 2003->vb7->vbWizards. Create a folder called XYZMod.
  2. Under this folder create two folders Scripts and Templates.
  3. Under Scripts folder create another folder called 1033
  4. Copy the default.js file from Webapplication->Scripts->1033 folder.
  5. Under Templates folder create folder called 1033.
  6. Copy tempXYZ.ascx, tempXYZ.ascx.vb, tempDB.vb, AssemblyInfo.vb and tempXYZMod.vbProj files from your application folder to the templates->1033 folder.
  7. Open tempXYZ.ascx in notepad.
  8. Change the CodeBehind property value from "tempXYZ.ascx.vb" to "$FILENAME$.vb"
  9. Change the Inherits property value from " tempXYZMod.XYZApp. .tempXYZ " to "XYZApp.$CLASSNAME$".
  10. Save this file and close it.
  11. Open tempXYZ.ascx.vb file in notepad.
  12. Change the class name from Public Class tempXYZ to Public Class [!output PROJECT_NAME].
  13. Save and close this file.
  14. Open the tempDB.vb file in notepad.
  15. Change the class declaration from Public Class tempDB to Public Class [!output SAFE_ITEM_NAME].
  16. Save and Close this file.
  17. Open the tempXYZMod.vbProj file.
  18. Remove everything under <Files><Include> tag. It should just have <Files><Include></Include></Files>.
  19. Save and Close this file.
  20. Go to the Scripts->1033 folder and open default.js file in notepad.
  21. Comment out all the code in Javascript file that adds - Global.ascx, Web.Config, Style.css and Webform.aspx files to the project.
  22. Also comment out the code that sets the WebForm1.aspx as start up page.
  23. Change the code that points to the Webapplication.vbproj to point to tempXYZMod.vbproj.
  24. Add the code to add the tempXYZ.ascx, tempXYZ.ascx.vb and tempDB.vb files to the project and rename the files to <projectname>xyz.ascx, <projectname>.ascx.vb and <projectname>DB.vb.
  25. Now your Javascript file will look as follows –

//

// Copyright (c) Microsoft Corporation 2001-2002. All rights reserved.

//

 

function OnFinish(selProj, selObj)

{

    var oldSuppressUIValue = true;

    try

    {

        oldSuppressUIValue = dte.SuppressUI;

        var bSilent = wizard.FindSymbol("SILENT_WIZARD");

        dte.SuppressUI = bSilent;

 

        var strProjectName = wizard.FindSymbol("PROJECT_NAME");

        var strProjectPath = wizard.FindSymbol("PROJECT_PATH");

        var strTemplatePath = wizard.FindSymbol("TEMPLATES_PATH");

        //var strTemplateFile = strTemplatePath + "\\WebApplication.vbproj";

        var strTemplateFile = strTemplatePath + "\\tempXYZMod.vbproj";

        var project = CreateVSProject(strProjectName, ".vbproj", strProjectPath, strTemplateFile);

        if( project )

        {

            strProjectName = project.Name;  //In case it got changed

 

            var item;

            var editor;

 

            //strTemplateFile = strTemplatePath + "\\Global.asax";

            //item = AddFileToVSProject("Global.asax", project, project.ProjectItems, strTemplateFile, false);

 

 

                    //item = DoesFileExistInProj(project,"Web.config")

            //if (item == null)

            //{

              // strTemplateFile = strTemplatePath + "\\Web.config";

               //item = AddFileToVSProject("Web.config", project, project.ProjectItems, strTemplateFile, false);

            //}

 

            //strTemplateFile = strTemplatePath + "\\Styles.css";

            //item = AddFileToVSProject("Styles.css", project, project.ProjectItems, strTemplateFile, false);

 

 

            var strRawGuid = wizard.CreateGuid();

            wizard.AddSymbol("GUID_ASSEMBLY", wizard.FormatGuid(strRawGuid, 0));

 

            strTemplateFile = strTemplatePath + "\\AssemblyInfo.vb";

            item = AddFileToVSProject("AssemblyInfo.vb", project, project.ProjectItems, strTemplateFile, false);

            if( item )

            {

                item.Properties("SubType").Value = "Code";

            }

           var newfilename;

            strTemplateFile = strTemplatePath + "\\tempXYZ.ascx";

            newfilename = strProjectName + ".ascx";

            item = AddFileToVSProject(newfilename, project, project.ProjectItems, strTemplateFile, false);

 

            strTemplateFile = strTemplatePath + "\\tempXYZ.ascx.vb";

            newfilename = strProjectName + ".ascx.vb";

            item = AddFileToVSProject(newfilename, project, project.ProjectItems, strTemplateFile, false);

 

            strTemplateFile = strTemplatePath + "\\tempDB.vb";

            newfilename = strProjectName + "DB.vb";

            item = AddFileToVSProject(newfilename, project, project.ProjectItems, strTemplateFile, false);

 

 

//            AddDefaultWebFormsPropertiesToWizard(dte, wizard, project);

//            strTemplateFile = strTemplatePath + "\\WebForm.aspx";

 //           item = AddFileToVSProject("WebForm1.aspx", project, project.ProjectItems, strTemplateFile, false);

 

//            var configs = new Enumerator(project.ConfigurationManager);

 //           for(;!configs.atEnd();configs.moveNext())

 //           {

  //              configs.item().Properties("StartPage").Value = "WebForm1.aspx";

  //          }

  //          editor = item.Open(vsViewKindPrimary);

  //          editor.Visible = true;

 

            project.Save();

        }

       

        return 0;

    }

    catch(e)

    {  

        switch(e.number)

        {

        case -2147024816 /* FILE_ALREADY_EXISTS */ :

            return -2147213313;

 

        default:

            SetErrorInfo(e);

            return e.number;

        }

    }

    finally

    {

        dte.SuppressUI = oldSuppressUIValue;

    }

}

 

 

  1. Go to VB7->vbProjects folder.
  2. Make a copy of WebApplication.vsz file and name it XYZMod.vsz.
  3. Open the XYZMod.vsz file.
  4. Change the Param="WIZARD_NAME = WebApplication" to Param="WIZARD_NAME = XYZMod"
  5. Your file should look like –

VSWIZARD 6.0

Wizard=VsWizard.VsWizardEngine.7.1

Param="WIZARD_NAME = XYZMod"

Param="WIZARD_UI = FALSE"

Param="PROJECT_TYPE = VBPROJ"

 

  1. Save and Close this file.
  2. Open Notepad and enter the following entry –

XYZMod.vsz|{164B10B9-B200-11D0-8C61-00A0C91E29D5}|XYZ Module Template|1|XYZ Module Assembly Template|{164B10B9-B200-11D0-8C61-00A0C91E29D5}|4503| 1|XYZMod

  1. Save this file as XYZMod.vsdir
  2. Close this file.
  3. Now Close Visual Studio .NET 2003 if it is open
  4. Open Visual studio.NET 2003.
  5. Go to File New Project.
  6. Under Visual Basic Projects you should see new entry under templates list called XYZ Module template.
  7. Under location type in http://localhost/testXYZMod (make sure it is pointing to webserver because this is web application).
  8. Click open.
  9. You may be asked whether you want to overwrite the ascx.vb file. Just click Yes.
  10. Now you have your startup application ready. You can open the testXYZMod.ascx file and make sure codebehind and Inherits property are pointing to the right values. Inherits will be pointing to XYZApp.testXYZMod and your Codebehind should be pointing to testXYZMod.ascx.vb.
  11. Now open the testXYZMod.ascx file and check whether you the class name has testXYZMod.
  12. Similarly check the changes in the class file too.

 

The above procedure allowed you to create a module template for your bigger application. The templates can save lot of time in coding. Many developers spend too much time adding the same code over and over again. You can also build Visual Studio.NET add-ins to automate some of your coding. I used the above template building wizard for the famous DotNetNuke Private assembly with some Visual Studio.NET add-in to automate new Module development. The whole process for simple modules used to take 2 hours and now it is down to 30 minutes.

 

Summary – In this article, I have explained how project templates can be used to save time for your code. You can have variations in your templates like having imports statement in your codebehind file or use references in your project file. This will standardize the process for all your developers building modules or applications.


View Entire Article

User Comments

Title: very good   
Name: Richard
Date: 2007-01-16 7:59:44 AM
Comment:
very good ,thank u
Title: Speed up with Project Templates   
Name: Prasad
Date: 2004-08-10 8:08:58 AM
Comment:
Excellent article. It saves development time.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-20 11:08:31 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search