I was recently asked to supply code snippets for use by a client's development team so that it would be easier for them to adhere to the development standards. Code snippets are also very good for use in speeding up repeated code. For instance, we use a standard block of code to access databases (see below).
:: begin code snippet ::
'setup the SQL statement
Dim sql As String = "select * from MyTable"
Using con As New SqlClient.SqlConnection(connectionString)
con.Open()
Using cmd As SqlClient.SqlCommand = con.CreateCommand()
With cmd
'setup the command
.CommandType = CommandType.Text
.CommandText = sql
'add the parameters
'execute the command
Using adapter As New SqlClient.SqlDataAdapter(cmd)
ds = New DataSet()
adapter.Fill(ds, "Data")
End Using
End With
End Using
End Using
:: end code snippet ::
This format is standard across most of our data access methods (with some small exceptions) and the developers tend to copy/paste from another class/method or type it out themselves.
Code snippets are pretty easy to create. To do so, you first need to create a snippet file (*.snippet) which is an XML file containing the code block and other information (which I will go over below) and then import that into Visual Studio using the Code Snippets Manager. You can also create a VSI (Visual Studio content Installer) along with the corresponding content file. The VSI file is simply a zip file with the extension changed to ".vsi" that also contains a “*.vscontent” file. A content file is an XML file that lists out the contents of the VSI file along with additional information about the various snippet files. As you can see below, the XML provides information about the location and contents of the snippet file as well as what version of Visual Studio supports it. If the “ContentVersion” is “1.0”, then it will be supported by both VS 2005 and 2008. If it is “2.0”, then the snippet file will be supported by VS 2008. If the VSI file contains more than one snippet, you simply need to provide multiple content blocks (one for each file that needs installed).
:: begin standard content file ::
<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">
<Content>
<FileName>database\bwcDbSqlQuery.snippet</FileName>
<DisplayName>bwcDbSqlQuery</DisplayName>
<Description>Standard SQL Server query (select) for database class.</Description>
<FileContentType>Code Snippet</FileContentType>
<ContentVersion>1.0</ContentVersion>
<Attributes>
<Attribute name="lang" value="vb" />
</Attributes>
</Content>
</VSContent>
:: end standard content file ::
A snippet file can contain more than one code snippet as well. The “CodeSnippet” element consists of two main elements, the header and the snippet. In the header, you will supply general information that will appear in the code snippet manager as well as the shortcut for inserting the snippet. In the snippet section, you will supply the code to be imported along with other optional sections. These optional sections can be used to check for references needed by your code, to import necessary namespaces if they don’t already exist, and to specify replaceable parameters. Replaceable parameters will show up as highlighted in your code and make it easy to change the parameter name in one place and have it cascade to all other instances within the snippet.
:: begin snippet file ::
<?xml version="1.0"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Standard SQL Server query</Title>
<Author>Brian Cordell</Author>
<Description>Standard SQL Server query (select) for database class.</Description>
<Shortcut>bwcDbSqlQuery</Shortcut>
</Header>
<Snippet>
<References>
<Reference>
<Assembly>System.dll</Assembly>
</Reference>
<Reference>
<Assembly>System.Data.dll</Assembly>
</Reference>
<Reference>
<Assembly>System.Xml.dll</Assembly>
</Reference>
</References>
<Imports>
<Import>
<Namespace>System</Namespace>
</Import>
<Import>
<Namespace>System.Data</Namespace>
</Import>
</Imports>
<Declarations>
<Object>
<ID>ds</ID>
<Type>DataSet</Type>
<ToolTip>Replace with desired variable name.</ToolTip>
<Default>ds</Default>
</Object>
<Object>
<ID>sql</ID>
<Type>String</Type>
<ToolTip>Replace with desired variable name.</ToolTip>
<Default>sql</Default>
</Object>
<Object>
<ID>con</ID>
<Type>SqlClient.SqlConnection</Type>
<ToolTip>Replace with desired variable name.</ToolTip>
<Default>con</Default>
</Object>
<Object>
<ID>connectionString</ID>
<Type>String</Type>
<ToolTip>Replace with desired variable name.</ToolTip>
<Default>connectionString</Default>
</Object>
<Object>
<ID>cmd</ID>
<Type>SqlClient.SqlCommand</Type>
<ToolTip>Replace with desired variable name.</ToolTip>
<Default>cmd</Default>
</Object>
<Object>
<ID>adapter</ID>
<Type>SqlClient.SqlDataAdapter</Type>
<ToolTip>Replace with desired variable name.</ToolTip>
<Default>adapter</Default>
</Object>
</Declarations>
<Code Language="VB" Kind="method body">
<![CDATA[
'setup the SQL statement
Dim $sql$ As String = ""
Using $con$ As New SqlClient.SqlConnection($connectionString$)
$con$.Open()
Using $cmd$ As SqlClient.SqlCommand = $con$.CreateCommand()
With $cmd$
'setup the command
.CommandType = CommandType.Text
.CommandText = $sql$
'add the parameters
'execute the command
Using $adapter$ As New SqlClient.SqlDataAdapter($cmd$)
$ds$ = New DataSet()
$adapter$.Fill($ds$, "Data")
End Using
End With
End Using
End Using
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
:: end snippet file ::
Remember that code snippets are a great way to speed up the development process and ease the adoption of standards by providing “ready made” blocks of code.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment