Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Maximize Your Productivity with Project Item Templates : Page 5

Create your own Windows Forms, Web Forms, or class templates to reuse code and maximize your productivity in Visual Studio 2003.

Customizing the Template File
At this point, your template should appear in the Add New Item dialog box—but the template is not yet customized. Go back and edit the BOTemplate.vb or BOTemplate.cs file and add any of the common elements that you want in your template. (You will not need to re-edit any of the other files.)

For this example, enter a standard comment header block. In C#, use the provided XML commenting feature. For VB, you can use a classic header comment style or manually enter XML-style comments (<summary>your comments go here...</summary>) for future compatibility with VB 2005. In both languages, define any common regions that you use in your classes.

Add any commonly used imports to the template using the Imports statement in VB or the using directive in C#. For example, in a C# business object your template may include:

using System.Diagnostics; using System.Data; using System.Configuration;

In VB, many of the common imports are already predefined as project properties. Right-click on the project, select Properties from the context menu, and click on Imports under the Common Properties folder to see the list of predefined project imports.

The most important step in defining a truly useful template is to create the standard set of interfaces or methods that are common to your business object. Most business objects have a method that obtains and populates the business object and a method that serializes or stores the business object. For this simplistic example, name the methods Retrieve and Save, respectively.

In VB:

''' <summary> ''' Retrieves the business object data. ''' </summary> ''' <param name="iID">Primary key for the business ''' object.</param> ''' <returns>DataSet containing the business ''' object data.</returns> Public Function Retrieve(ByVal iID As Integer) _ As DataSet Dim ds As DataSet Dim sConn As String ' Obtain the connection string sConn = Configuration.ConfigurationSettings. _ AppSettings("CONNECTION").ToString() ' Retrieve the dataset 'TODO: Add the name of the stored procedure 'as the third parameter ds = SqlHelper.ExecuteDataset(sConn, _ CommandType.StoredProcedure, "", _ New SqlClient.SqlParameter("@ID", iID)) 'TODO: Set the table name ds.Tables(0).TableName = "" Return ds End Function

In C#:

/// <summary> /// Retrieves the business object data. /// </summary> /// <param name="iID">Primary key for the /// business object.</param> /// <returns>DataSet containing the business /// object data.</returns> public DataSet Retrieve(integer iID) { DataSet ds=null; string sConn=null; // Obtain the connection string sConn = ConfigurationSettings. AppSettings["CONNECTION"].ToString(); // Retrieve the dataset //TODO: Add the name of the stored procedure as // the third parameter ds = SqlHelper.ExecuteDataset(sConn, CommandType.Text, "", new SqlClient.SqlParameter("@ID", iID)); //TODO: Set the table name ds.Tables[0].TableName = ""; return ds; }

Add any other methods such as validation, authentication, or any other common functionality that you use in your business objects. Include as much commenting and code in these methods as you can while still keeping the method general enough to be reused as a template. Make liberal use of the Task List features of Visual Studio and add "ToDo" notes to aid in using the template.

At any point, you can test your template. Just open any exiting project or create a new project, right-click on the project and select Add and then choose Add Class from the context menu, give the class a name, and click OK. If the class file is created with your new template, all is well.

You can use the technique described in this article to create templates for Windows Forms, Web Forms, classes, or any other type of project item.

Though there are quite a few steps to this process, creating a template is not difficult and is really worth the effort, if you use the template frequently. Templates are also great for team development efforts because they promote consistency in how the code is structured. To share your template with another developer, copy the wizard subdirectory, the wizard file (.vsz), and the updated VSDir (.vsdir) file to the equivalent directories on the other developer's system.

Deborah Kurata is cofounder of InStep Technologies Inc., a professional consulting firm that focuses on turning your business vision into reality using Microsoft .NET technologies. She has over 15 years of experience in architecting, designing, and developing successful .NET applications. Deborah is the author of several books, including "Doing Objects in Visual Basic 6.0" (SAMS) and "Doing Web Development: Client-Side Techniques" (APress). She is on the INETA Speaker's Bureau and is a well-known speaker at technical conferences.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date